本质是将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的更多相关文章

  1. asp.net教程:GridView导出到Excel或Word文件

    asp.net教程:GridView导出到Excel或Word文件</ br> 在项目中我们经常会遇到要求将一些数据导出成Excel或者Word表格的情况,比如中国移动(我是中国移动用户) ...

  2. ASP.NET-GridView之导出excel或word

    在CS阶段我们涉及到表格的导出,再Web开发同样可以实现,而且实现形式多种多样.以下面的例子说明表格导出到excel和word 这里用到了一个后台方法输出流形成***文件的的公共方法 DEMO < ...

  3. HTML页面的导出,包括Excel和Word导出

    //导出到Excel --- 全部导出,可以设置一些隐藏进行导出 protected void btnExport_Click(object sender, EventArgs e)    {     ...

  4. 无插件,无com组件,利用EXCEL、WORD模板做数据导出(一)

    本次随笔主要讲述着工作中是如何解决数据导出的,对于数据导出到excel在日常工作中大家还是比较常用的,那导出到word呢,改如何处理呢,简单的页面导出问题应该不大,但是如果是标准的公文导出呢,要保证其 ...

  5. RDLC - 后台代码直接导出Excel/PDF/Word格式

    最近做报表功能,用到了.net的报表组件rdlc. 其中有个功能就是后台代码直接输出Excel/PDF/Word格式的文件,网上看了些资源,做个总结: 参考地址 我直接贴出代码: //自动导出exce ...

  6. EasyOffice-.NetCore一行代码导入导出Excel,生成Word

    简介 Excel和Word操作在开发过程中经常需要使用,这类工作不涉及到核心业务,但又往往不可缺少.以往的开发方式在业务代码中直接引入NPOI.Aspose或者其他第三方库,工作繁琐,耗时多,扩展性差 ...

  7. js将HTML中table导出到EXCEL word (只支持IE) 另用php 配合AJAX可以支持所有浏览器

    转载请注明来源:https://www.cnblogs.com/hookjc/ <HTML>     <HEAD>       <title>WEB页面导出为EXC ...

  8. asp.net 将word文档进行编辑并导出一个新的word

    最近做项目,需要多word文档进行编辑并导出一个新的word,在最初的word编辑中留下特定的字符串用来替换,然后在本地生成一个新的word文档,并且不修改服务器中的word文档,这样才能保证服务器中 ...

  9. 使用NPOI读写Excel、Word

    NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目. 使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...

随机推荐

  1. Polygon对象

    Polylgon对象是由一个或多个Ring对象的有序集合,它可以是由单个Ring 对象构成,也可以使用多个Ring组成.Polygon通常用来代表有面积的多边形矢量对象,如行政区,建筑物等.

  2. 读书笔记_Effective_C++_条款二十二:将成员变量声明为private

    1.格式统一 在调用的时候,不会去想有没有(),一律是有get(),或者set()之类的. 2.封装 能直接访问得越少,表明封装性越高, 封装性越高,我们的顾虑就少了, 例如:我们a.data*0.9 ...

  3. PHP 计算页面执行时间

    PHP 计算页面执行时间 < ?php class runtime { var $StartTime = 0; var $StopTime = 0; function get_microtime ...

  4. Visual Studio express

    之前一直没用过Visual Studio的express版本.在最近一段时间,使用VS2010和2012的破解版都有点问题.vs2010突然不能使用,需要重新安装,家里和单位两台电脑都如此.家里一台电 ...

  5. hdu 2106 decimal system

    #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> # ...

  6. Codeforces 566F Clique in the Divisibility Graph

    http://codeforces.com/problemset/problem/566/F 题目大意: 有n个点,点上有值a[i], 任意两点(i, j)有无向边相连当且仅当 (a[i] mod a ...

  7. wpf只运行一个实例

    原文:wpf只运行一个实例 在winform下,只运行一个实例只需这样就可以: 1. 首先要添加如下的namespace: using System.Threading; 2. 修改系统Main函数, ...

  8. 如何将android studio项目转换成eclipse

    更新:虽然本人坚守eclipse很久,但是现在我也不再推荐继续用eclispe了,自己的项目用还没什么问题,但是你如果想用github上的项目,用eclispe会越来越难.如果你仍然感兴趣,继续看下面 ...

  9. BZOJ1108: [POI2007]天然气管道Gaz

    1108: [POI2007]天然气管道Gaz Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 410  Solved: 211[Submit][Sta ...

  10. 【转】ubuntu12.04完美安装QQ2012、QQMusic、Foxmail等--wine

    原文网址:http://blog.csdn.net/hanmengaidudu/article/details/17616921 其实在这之前,试过无数次的wine模拟,没有一次成功的,也不能说是不成 ...