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");
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)