地铁二期项目正式开始

ExcelWorkbook.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #region 版权所有 杨立伟 Benny
  2. //************************************
  3. // Created Time 2008-07-12
  4. // Created By Benny
  5. //************************************
  6. #endregion
  7. using System;
  8. using System.Collections;
  9. using System.Web;
  10. using System.Data;
  11. using System.Text;
  12. namespace YTSoft.Common.Office
  13. {
  14. /// <summary>
  15. /// Excel工作本
  16. /// </summary>
  17. public class ExcelWorkbook
  18. {
  19. private string filename=Guid.NewGuid().ToString().Replace("-","");
  20. /// <summary>
  21. /// 导出EXCEL文件名
  22. /// </summary>
  23. public string FileName
  24. {
  25. set { filename = HttpUtility.UrlEncode(value, System.Text.Encoding.UTF8); }
  26. }
  27. ExcelStyleCollection styles = new ExcelStyleCollection();
  28. /// <summary>
  29. /// 样式集合
  30. /// </summary>
  31. public ExcelStyleCollection Styles
  32. {
  33. set { styles = value; }
  34. }
  35. WorksheetCollection worksheets = new WorksheetCollection();
  36. /// <summary>
  37. /// 表单集合
  38. /// </summary>
  39. public WorksheetCollection WorkSheets
  40. {
  41. get { return worksheets; }
  42. set { worksheets = value; }
  43. }
  44. public void OutPut()
  45. {
  46. HttpResponse Response = HttpContext.Current.Response;
  47. Response.Clear();
  48. Response.Buffer = true;
  49. Response.Charset = "GB2312";
  50. Response.AppendHeader("Content-Disposition", "attachment;filename="+filename+".xls");
  51. Response.ContentEncoding = Encoding.GetEncoding("GB2312");
  52. Response.ContentType = "application/ms-excel";
  53. FormatExcel(Response);
  54. Response.End();
  55. }
  56. private void FormatExcel(HttpResponse Response)
  57. {
  58. WriteBegininfo(Response);
  59. WriteDocumentProperties(Response);
  60. WriteOfficeDocumentSettings(Response);
  61. WriteExcelWorkbook(Response);
  62. WriteStyles(Response, styles);
  63. WriteWorkSheets(Response, worksheets);
  64. Response.Write("</Workbook>");
  65. }
  66. private void WriteWorkSheets(HttpResponse Response, WorksheetCollection worksheets)
  67. {
  68. foreach (Worksheet ws in worksheets)
  69. {
  70. ws.OutPut();
  71. WriteWorksheetOptions(Response);
  72. Response.Write("</Worksheet>");
  73. }
  74. }
  75. private void WriteWorksheetOptions(HttpResponse Response)
  76. {
  77. Response.Write("<WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
  78. Response.Write("<PageSetup><Header x:Margin=\"0.3\"/><Footer x:Margin=\"0.3\"/><PageMargins x:Bottom=\"0.75\" x:Left=\"0.7\" x:Right=\"0.7\" x:Top=\"0.75\"/></PageSetup>");
  79. Response.Write("<Print><ValidPrinterInfo/><PaperSizeIndex>9</PaperSizeIndex><HorizontalResolution>200</HorizontalResolution><VerticalResolution>200</VerticalResolution></Print>");
  80. Response.Write("<Selected/>");
  81. Response.Write("<Panes><Pane><Number>3</Number><ActiveRow>2</ActiveRow><ActiveCol>2</ActiveCol></Pane></Panes>");
  82. Response.Write("<ProtectObjects>False</ProtectObjects>");
  83. Response.Write(" <ProtectScenarios>False</ProtectScenarios>");
  84. Response.Write("</WorksheetOptions>");
  85. }
  86. private void WriteStyles(HttpResponse Response, ExcelStyleCollection styles)
  87. {
  88. Response.Write("<Styles>");
  89. foreach (ExcelStyle s in styles)
  90. {
  91. Response.Write(s.StyleString.ToString());
  92. }
  93. Response.Write("</Styles>");
  94. }
  95. private void WriteExcelWorkbook(HttpResponse Response)
  96. {
  97. Response.Write("<ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel \">");
  98. Response.Write("<WindowHeight>11640</WindowHeight>");
  99. Response.Write("<WindowWidth>15480</WindowWidth>");
  100. Response.Write("<WindowTopX>0</WindowTopX>");
  101. Response.Write("<WindowTopY>90</WindowTopY>");
  102. Response.Write("<ProtectStructure>False</ProtectStructure>");
  103. Response.Write("<ProtectWindows>False</ProtectWindows>");
  104. Response.Write("</ExcelWorkbook>");
  105. }
  106. private void WriteOfficeDocumentSettings(HttpResponse Response)
  107. {
  108. Response.Write(" <OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\">");
  109. Response.Write(" <RemovePersonalInformation/>");
  110. Response.Write(" </OfficeDocumentSettings>");
  111. }
  112. private void WriteDocumentProperties(HttpResponse Response)
  113. {
  114. Response.Write("<DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
  115. Response.Write("<Created>"+DateTime.Now.ToString()+"</Created>");
  116. Response.Write("<LastSaved>"+DateTime.Now.ToString()+"</LastSaved>");
  117. Response.Write("<Version>12.00</Version>");
  118. Response.Write("</DocumentProperties>");
  119. }
  120. private void WriteBegininfo(HttpResponse Response)
  121. {
  122. #region
  123. Response.Write("<?xml version=\"1.0\" encoding=\"GB2312\" ?>");
  124. Response.Write("<?mso-application progid=\"Excel.Sheet\"?>");
  125. Response.Write("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
  126. #endregion
  127. }
  128. }
  129. }