HTMLTable转换为EXcel
1.netcore3.0,.netFramework
2.nuget 下载office安装包![在这里插入图片描述](https://img-blog.csdnimg.cn/20200916113804736.png#pic_center
3.思路用Excel打开html,然后再另存为.xls文档
4.代码实现
public static string ReadHtmlToExcelFile(string readFileName, string saveFileName)
{
Excel.WorkbookClass wbclass;
Worksheet ws;
Excel.Application excelApp;
excelApp = new Microsoft.Office.Interop.Excel.Application();
object objOpt = System.Reflection.Missing.Value;
string path2 = Directory.GetCurrentDirectory();
log.Info($"读取文件路径{path2}/{readFileName}");
wbclass = (WorkbookClass)excelApp.Workbooks.Open(@$"{path2}/{readFileName}",
objOpt,objOpt, objOpt, objOpt, objOpt,
objOpt, objOpt, objOpt, objOpt, objOpt);//excel打开文件按
ws = (Worksheet)wbclass.Worksheets.get_Item(1);
//添加网格线
ws.UsedRange.Borders.Color = Color.Black;
wbclass.DefaultTableStyle = true;
var b = wbclass;
string path = $"{path2}\\{saveFileName }\\{DateTime.Now.ToString("yyyyMMddHHmmss")}.xls";
b.SaveAs(path,
Excel.XlFileFormat.xlWorkbookNormal, objOpt, objOpt, objOpt, objOpt, Excel.XlSaveAsAccessMode.xlNoChange, objOpt, objOpt, objOpt, objOpt);//另存为xls
excelApp.Workbooks.Close();
excelApp.Quit();
return path;
// }
}
生成结果有跨行跨列,但是没有色彩
5.调用
[HttpPost]
[Route("/ToExcel2")]
[ValidateModelState]
[SwaggerOperation("ToExce2lPost")]
[SwaggerResponse(statusCode: 200)]
public virtual IActionResult ToExcel2Post([FromBody] FileHtml html)
{
log.Warn($"ht");
string readPath = $"Temp\\{DateTime.Now.ToString("yyyyMMddHHmmss")}{html.Name}.html";
System.IO.File.WriteAllText(readPath, $"<html><head></head><body>{html.Html}</body></html>");//生成html文件
string path=FileUnitity2.ReadHtmlToExcelFile(readPath, @"Temp");
var fs= System.IO.File.ReadAllBytes(path);
System.IO.File.Delete(path);
System.IO.File.Delete(readPath);
return File(fs, "application/x-xls", $"{html.Name}.xls");
}
public class FileHtml
{
public string Name { set; get; }//生成文件的名称
public string Html { set; get; }//需要转成Excel的htmlTable代码
}
示例:
{
“name”: “Test”,
“html”: “
°<th class=“ant-table-cell haverColor” style=“text-align: center;”><div style=“white-space: nowrap;”>北緯
°<th class=“ant-table-cell haverColor” style=“text-align: center;”><div style=“white-space: nowrap;”>持續風
(km/h)<th class=“ant-table-cell haverColor” style=“text-align: center;”><div style=“white-space: nowrap;”>移速
(km/h)<th class=“ant-table-cell haverColor” style=“text-align: center;”><div style=“white-space: nowrap;”>移向
(km/h)<th class=“ant-table-cell haverColor” style=“text-align: center;”><div style=“white-space: nowrap;”>距澳
(km)<th class=“ant-table-cell noColor” style=“text-align: center;”><div style=“white-space: nowrap;”>東經
°<th class=“ant-table-cell noColor” style=“text-align: center;”><div style=“white-space: nowrap;”>北緯
°<th class=“ant-table-cell noColor” style=“text-align: center;”><div style=“white-space: nowrap;”>持續風
(km/h)<th class=“ant-table-cell noColor” style=“text-align: center;”><div style=“white-space: nowrap;”>移速
(km/h)<th class=“ant-table-cell noColor” style=“text-align: center;”><div style=“white-space: nowrap;”>移向
(km/h)<th class=“ant-table-cell noColor” style=“text-align: center;”><div style=“white-space: nowrap;”>距澳
(km)
}
缺点:表结构必须由Table里的标签完成,不能是CSS,生成表格样式比较简单