将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();
}
}
}
将html导出到excel或word的更多相关文章
- asp.net教程:GridView导出到Excel或Word文件
asp.net教程:GridView导出到Excel或Word文件</ br> 在项目中我们经常会遇到要求将一些数据导出成Excel或者Word表格的情况,比如中国移动(我是中国移动用户) ...
- ASP.NET-GridView之导出excel或word
在CS阶段我们涉及到表格的导出,再Web开发同样可以实现,而且实现形式多种多样.以下面的例子说明表格导出到excel和word 这里用到了一个后台方法输出流形成***文件的的公共方法 DEMO < ...
- HTML页面的导出,包括Excel和Word导出
//导出到Excel --- 全部导出,可以设置一些隐藏进行导出 protected void btnExport_Click(object sender, EventArgs e) { ...
- 无插件,无com组件,利用EXCEL、WORD模板做数据导出(一)
本次随笔主要讲述着工作中是如何解决数据导出的,对于数据导出到excel在日常工作中大家还是比较常用的,那导出到word呢,改如何处理呢,简单的页面导出问题应该不大,但是如果是标准的公文导出呢,要保证其 ...
- RDLC - 后台代码直接导出Excel/PDF/Word格式
最近做报表功能,用到了.net的报表组件rdlc. 其中有个功能就是后台代码直接输出Excel/PDF/Word格式的文件,网上看了些资源,做个总结: 参考地址 我直接贴出代码: //自动导出exce ...
- EasyOffice-.NetCore一行代码导入导出Excel,生成Word
简介 Excel和Word操作在开发过程中经常需要使用,这类工作不涉及到核心业务,但又往往不可缺少.以往的开发方式在业务代码中直接引入NPOI.Aspose或者其他第三方库,工作繁琐,耗时多,扩展性差 ...
- js将HTML中table导出到EXCEL word (只支持IE) 另用php 配合AJAX可以支持所有浏览器
转载请注明来源:https://www.cnblogs.com/hookjc/ <HTML> <HEAD> <title>WEB页面导出为EXC ...
- asp.net 将word文档进行编辑并导出一个新的word
最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中 ...
- 使用NPOI读写Excel、Word
NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...
随机推荐
- ios app开发步骤
虽然开发一个app的任务看上去可能很艰巨,但是整个过程可以抽象成几个相对简单的步骤,下面这些步骤会在你开发第一个app时帮你步入正途. 定义Concept 每个好app都是从一个concept开始. ...
- 武汉科技大学ACM :1001: A + B Problem
Problem Description Calculate A + B. Input Each line will contain two integers A and B. Process to e ...
- c++回调实现
回调是A将一个函数指针传给B,然后调用B,B在执行自身函数后,再在合适的时候执行A的这个函数指针,这样就能避免A和B的相互包含和链接,在大型项目中回调是一种打破循环依赖的常用技术. typedef v ...
- uva 296 - Safebreaker
枚举法 #include <cstdio> using namespace std; int main() { int t, n, i, j, k; scanf("%d" ...
- DataTable 对象 转换为Json 字符串
/// <summary> /// DataTable 对象 转换为Json 字符串 /// </summary> /// <param name="dt&qu ...
- quartz spring
简单例子可参考 http://yangpanwww.iteye.com/blog/797563 http://liuzidong.iteye.com/blog/1118992 关于时间配置可参考另一篇 ...
- F#(1)
如果你也会C#,那不妨了解下F#(1):F# 数据类型 简单介绍 F#(与C#一样,念作“F Sharp”)是一种基于.Net框架的强类型.静态类型的函数式编程语言.可以说C#是一门包含函数式编程 ...
- NGINX配置小随笔
达到以下效果: 1,特定目录被指定IP访问 2,不是指定的IP地址不能执行URI中特定字符串 3,特定目录中不能执行PHP文件 set $self_visit ''; if ( $request_ur ...
- 面试题 46 1+ 2+3+...+n
class Temp{ public: Temp(){ ++N; sum+=N; } static void Reset(){ N = ; sum = ; } static int getSum(){ ...
- 勾选checkbox之后,button按钮可用
js:function chkClick() { if (document.getElementById("chkBox").checked == true) { ...