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 ...
随机推荐
- Android动画(Animations)
动画类型Android的animation由四种类型组成 XML中 alpha : 渐变透明度动画效果 scale :渐变尺寸伸缩动画效果 translate : 画面转换位置移动动画效果 ro ...
- AC自动机练习
多模板串匹配一般有两种方法 暴力kmp, 适用于模板串少的情形 直接trie上暴力, 适用于模板串比较短的情形, 并且可以动态插入合并 建立AC自动机, 复杂度是严格线性的, 但不能动态插入 cons ...
- python-day65-django基础
一.MTV模型 1.官方说明: Django的MTV分别代表: Model(模型):负责业务对象与数据库的对象(ORM) Template(模版):负责如何把页面展示给用户 View(视图):负责业务 ...
- python中的注意事项
.python 中的 and 从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值: or 也是从左到有计算表达式,返回第一个为真的值: 其中数字 0 是假,其他都是真: 字 ...
- hdu-3980-nim博弈/sg函数
Paint Chain Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- Oracle to_char()和to_date()函数的用法
to_char()函数是我们经常使用的函数,下面就为您详细介绍Oracle to_date()函数的用法 1.to_char()函数分析 1)SQL中不区分大小写,MM和mm被认为是相同的格式代码 先 ...
- iOS UI-(多)视图控制器的生命周期、加载方法和模态视图方法以及屌丝方法
#import "ViewController.h" #import "SecondViewController.h" @interface ViewContr ...
- spark RDD底层原理
RDD底层实现原理 RDD是一个分布式数据集,顾名思义,其数据应该分部存储于多台机器上.事实上,每个RDD的数据都以Block的形式存储于多台机器上,下图是Spark的RDD存储架构图,其中每个Exe ...
- VGA
VGA(Video Graphics Array )视频图形阵列,是IBM在1987年随PS/2机一起推出的一种视频传输标准,具有分辨率高.显示速率快.颜色丰富等优点,在彩色显示器领域得到了广泛的应用 ...
- DevExpress v17.2新版亮点—WPF篇(五)
用户界面套包DevExpress v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WPF v17.2 新的Hamburger Menu.Sched ...