hi,
I’m using convert html to excel, and notice css style will slow performance very much, is there any solution or suggestion for that?
it spent about 35 seconds to complete work, if i remove css style, it’s will reduce to 8 seconds
my code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Excel = GemBox.Spreadsheet;
namespace GemboxPerformanceTest
{
internal class Program
{
static void Main(string[] args)
{
BuildExcelWithHTML();
}
private static void BuildExcelWithHTML()
{
Excel.SpreadsheetInfo.SetLicense("dfdfdf==A");
var htmlOptions = new Excel.HtmlLoadOptions();
htmlOptions.DateTimeFormats.Insert(0, "MM/dd/yyyy");
var html = GenerateHTML();
var htmlStream = new MemoryStream(htmlOptions.Encoding.GetBytes(html));
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
var workbook = Excel.ExcelFile.Load(htmlStream, htmlOptions);
var xlsxStream = new MemoryStream();
workbook.Save("test.xlsx", Excel.SaveOptions.XlsxDefault);
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
// Format and display the TimeSpan value.
string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
ts.Hours, ts.Minutes, ts.Seconds,
ts.Milliseconds / 10);
Console.WriteLine("RunTime " + elapsedTime);
}
private static string GenerateHTML()
{
var template = @"<html>
<head>
<style>
@page {
margin: 0.5cm;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
font-family:""Calibri, sans-serif"";
}
</style>
</head>
<body>
{{content}}
</body>
</html>";
var result = new StringBuilder();
var columns = new List<dynamic>
{
new { Name = "Date", Width = "100" },
new { Name = "Name", Width = "200" },
new { Name = "User", Width = "150" },
new { Name = "Tag", Width = "100" },
new { Name = "Entry", Width = "100" },
new { Name = "Remarks", Width = "300" },
new { Name = "c", Width = "100" },
new { Name = "b", Width = "100" },
};
result.Append("<table><thead><tr>");
foreach (var column in columns)
{
result.AppendFormat("<th width='{1}px'>{0}</th>", column.Name, column.Width);
}
result.Append("</tr></thead>");
result.Append("</tbody>");
for (var i = 0; i < 20000; i++)
{
result.Append("<tr>");
foreach (var column in columns)
{
var value = column.Name + i;
// if (column.Name == "Remarks")
// {
// value = @"HSE Incidents: No Current Activity @ dfdf: RELEASED TO RIO PECOS 27-18 SL UNIT 1 # 251 Depth @ dfdfdf: 1,318’ MD MW @ dfdf: Last Survey: (1,263’ MD, 3.21° INC, 295.10° AZI, 1,262.01’ TVD, 19.05’ VS, 9.41 +N/-S:, -39.53 +E/-W:, 0.81° DLS) Distance Above/Below & Left/Right of target plan/line: 6’ AHEAD & 3’ LEFT 24 Hr Summary: DRILL F/ 698' TO TD @ 1,dfdf'. CUC, PUMP SWEEPS, SPOT PILL, TOOH FOR CASING & CEMENT OPERATIONS. LAND CASING @ 1,303'. CIRC & COND. RD FRANKS, REMOVE LANDING JOINT AND RELEASE RIG TO RIO PECOS 27-18 SL UNIT 1 #251 @ 1400hRS Cumulative NPT for well: 1 HRS Daily Cost/Cumulative Cost: $134,698 / $218,407
//" + i;
// }
// else if (column.Name == "Date")f
// {
// value = DateTime.Now.AddMinutes(-i).ToString("MM/dd/yyyy hh:mm tt");
// }
result.AppendFormat("<td>{0}</td>", value);
}
result.Append("</tr>");
}
result.Append("</tbody></table>");
return template.Replace("{{content}}", result.ToString());
}
}
}