将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 或者相应环境的机器上对 ...
随机推荐
- 在eclipse中对于java的操作
1. 新建project new project --> java project -->input the name of the project and choose the jre ...
- Guava API学习之Multimap
相信大家对Java中的Map类及其之类有大致的了解,Map类是以键值对的形式来存储元素(Key->Value),但是熟悉Map的人都知 道,Map中存储的Key是唯一的.什么意思呢?就是假如我们 ...
- javascript版QQ在线聊天挂件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Linux编程遇到的问题汇集(持续更新中)
1.源代码编译redis报告错误: undefined reference to `__sync_add_and_fetch_4' 最近项目组在实验Redis,源代码编译的时候,遇到了错误:undef ...
- WCF-NAT模式访问
由于项目需求,第一次用wcf做基于internet的访问.整理一下自己的探索,作为总结,也供自己以后回顾. 要求:(1)wcf服务需要部署在internet之中. (2)双工通信. 出现问题: 根据上 ...
- CCI_chapter 3 Stacks and Queues
3.1Describe how you could use a single array to implement three stacks for stack 1, we will use [0, ...
- Logstash 介绍
Logstash 介绍: Logstash 是一个开源的数据收集引擎具有实时管道能力, Logstash 可以动态的统一数据从不同的来源和使数据规范化到你选择的目的地. 当Logstash 起初驾驭创 ...
- 【转】Android将Activity打成jar包供第三方调用(解决资源文件不能打包的问题)
Android中引入第三方Jar包的方法(java.lang.NoClassDefFoundError解决办法) 鼠标右键项目,然后属性,然后java buildpath 然后order and ex ...
- Android AutoCompleteTextView和MultiAutoCompleteTextView使用
Android AutoCompleteTextView和MultiAutoCompleteTextView的功能类似于百度或者Google在搜索栏输入信息的时候,弹出的与输入信息接近的提示信息: 它 ...
- ops
consists several key projects separately stand-alone connected entities massive scalability massive ...