C#导出EXCEL没有网格线的解决方法
2014-02-26 13:04
387 查看
今天在做项目时,通过流导出数据到Excel却不显示网格线,真是郁闷。上网查了好久才得一良方(注意<XML>标签中的代码):
DataTable thisTable = DBHelper.GetDataTable("select * from table");
string sheetName = "sheetName";
string fileName = "fileName";
if (thisTable != null)
{
StringWriter sw = new StringWriter();
sw.WriteLine("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine("<head>");
sw.WriteLine("<!--[if gte mso 9]>");
sw.WriteLine("<xml>");
sw.WriteLine(" <x:ExcelWorkbook>");
sw.WriteLine(" <x:ExcelWorksheets>");
sw.WriteLine(" <x:ExcelWorksheet>");
sw.WriteLine(" <x:Name>" + sheetName + "</x:Name>");
sw.WriteLine(" <x:WorksheetOptions>");
sw.WriteLine(" <x:Print>");
sw.WriteLine(" <x:ValidPrinterInfo />");
sw.WriteLine(" </x:Print>");
sw.WriteLine(" </x:WorksheetOptions>");
sw.WriteLine(" </x:ExcelWorksheet>");
sw.WriteLine(" </x:ExcelWorksheets>");
sw.WriteLine("</x:ExcelWorkbook>");
sw.WriteLine("</xml>");
sw.WriteLine("<![endif]-->");
sw.WriteLine("</head>");
sw.WriteLine("<body>");
sw.WriteLine("<table>");
sw.WriteLine(" <tr>");
sw.WriteLine(" <td><strong>列名0</strong></td>");
sw.WriteLine(" <td>列名1</td>");
sw.WriteLine(" <td>列名2</td>");
sw.WriteLine(" <td>列名3</td>");
sw.WriteLine(" <td>列名4</td>");
sw.WriteLine(" <td>列名5</td>");
sw.WriteLine(" </tr>");
foreach (DataRow dr in thisTable.Rows)
{
sw.WriteLine(" <tr>");
sw.WriteLine(" <td>" + dr["C0"] + "</td>");
sw.WriteLine(" <td>" + dr["C1"] + "</td>");
sw.WriteLine(" <td>" + dr["C2"] + "</td>");
sw.WriteLine(" <td>" + dr["C3"] + "</td>");
sw.WriteLine(" <td>" + dr["C4"] + "</td>");
sw.WriteLine(" <td>" + dr["C5"] + "</td>");
sw.WriteLine(" </tr>");
}
sw.WriteLine("</table>");
sw.WriteLine("</body>");
sw.WriteLine("</html>");
sw.Close();
Response.Clear();
Response.Buffer = true;
Response.Charset = "UTF-8";
this.EnableViewState = false;
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName +".xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();
}
DataTable thisTable = DBHelper.GetDataTable("select * from table");
string sheetName = "sheetName";
string fileName = "fileName";
if (thisTable != null)
{
StringWriter sw = new StringWriter();
sw.WriteLine("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine("<head>");
sw.WriteLine("<!--[if gte mso 9]>");
sw.WriteLine("<xml>");
sw.WriteLine(" <x:ExcelWorkbook>");
sw.WriteLine(" <x:ExcelWorksheets>");
sw.WriteLine(" <x:ExcelWorksheet>");
sw.WriteLine(" <x:Name>" + sheetName + "</x:Name>");
sw.WriteLine(" <x:WorksheetOptions>");
sw.WriteLine(" <x:Print>");
sw.WriteLine(" <x:ValidPrinterInfo />");
sw.WriteLine(" </x:Print>");
sw.WriteLine(" </x:WorksheetOptions>");
sw.WriteLine(" </x:ExcelWorksheet>");
sw.WriteLine(" </x:ExcelWorksheets>");
sw.WriteLine("</x:ExcelWorkbook>");
sw.WriteLine("</xml>");
sw.WriteLine("<![endif]-->");
sw.WriteLine("</head>");
sw.WriteLine("<body>");
sw.WriteLine("<table>");
sw.WriteLine(" <tr>");
sw.WriteLine(" <td><strong>列名0</strong></td>");
sw.WriteLine(" <td>列名1</td>");
sw.WriteLine(" <td>列名2</td>");
sw.WriteLine(" <td>列名3</td>");
sw.WriteLine(" <td>列名4</td>");
sw.WriteLine(" <td>列名5</td>");
sw.WriteLine(" </tr>");
foreach (DataRow dr in thisTable.Rows)
{
sw.WriteLine(" <tr>");
sw.WriteLine(" <td>" + dr["C0"] + "</td>");
sw.WriteLine(" <td>" + dr["C1"] + "</td>");
sw.WriteLine(" <td>" + dr["C2"] + "</td>");
sw.WriteLine(" <td>" + dr["C3"] + "</td>");
sw.WriteLine(" <td>" + dr["C4"] + "</td>");
sw.WriteLine(" <td>" + dr["C5"] + "</td>");
sw.WriteLine(" </tr>");
}
sw.WriteLine("</table>");
sw.WriteLine("</body>");
sw.WriteLine("</html>");
sw.Close();
Response.Clear();
Response.Buffer = true;
Response.Charset = "UTF-8";
this.EnableViewState = false;
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName +".xls");
Response.ContentType = "application/ms-excel";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.Write(sw);
Response.End();
}
相关文章推荐
- C#导出EXCEL没有网格线的解决方法
- C#导出EXCEL没有网格线的解决方法介绍
- 解决C#导出excel异常来自 HRESULT:0x800A03EC的方法 .
- c#中转出Excel时碰到Excel的COM组件没有注册的问题解决方法
- easyui messager.prompt没有输入默认值的解决办法=在JS中导出EXCEL的方法
- 解决C#导出excel异常来自 HRESULT:0x800A03EC的方法
- C# GridView导出excel,字段值前边带0的,导出后不显示0的解决方法
- C#导出数据到CSV和EXCEL文件,过长的数值被转义的解决方法
- c# 导出excel 数字太长而显示为科学计数法的解决方法
- [ASP.NET-C#]将DataSet、DataTable中的数据导出为Excel的解决方法
- C#导出数据到CSV和EXCEL文件时数字文本被转义的解决方法
- C#导出数据到CSV和EXCEL文件时,过长的数值被转义的解决方法
- C#导出Excel,某单元格内容长度超过255 的解决方法
- C#导出数据到CSV和EXCEL文件时,过长的数值被转义的解决方法
- 解决C#导出excel异常来自 HRESULT:0x800A03EC的方法 .
- C# 使用oledb导出excel,字段内容超长的解决方法
- 详讲:C#快速导出多个sheet到excel的两种方法(Cell和Range方法), 解决了(导入时外部表不是预期的格式)
- C#导出数据到CSV和EXCEL文件,过长的数值被转义的解决方法
- 解决C#导出excel异常来自 HRESULT:0x800A03EC的方法 .
- C# 导出excel单个单元格内容超出当前范围解决方法