项目用到的一种导出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的一种方法的更多相关文章

  1. .NET导出Excel的四种方法及评测

    .NET导出Excel的四种方法及评测 导出Excel是.NET的常见需求,开源社区.市场上,都提供了不少各式各样的Excel操作相关包.本文,我将使用NPOI.EPPlus.OpenXML.Aspo ...

  2. [转帖].NET导出Excel的四种方法及评测

    .NET导出Excel的四种方法及评测 https://www.cnblogs.com/sdflysha/p/20190824-dotnet-excel-compare.html 导出Excel是.N ...

  3. Asp.net导出Excel乱码的解决方法

    通过跟踪Asp.net服务器代码,没有乱码,然而导出Excel到浏览器后,打开时出现乱码. 解决方法是添加编码格式的前缀字节码:Response.BinaryWrite(System.Text.Enc ...

  4. 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; ...

  5. HTML用JS导出Excel的五种方法

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. Asp.net导出Excel(HTML输出方法)

    主要思路: 实例化Gridview,将值绑定后输出...(用烂了的方法) 贴上核心代码: public static void ExportToExcel(DataTable dataList, st ...

  7. ASP.Net数据导出Excel的几种方法

    方法一 通过GridView(简评:方法比较简单,但是只适合生成格式简单的Excel,且无法保留VBA代码),页面无刷新 aspx.cs部分 代码如下: using System; using Sys ...

  8. asp.net 导出excel 中文乱码解决方法 (转)

    用我转载的上一篇文章 Asp.net中把DataTable或DataGrid导出为Excel 导出的文档,中文有乱码现象,其实要解决中文乱码很简单,设置一下字符集.如下: // 设置编码和附件格式 c ...

  9. C#导出EXCEL的几种方法

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.W ...

随机推荐

  1. 3-15 《元编程》第6章 3-16 hook method

    Code That Writes Code 6.1 Coding your way to the weekend 6.2 Kernel#eval, Binding#eval Binding: Obje ...

  2. Jersey 2.x 基于 Servlet 的服务器端应用

    下面的依赖通常应用到应用服务器上(servlet 容器),同时这个应用服务器上没有整合任何 JAX-RS 的实现. 因此,这个应用服务器需要包含有 JAX-RS API 和 Jersey 实现,同时部 ...

  3. H5表单基础知识(二)

    表单新增属性 <!--<input type="text" class="name" />--> <!-- placeholder ...

  4. CF1083B The Fair Nut and String

    题意 给出两个长度为n的01字符串S和T. 选出k个字典序在S和T之间的长度为n的01字符串,使得尽可能多的字符串满足其是所选字符串中至少一个串的前缀. 这是一道思路比较奇怪的类似计数dp的题. 首先 ...

  5. HDOJ1004

    #include<iostream> #include "cstring" using namespace std; int add(char s1[],char s2 ...

  6. 供应商API补充(详解EBS接口开发之供应商导入)(转)

    原文地址  供应商导入的API补充(详解EBS接口开发之供应商导入) --供应商 --创建 AP_VENDOR_PUB_PKG.Create_Vendor ( p_api_version IN NUM ...

  7. <meta http-equiv="X-UA-Compatible" content="IE=7" />

    <meta http-equiv="X-UA-Compatible" content="IE=7" />意思是将IE8用IE7进行渲染,使网页在IE ...

  8. 【LeetCode】Unique Binary Search Trees II 异构二叉查找树II

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4048209.html 原题: Given n, generate all struc ...

  9. Win32.com安装

    Win32.com安装     http://sourceforge.net/projects/pywin32/files/pywin32     

  10. bacula备份终端操作bconsole指令

    1.list命令列出各种备份状态信息   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 list Jobs     #列出所有备份记录状态 list jobid= ...