c# npoi html,有没有一种方法可以使用NPOI / C#将HTML表转换为xls / xlsx文件?

my main goal is to dynamically create a xls containing a html table on serverside in my .NET-Application. The Datamodel of my HTMLtable is pretty complex, though, but I managed to create the Model using dotliquid for another use case - the export to pdf-feature of my application. Here I use the EO.pdf-library from essential objects.

Now I hope there's no need to do some "double work", for at some point of my code, I have the complete HTML-Markup from which the PDF actually is created.

Code to generate my pdf:

#region plan

string legend = string.Empty;

string allg = string.Empty;

int lineCount = 0;

int dayCount = 0;

var plan = Utils.Pdf.ConvertToPdf.ConvertPlanTemplate(out allg, out legend, out lineCount, out dayCount);

if (RequestValues.Extension.ToLower() == "pdf")

{

...

var doc = new EO.Pdf.PdfDocument();

EO.Pdf.HtmlToPdf.ConvertHtml(plan, doc, options);

...

}

Here I Use EO's "ConvertHtml"-Method to convert my html (created using dotliquid) to my PdfDocument-Instance.

To make a long story short, I'm searching for a method like this in NPOI. Is there a way to convert a HTML table to xls / xlsx-file via NPOI (other than the "manual" way)?

Best Regards,

getoveritde

Talk1:

Any update on converting HTML to Excel?

Solutions1

Yes, there is a way to convert an html table to an xls/xlsx file using NPOI in C# and that method is discussed on StackOverflow in detail; you can find the solution for your problem in the link below:

Talk1:

Where exactly is it discussed in that link?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用C#中的NPOI库来将Excel数据转换为DataSource。下面是一个示例代码: ```csharp using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System.Data; public static class ExcelUtility { public static DataTable ExcelToDataTable(string filePath) { IWorkbook workbook = null; ISheet sheet = null; DataTable data = new DataTable(); using (var file = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) { if (filePath.EndsWith(".xls")) { workbook = new HSSFWorkbook(file); } else if (filePath.EndsWith(".xlsx")) { workbook = new XSSFWorkbook(file); } if (workbook != null) { sheet = workbook.GetSheetAt(0); if (sheet != null) { var firstRow = sheet.GetRow(0); int cellCount = firstRow.LastCellNum; for (int i = firstRow.FirstCellNum; i < cellCount; ++i) { var cell = firstRow.GetCell(i); if (cell != null) { string columnName = cell.ToString(); if (!string.IsNullOrEmpty(columnName)) { data.Columns.Add(columnName); } } } for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; ++i) { var row = sheet.GetRow(i); if (row != null) { bool emptyRow = true; DataRow dataRow = data.NewRow(); for (int j = row.FirstCellNum; j < cellCount; ++j) { if (row.GetCell(j) != null) { dataRow[j] = row.GetCell(j).ToString(); if (!string.IsNullOrEmpty(dataRow[j].ToString())) { emptyRow = false; } } } if (!emptyRow) { data.Rows.Add(dataRow); } } } } } } return data; } } ``` 这段代码会根据文件路径读取Excel文件,将第一个工作转换为一个DataTable对象,并返回该对象。你可以使用该DataTable对象作为DataSource来绑定数据控件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值