asp.net 导出excel的一种方法
项目用到的一种导出excel 的方法予以记录:(具体的业务类可更具情况替换使用)
protected void Export(string filename, List<ComponentConmentDetailHelp> list)
{
string[][] cols = new string[][] {
new string[] { "Id", "主键" },
new string[] { "UserCode","员工卡号"},
new string[] { "UserName","姓名"},
new string[] { "Comment", "评论内容"},
new string[] { "AuthdorName", "作者"},
new string[] { "FileName", "文件名"},
new string[] { "Ts","时间"}
};
StringBuilder builder = new StringBuilder();
builder.Append("<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
builder.Append("xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
builder.Append("xmlns=\"http://www.w3.org/TR/REC-html40\"");
builder.Append("<head>");
builder.Append("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
builder.Append("<style>");
builder.Append(".xl26");
builder.Append("{mso-style-parent:style0;");
builder.Append("font-family:\"Times New Roman\", serif;");
builder.Append("mso-font-charset:0;");
builder.Append("mso-number-format:\"@\";}");
builder.Append("</style>");
builder.Append("<xml>");
builder.Append("<x:ExcelWorkbook>");
builder.Append("<x:ExcelWorksheets>");
builder.Append("<x:ExcelWorksheet>");
builder.Append("<x:Name>Sheet1</x:Name>");
builder.Append("<x:WorksheetOptions>");
builder.Append("<x:DefaultRowHeight>285</x:DefaultRowHeight>");
builder.Append("<x:Selected/>");
builder.Append("<x:Panes>");
builder.Append("<x:Pane>");
builder.Append("<x:Number>3</x:Number>");
builder.Append("<x:ActiveCol>1</x:ActiveCol>");
builder.Append("</x:Pane>");
builder.Append("</x:Panes>");
builder.Append("<x:ProtectContents>False</x:ProtectContents>");
builder.Append("<x:ProtectObjects>False</x:ProtectObjects>");
builder.Append("<x:ProtectScenarios>False</x:ProtectScenarios>");
builder.Append("</x:WorksheetOptions>");
builder.Append("</x:ExcelWorksheet>");
builder.Append("<x:WindowHeight>6750</x:WindowHeight>");
builder.Append("<x:WindowWidth>10620</x:WindowWidth>");
builder.Append("<x:WindowTopX>480</x:WindowTopX>");
builder.Append("<x:WindowTopY>75</x:WindowTopY>");
builder.Append("<x:ProtectStructure>False</x:ProtectStructure>");
builder.Append("<x:ProtectWindows>False</x:ProtectWindows>");
builder.Append("</x:ExcelWorkbook>");
builder.Append("</xml>");
builder.Append("</head>");
builder.Append("<body>");
builder.Append("<table align=\"center\"style='border-collapse:collapse;table-layout:fixed'>");
builder.Append("<tr>");
System.IO.StringWriter writer = new System.IO.StringWriter();
foreach (string[] col in cols)
{
builder.Append("<td><b>" + col[1] + "</b></td>");
}
builder.Append("</tr>");
foreach (ComponentConmentDetailHelp item in list)
{
Type type = item.GetType();//获取类型
builder.Append("<tr>");
foreach (string[] col in cols)
{
System.Reflection.PropertyInfo propertyInfo = type.GetProperty(col[0]);
string val = Convert.ToString(propertyInfo.GetValue(item, null));
builder.Append("<td class='xl26'>" + val + "</td>");
}
builder.Append("</tr>");
}
builder.Append("</body>");
builder.Append("</html>");
WebUtility.ExportToFile(filename + ".xls", builder.ToString(), "application/ms-excel");
}
asp.net 导出excel的一种方法的更多相关文章
- .NET导出Excel的四种方法及评测
.NET导出Excel的四种方法及评测 导出Excel是.NET的常见需求,开源社区.市场上,都提供了不少各式各样的Excel操作相关包.本文,我将使用NPOI.EPPlus.OpenXML.Aspo ...
- [转帖].NET导出Excel的四种方法及评测
.NET导出Excel的四种方法及评测 https://www.cnblogs.com/sdflysha/p/20190824-dotnet-excel-compare.html 导出Excel是.N ...
- Asp.net导出Excel乱码的解决方法
通过跟踪Asp.net服务器代码,没有乱码,然而导出Excel到浏览器后,打开时出现乱码. 解决方法是添加编码格式的前缀字节码:Response.BinaryWrite(System.Text.Enc ...
- html table表格导出excel的方法 html5 table导出Excel HTML用JS导出Excel的五种方法 html中table导出Excel 前端开发 将table内容导出到excel HTML table导出到Excel中的解决办法 js实现table导出Excel,保留table样式
先上代码 <script type="text/javascript" language="javascript"> var idTmr; ...
- HTML用JS导出Excel的五种方法
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- Asp.net导出Excel(HTML输出方法)
主要思路: 实例化Gridview,将值绑定后输出...(用烂了的方法) 贴上核心代码: public static void ExportToExcel(DataTable dataList, st ...
- ASP.Net数据导出Excel的几种方法
方法一 通过GridView(简评:方法比较简单,但是只适合生成格式简单的Excel,且无法保留VBA代码),页面无刷新 aspx.cs部分 代码如下: using System; using Sys ...
- asp.net 导出excel 中文乱码解决方法 (转)
用我转载的上一篇文章 Asp.net中把DataTable或DataGrid导出为Excel 导出的文档,中文有乱码现象,其实要解决中文乱码很简单,设置一下字符集.如下: // 设置编码和附件格式 c ...
- C#导出EXCEL的几种方法
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.W ...
随机推荐
- python-day63--前端
一. 响应式介绍 - 响应式布局是什么? 同一个网页在不同的终端上呈现不同的布局等 - 响应式怎么实现的? 1. CSS3 media query 媒体查询 2. JS去控制网页的布局和样式等 - 缺 ...
- python-day39--mysql基本操作
1.修改密码: mysqladmin -uroot password 123 2.忘记密码如何修改密码: 1.干掉data目录---> 重新初始化 (不推荐,所有授权信息全部丢失!!!) 2 ...
- zzuli1731 矩阵(容斥)
1731: 矩阵 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 600 Solved: 106 SubmitStatusWeb Board Descr ...
- python里实现DSL
以后用到的话可以参考如下链接: http://safehammad.com/downloads/domain-specific-languages-and-python-2011-04-21.pdf ...
- JavaScript学习总结(十一)——Object类详解
一.Object类介绍 Object类是所有JavaScript类的基类(父类),提供了一种创建自定义对象的简单方式,不再需要程序员定义构造函数. 二.Object类主要属性 1.constructo ...
- KMP 求next数组
一直没理解.看这个倒是看懂了.但是博主代码好像有点问题吖.测试并不正确.思想还是没错的. 转载自:http://www.tuicool.com/articles/yayeIbe
- learning docker steps(5) ----- docker stack 初次体验
参考:https://docs.docker.com/get-started/part5/ stack 技术栈.技术栈是一组相关的服务,它们共享依赖项并且可以一起进行编排和扩展.单个技术栈能够定义和协 ...
- bzoj1091
题解: 暴力枚举顺序 然后计算几何 代码: #include<bits/stdc++.h> ],lp=; double v1,v2,ans=1e10; struct pos { doubl ...
- Jboss7 部署EJB3 简明教程
什么是EJB? EJB 是 Java 企业Bean, 是JavaEE服务端 企业组件模型,它的设计目标与核心应用是部署分布式应用程序.话不多说,直接看如何在本机部署EJB3. 部署环境: 操作系统 ...
- JSON 数组的创建方式
procedure TFormDZMD.Button1Click(Sender: TObject); var ja: ISuperObject; begin ja := SA([]); ja.AsAr ...