Няма описание

DataToExcel.cs 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Diagnostics;
  3. using System.Collections;
  4. using System.Data;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Text;
  9. namespace XYFDRQ.Common
  10. {
  11. /// <summary>
  12. /// 操作EXCEL导出数据报表的类
  13. /// Copyright (C) XYFDRQ
  14. /// </summary>
  15. public class DataToExcel
  16. {
  17. public DataToExcel()
  18. {
  19. }
  20. /// <summary>
  21. /// DataTable保存到Excel
  22. /// </summary>
  23. public static void SaveToExcelGB2312(DataTable p_Table, HttpResponse p_Response, string fileName)
  24. {
  25. int _CountR = p_Table.Rows.Count;//行数
  26. int _CountC = p_Table.Columns.Count;//列数
  27. p_Response.Clear();
  28. p_Response.Buffer = true;
  29. //设置编码
  30. p_Response.Charset = "GB2312";
  31. p_Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
  32. p_Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString());
  33. p_Response.ContentType = "application/ms-excel";
  34. //写表头
  35. for (int i = 0; i < _CountC; i++)
  36. {
  37. p_Response.Write(p_Table.Columns[i].ColumnName + "\t");
  38. }
  39. p_Response.Write("\n");
  40. //写表内容
  41. for (int RowNo = 0; RowNo <= _CountR - 1; RowNo++)
  42. {
  43. string RowContent = "";
  44. for (int CloumnNo = 0; CloumnNo <= _CountC - 1; CloumnNo++)
  45. {
  46. RowContent += Convert.ToString(p_Table.Rows[RowNo][CloumnNo]).Replace("\t", "").Replace("\r", "").Replace("\n", "").Replace("\"", "") + "\t";
  47. }
  48. RowContent += "\n";
  49. p_Response.Write(RowContent);
  50. }
  51. p_Response.End();
  52. }
  53. /// <summary>
  54. /// DataTable保存到Excel
  55. /// </summary>
  56. /// <param name="p_Table"></param>
  57. /// <param name="p_Response"></param>
  58. public static void SaveToExcelUtf8(DataTable p_Table, HttpResponse p_Response, string fileName)
  59. {
  60. int _CountR = p_Table.Rows.Count;//行数
  61. int _CountC = p_Table.Columns.Count;//列数
  62. p_Response.Clear();
  63. p_Response.Buffer = true;
  64. p_Response.Charset = "UTF-8";
  65. p_Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
  66. p_Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlDecode(fileName, Encoding.UTF8).ToString());
  67. p_Response.ContentType = "application/ms-excel";
  68. //写表头
  69. for (int i = 0; i < _CountC; i++)
  70. {
  71. p_Response.Write(p_Table.Columns[i].ColumnName + "\t");
  72. }
  73. p_Response.Write("\n");
  74. //写表内容
  75. for (int RowNo = 0; RowNo <= _CountR - 1; RowNo++)
  76. {
  77. string RowContent = "";
  78. for (int CloumnNo = 0; CloumnNo <= _CountC - 1; CloumnNo++)
  79. {
  80. RowContent += Convert.ToString(p_Table.Rows[RowNo][CloumnNo]) + "\t";
  81. }
  82. RowContent += "\n";
  83. p_Response.Write(RowContent);
  84. }
  85. p_Response.End();
  86. }
  87. }
  88. }