# HTMLTable转换为Excel

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”: “

<col style=“width: 176px; min-width: 176px;”><col style=“width: 106px; min-width: 106px;”><col style=“width: 106px; min-width: 106px;”><col style=“width: 88px; min-width: 88px;”><col style=“width: 88px; min-width: 88px;”><col style=“width: 88px; min-width: 88px;”><col style=“width: 88px; min-width: 88px;”><col style=“width: 106px; min-width: 106px;”><col style=“width: 106px; min-width: 106px;”><col style=“width: 87px; min-width: 87px;”><col style=“width: 87px; min-width: 87px;”><col style=“width: 87px; min-width: 87px;”><col style=“width: 87px; min-width: 87px;”><col style=“width: 17px; min-width: 17px;”><thead class=“ant-table-thead”><th rowspan=“2” class=“ant-table-cell ant-table-cell-fix-left ant-table-cell-fix-left-last” style=“text-align: center; position: sticky; left: 0px;”>時間<th colspan=“6” class=“ant-table-cell” style=“text-align: center;”>VMMC本局預報<th colspan=“6” class=“ant-table-cell” style=“text-align: center;”>实测<th class=“ant-table-cell ant-table-cell-scrollbar” rowspan=“2”><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;”>北緯
°<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)
<col style=“width: 100px; min-width: 100px;”><col style=“width: 60px; min-width: 60px;”><col style=“width: 60px; min-width: 60px;”><col style=“width: 50px; min-width: 50px;”><col style=“width: 50px; min-width: 50px;”><col style=“width: 50px; min-width: 50px;”><col style=“width: 50px; min-width: 50px;”><col style=“width: 60px; min-width: 60px;”><col style=“width: 60px; min-width: 60px;”><col style=“width: 50px; min-width: 50px;”><col style=“width: 50px; min-width: 50px;”><col style=“width: 50px; min-width: 50px;”><col style=“width: 50px; min-width: 50px;”><tbody class=“ant-table-tbody”><tr aria-hidden=“true” class=“ant-table-measure-row” style=“height: 0px;”><td style=“padding: 0px; border: 0px; height: 0px;”><td style=“padding: 0px; border: 0px; height: 0px;”><td style=“padding: 0px; border: 0px; height: 0px;”><td style=“padding: 0px; border: 0px; height: 0px;”><td style=“padding: 0px; border: 0px; height: 0px;”><td style=“padding: 0px; border: 0px; height: 0px;”><td style=“padding: 0px; border: 0px; height: 0px;”><td style=“padding: 0px; border: 0px; height: 0px;”><td style=“padding: 0px; border: 0px; height: 0px;”><td style=“padding: 0px; border: 0px; height: 0px;”><td style=“padding: 0px; border: 0px; height: 0px;”><td style=“padding: 0px; border: 0px; height: 0px;”><td style=“padding: 0px; border: 0px; height: 0px;”><tr data-row-key=“99” class=“ant-table-row ant-table-row-level-0”><td class=“ant-table-cell ant-table-cell-fix-left ant-table-cell-fix-left-last” style=“text-align: center; position: sticky; left: 0px;”>2020-05-16 23:00<td class=“ant-table-cell haverColor” style=“text-align: center;”><td class=“ant-table-cell haverColor” style=“text-align: center;”><td class=“ant-table-cell haverColor” style=“text-align: center;”><div class=“green”><td class=“ant-table-cell haverColor” style=“text-align: center;”><td class=“ant-table-cell haverColor” style=“text-align: center;”><td class=“ant-table-cell haverColor” style=“text-align: center;”><td class=“ant-table-cell noColor” style=“text-align: center;”>120.4<td class=“ant-table-cell noColor” style=“text-align: center;”>19.4<td class=“ant-table-cell noColor” style=“text-align: center;”><div class=“yellow”>65<td class=“ant-table-cell noColor” style=“text-align: center;”>11<td class=“ant-table-cell noColor” style=“text-align: center;”>20<td class=“ant-table-cell noColor” style=“text-align: center;”>

}
在这里插入图片描述

缺点:表结构必须由Table里的标签完成,不能是CSS,生成表格样式比较简单

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值