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 ...
随机推荐
- 请求和响应:类ActionController::Base ; 类ActionDispatch::Request
扩展:ActionController::Base < Metal 2个基本主题: Get and Show do and redirect Requests 每个请求,由router决定了co ...
- dp练习(1)——马走日字
3328: 马走日字 时间限制: 1 Sec 内存限制: 128 MB提交: 35 解决: 5[提交][状态][讨论版] 题目描述 一次外出旅游,你路上遇到了一个骑着马的强盗,你很害怕,你需要找一 ...
- PHP header函数设置http报文头示例详解
//定义编码 header( 'Content-Type:text/html;charset=utf-8 '); //Atom header('Content-type: application/at ...
- Java NIO理解与使用
https://blog.csdn.net/qq_18860653/article/details/53406723 Netty的使用或许我们看着官网user guide还是很容易入门的.因为java ...
- Windows 7 英文版操作系统中文软件乱码解决方法
http://blog.csdn.net/lqhbupt/article/details/18863243
- jQuery一句话实现多选框全选/取消
<!DOCTYPE Html> <html> <head> <script type="text/javascript" src=&quo ...
- Alphabet Cookies
Alphabet Cookies 题目描述 Kitty likes cookies very much, and especially the alphabet cookies. Now, she g ...
- DevExpress v18.1新版亮点——WinForms篇(六)
用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WinForms v18.1 的新功能,快来下载试用新版本! ...
- mybatis之sql执行有数据但返回结果为null
最近在使用mybatis查询数据库时,发现在pl/sql中单独执行sql时结果是有值的,但是在程序中拿到的却是null,相关配置如下: (1) <resultMap type="mon ...
- get_class
<?phpclass foo { function foo() { // implements some logic } function name() { ...