项目用到的一种导出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");}