将html导出到excel或word

本质是将html写成word或excel支持的html格式。

如何将html写成word或excel支持的格式?

只需打开计算机上任意一个word或excel文档,打开文件->另存为,选择文件类型为“*.htm,*.html”格式,即将一篇word转化为html格式文件。

在浏览器中打开该文件,查看源码,即可看到转化后的html文档。

只需参照此文档内容,将html写成该格式,然后将html内容写入到文件中,把文件后缀名改为“.xls”或“.doc”即可。

 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
/// <summary>
/// 导出到excel
/// </summary>
/// <returns></returns>
public static void exportExcel() {
string HEADER = "<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">" +
"<meta http-equiv=Content-Type content=\"text/html; charset=\"gb2312\">" +
"<head>" +
"<!--[if gte mso 9]><xml>" +
"<x:ExcelWorkbook>" +
"<x:ExcelWorksheets>" +
"<x:ExcelWorksheet>" +
"<x:Name>工作表标题</x:Name>" +
"<x:WorksheetOptions>" +
"<x:Print>" +
"<x:ValidPrinterInfo />" +
"</x:Print>" +
"</x:WorksheetOptions>" +
"</x:ExcelWorksheet>" +
"</x:ExcelWorksheets>" +
"</x:ExcelWorkbook>" +
"</xml>" +
"<![endif]-->" ; string STYLE="<style type=\"text/css\">" +
".spercent" +
" {" +
" background-color:#ffff99;" +
" mso-number-format:0.00%;" +
" }" +
".sId" +
" {" +
" background-color:#ff6633;" +
" mso-number-format:0;" +
" }" +
".sName" +
" {" +
" color:red;" +
" }" +
".sValue" +
" {" +
" color:blue;" +
" mso-number-format:0;" +
" }" +
"</style>"; using (StreamWriter writer = new StreamWriter(@"C:\Users\linghuam\Desktop\1111.xls", true, System.Text.Encoding.GetEncoding("gb2312"), ))
{
writer.WriteLine(HEADER);
writer.WriteLine(STYLE);
writer.WriteLine("</head><body><table border=\"1\" style=\"font-size:9pt\"><tr>");
writer.WriteLine("<th>ID</th>");
writer.WriteLine("<th>Name</th>");
writer.WriteLine("<th>Value</th>");
writer.WriteLine("<th>Percent</th>"); for (int row = ; row < ; row++)
{
writer.WriteLine("<tr>");
writer.WriteLine("<td class=\"sId\">{0}</td>", row);
writer.WriteLine("<td class=\"sName\">{0}</td>", Guid.NewGuid().ToString());
writer.WriteLine("<td class=\"sValue\">{0}</td>", new Random().Next());
writer.WriteLine("<td class=\"spercent\">{0}</td>", new Random().NextDouble());
writer.WriteLine("</tr>");
} writer.WriteLine("</table></body>");
}
}
/// <summary>
/// 导出到word
/// </summary>
/// <returns></returns> public static bool exportWord() {
string html = "<html xmlns:v=\"urn:schemas-microsoft-com:vml\"xmlns:o=\"urn:schemas-microsoft-com:office:office\"xmlns:w=\"urn:schemas-microsoft-com:office:word\"xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\"xmlns=\"http://www.w3.org/TR/REC-html40\"> <head> <![endif]><!--[if gte mso 9]><xml> <x:ExcelWorkbook> <x:ExcelWorksheets> <x:ExcelWorksheet> <x:Name>gis</x:Name> <x:WorksheetSource/> </x:ExcelWorksheet> </x:ExcelWorksheets> <x:ProtectStructure>False</x:ProtectStructure> <x:ProtectWindows>False</x:ProtectWindows> </x:ExcelWorkbook> </xml><![endif]--> <meta http-equiv=Content-Type content=\"text/html; charset=gb2312\"> <meta name=ProgId content=Word.Document> <meta name=Generator content=\"Microsoft Word 15\"> <meta name=Originator content=\"Microsoft Word 15\"> <style> div{background:rgba(255,0,0,0.5); } p{color: blue; font-size: 22px; font-weight: bolder; } h1{color: rgb(125,125,125); width: 100%; height: 100%; padding: 20px; background:rgb(66,89,224); } </style> </head> <body> <div> <p>I am chinese!</p> <h1>我发发非法*</h1> </div> </body> </html>";
using (StreamWriter writer = new StreamWriter(@"C:\Users\linghuam\Desktop\1111.doc", true, System.Text.Encoding.GetEncoding("gb2312"), ))
{
writer.WriteLine(html);
return true;
}
return false;
}
static void Main(string[] args)
{
exportExcel();
exportWord();
}
}
}
上一篇:Django去操作已经存在的数据库


下一篇:Python 源码剖析(二)【整数对象】