国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET導入EXCEL方法匯總

2019-11-17 01:40:48
字體:
來源:轉載
供稿:網友

asp.net導入Excel方法匯總

  1. 1、由dataset生成
  2. publicvoidCreateExcel(DataSetds,stringtypeid,stringFileName)
  3. {
  4. Httxml格式文件
  5. if(typeid=="1")
  6. {
  7. //取得數據表各列標題,各標題之間以/t分割,最后一個列標題后加回車符
  8. for(i=0;icolHeaders+=dt.Columns[i].Caption.ToString()+"/t";
  9. colHeaders+=dt.Columns[i].Caption.ToString()+"/n";
  10. //向HTTP輸出流中寫入取得的數據信息
  11. resp.Write(colHeaders);
  12. //逐行處理數據
  13. foreach(DataRowrowinmyRow)
  14. {
  15. //在當前行中,逐列獲得數據,數據之間以/t分割,結束時加回車符/n
  16. for(i=0;ils_item+=row[i].ToString()+"/t";
  17. ls_item+=row[i].ToString()+"/n";
  18. //當前行數據寫入HTTP輸出流,并且置空ls_item以便下行數據
  19. resp.Write(ls_item);
  20. ls_item="";
  21. }
  22. }
  23. else
  24. {
  25. if(typeid=="2")
  26. {
  27. //從DataSet中直接導出XML數據并且寫到HTTP輸出流中
  28. resp.Write(ds.GetXml());
  29. }
  30. }
  31. //寫緩沖區中的數據到HTTP頭文件中
  32. resp.End();
  33. }
  34. 2、由datagrid生成
  35. publicvoidToExcel(System.Web.UI.Controlctl)
  36. {
  37. HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
  38. HttpContext.Current.Response.Charset="UTF-8";
  39. HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.Default;
  40. HttpContext.Current.Response.ContentType="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.msexcel/
  41. msWord
  42. ctl.Page.EnableViewState=false;
  43. System.IO.StringWritertw=newSystem.IO.StringWriter();
  44. System.Web.UI.HtmlTextWriterhw=newSystem.Web.UI.HtmlTextWriter(tw);
  45. ctl.RenderControl(hw);
  46. HttpContext.Current.Response.Write(tw.ToString());
  47. HttpContext.Current.Response.End();
  48. }
  49. 用法:ToExcel(datagrid1);
  50. 3、這個用dataview
  51. publicvoidOutputExcel(DataViewdv,stringstr)
  52. {
  53. //
  54. //TODO:在此處添加構造函數邏輯
  55. //
  56. //dv為要輸出到Excel的數據,str為標題名稱
  57. GC.Collect();
  58. Applicationexcel;//=newApplication();
  59. introwIndex=4;
  60. intcolIndex=1;
  61. _WorkbookxBk;
  62. _WorksheetxSt;
  63. excel=newApplicationClass();
  64. xBk=excel.Workbooks.Add(true);
  65. xSt=(_Worksheet)xBk.ActiveSheet;
  66. //
  67. //取得標題
  68. //
  69. foreach(DataColumncolindv.Table.Columns)
  70. {
  71. colIndex++;
  72. excel.Cells[4,colIndex]=col.ColumnName;
  73. xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment=XlVAlign.xlVAlignCenter;//設置標題格
  74. 式為居中對齊
  75. }
  76. //
  77. //取得表格中的數據
  78. //
  79. foreach(DataRowViewrowindv)
  80. {
  81. rowIndex++;
  82. colIndex=1;
  83. foreach(DataColumncolindv.Table.Columns)
  84. {
  85. colIndex++;
  86. if(col.DataType==System.Type.GetType("System.DateTime"))
  87. {
  88. excel.Cells[rowIndex,colIndex]=(Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
  89. xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment=
  90. XlVAlign.xlVAlignCenter;//設置日期型的字段格式為居中對齊
  91. }
  92. else
  93. if(col.DataType==System.Type.GetType("System.String"))
  94. {
  95. excel.Cells[rowIndex,colIndex]="’"+row[col.ColumnName].ToString();
  96. xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment=
  97. XlVAlign.xlVAlignCenter;//設置字符型的字段格式為居中對齊
  98. }
  99. else
  100. {
  101. excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
  102. }
  103. }
  104. }
  105. //
  106. //加載一個合計行
  107. //
  108. introwSum=rowIndex+1;
  109. intcolSum=2;
  110. excel.Cells[rowSum,2]="合計";
  111. xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment=XlHAlign.xlHAlignCenter;
  112. //
  113. //設置選中的部分的顏色
  114. //
  115. xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
  116. xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex=19;//設置為淺黃色,共計有
  117. 56種
  118. //
  119. //取得整個報表的標題
  120. //
  121. excel.Cells[2,2]=str;
  122. //
  123. //設置整個報表的標題格式
  124. //
  125. xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold=true;
  126. xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size=22;
  127. //
  128. //設置報表表格為最適應寬度
  129. //
  130. xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
  131. xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
  132. //
  133. //設置整個報表的標題為跨列居中
  134. //
  135. xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
  136. xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment=XlHAlign.xlHAlignCenterAcrossSelection;
  137. //
  138. //繪制邊框
  139. //
  140. xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle=1;
  141. xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight=
  142. XlBorderWeight.xlThick;//設置左邊線加粗
  143. xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight=
  144. XlBorderWeight.xlThick;//設置上邊線加粗
  145. xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight=
  146. XlBorderWeight.xlThick;//設置右邊線加粗
  147. xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight=
  148. XlBorderWeight.xlThick;//設置下邊線加粗
  149. //
  150. //顯示效果
  151. //
  152. excel.Visible=true;
  153. //xSt.Export(Server.MapPath(".")
  154. +"file://"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportForma
  155. t.ssExportHTML/);
  156. xBk.SaveCopyAs(Server.MapPath(".")+"file://"+this.xlfile.Text+".xls/");
  157. ds=null;
  158. xBk.Close(false,null,null);
  159. excel.Quit();
  160. System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
  161. System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
  162. System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
  163. xBk=null;
  164. excel=null;
  165. xSt=null;
  166. GC.Collect();
  167. stringpath=Server.MapPath(this.xlfile.Text+".xls");
  168. System.IO.FileInfofile=newSystem.IO.FileInfo(path);
  169. Response.Clear();
  170. Response.Charset="GB2312";
  171. Response.ContentEncoding=System.Text.Encoding.UTF8;
  172. //添加頭信息,為"文件下載/另存為"對話框指定默認文件名
  173. Response.AddHeader("Content-Disposition","attachment;filename="+Server.UrlEncode(file.Name));
  174. //添加頭信息,指定文件大小,讓瀏覽器能夠顯示下載進度
  175. Response.AddHeader("Content-Length",file.Length.ToString());
  176. //指定返回的是一個不能被客戶端讀取的流,必須被下載
  177. Response.ContentType="application/ms-excel";
  178. //把文件流發送到客戶端
  179. Response.WriteFile(file.FullName);
  180. //停止頁面的執行
  181. Response.End();
  182. }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 钦州市| 六枝特区| 通化县| 尼木县| 罗平县| 双桥区| 颍上县| 清河县| 泗水县| 江津市| 大悟县| 德庆县| 平利县| 阿合奇县| 孟津县| 襄樊市| 克什克腾旗| 阿克陶县| 苍山县| 六枝特区| 临颍县| 牙克石市| 江都市| 阜新市| 汽车| 高雄市| 团风县| 镇巴县| 潜山县| 安顺市| 扎赉特旗| 扬州市| 偃师市| 咸宁市| 赣州市| 邹平县| 乌兰察布市| 福贡县| 固镇县| 通化县| 宁阳县|