asp.net里导出excel表方法汇总

 
  1. public void CreateExcel(DataSet ds,string typeid,string FileName)   
  2. {   
  3. HttpResponse resp;   
  4. resp = Page.Response;   
  5. resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");   
  6. resp.AppendHeader("Content-Disposition""attachment;filename=" + FileName);   
  7. string colHeaders= "", ls_item="";   
  8. int i=0;   
  9.   
  10. //定义表对象与行对像,同时用DataSet对其值进行初始化    
  11. DataTable dt=ds.Tables[0];   
  12. DataRow[] myRow=dt.Select("");   
  13. // typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件    
  14. if(typeid=="1")   
  15. {   
  16. //取得数据表各列标题,各标题之间以/t分割,最后一个列标题后加回车符    
  17. for(i=0;i colHeaders+=dt.Columns[i].Caption.ToString()+"/t";   
  18. colHeaders +=dt.Columns[i].Caption.ToString() +"/n";   
  19. //向HTTP输出流中写入取得的数据信息    
  20. resp.Write(colHeaders);   
  21. //逐行处理数据    
  22. foreach(DataRow row in myRow)   
  23. {   
  24. //在当前行中,逐列获得数据,数据之间以/t分割,结束时加回车符/n    
  25. for(i=0;i ls_item +=row[i].ToString() + "/t";   
  26. ls_item += row[i].ToString() +"/n";   
  27. //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据    
  28. resp.Write(ls_item);   
  29. ls_item="";   
  30. }   
  31. }   
  32. else   
  33. {   
  34. if(typeid=="2")   
  35. {   
  36. //从DataSet中直接导出XML数据并且写到HTTP输出流中    
  37. resp.Write(ds.GetXml());   
  38. }   
  39. }   
  40. //写缓冲区中的数据到HTTP头文件中    
  41. resp.End();   
  42.   
  43.   
  44. }   

 

2、使用微软的C++写的ACTIVEX控件:http://download.microsoft.com/download/OfficeXPDev/sample/1.0/WIN98MeXP/EN-US/Dsoframerctl.exe


3、由datagrid生成:

  1. public void ToExcel(System.Web.UI.Control ctl)   
  2. {   
  3. HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");   
  4. HttpContext.Current.Response.Charset ="UTF-8";   
  5. HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;   
  6. HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword    
  7. ctl.Page.EnableViewState =false;   
  8. System.IO.StringWriter tw = new System.IO.StringWriter() ;   
  9. System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);   
  10. ctl.RenderControl(hw);   
  11. HttpContext.Current.Response.Write(tw.ToString());   
  12. HttpContext.Current.Response.End();   
  13. }   

用法:ToExcel(datagrid1);

 

4、这个用dataview ,代码好长

  1. public void OutputExcel(DataView dv,string str)   
  2. {   
  3. //    
  4. // TODO: 在此处添加构造函数逻辑    
  5. //    
  6. //dv为要输出到Excel的数据,str为标题名称    
  7. GC.Collect();   
  8. Application excel;// = new Application();    
  9. int rowIndex=4;   
  10. int colIndex=1;   
  11.   
  12. _Workbook xBk;   
  13. _Worksheet xSt;   
  14.   
  15. excel= new ApplicationClass();   
  16.   
  17. xBk = excel.Workbooks.Add(true);   
  18.   
  19. xSt = (_Worksheet)xBk.ActiveSheet;   
  20.   
  21. //    
  22. //取得标题    
  23. //    
  24. foreach(DataColumn col in dv.Table.Columns)   
  25. {   
  26. colIndex++;   
  27. excel.Cells[4,colIndex] = col.ColumnName;   
  28. xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐    
  29. }   
  30.   
  31. //    
  32. //取得表格中的数据    
  33. //    
  34. foreach(DataRowView row in dv)   
  35. {   
  36. rowIndex ++;   
  37. colIndex = 1;   
  38. foreach(DataColumn col in dv.Table.Columns)   
  39. {   
  40. colIndex ++;   
  41. if(col.DataType == System.Type.GetType("System.DateTime"))   
  42. {   
  43. excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");   
  44. xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐    
  45. }   
  46. else   
  47. if(col.DataType == System.Type.GetType("System.String"))   
  48. {   
  49. excel.Cells[rowIndex,colIndex] = "''"+row[col.ColumnName].ToString();   
  50. xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐    
  51. }   
  52. else   
  53. {   
  54. excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();   
  55. }   
  56. }   
  57. }   
  58. //    
  59. //加载一个合计行    
  60. //    
  61. int rowSum = rowIndex + 1;   
  62. int colSum = 2;   
  63. excel.Cells[rowSum,2] = "合计";   
  64. xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;   
  65. //    
  66. //设置选中的部分的颜色    
  67. //    
  68. xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();   
  69. xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种    
  70. //    
  71. //取得整个报表的标题    
  72. //    
  73. excel.Cells[2,2] = str;   
  74. //    
  75. //设置整个报表的标题格式    
  76. //    
  77. xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;   
  78. xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;   
  79. //    
  80. //设置报表表格为最适应宽度    
  81. //    
  82. xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();   
  83. xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();   
  84. //    
  85. //设置整个报表的标题为跨列居中    
  86. //    
  87. xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();   
  88. xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;   
  89. //    
  90. //绘制边框    
  91. //    
  92. xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;   
  93. xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗    
  94. xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗    
  95. xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗    
  96. xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗    
  97. //    
  98. //显示效果    
  99. //    
  100. excel.Visible=true;   
  101.   
  102. //xSt.Export(Server.MapPath(".")+"//"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);    
  103. xBk.SaveCopyAs(Server.MapPath(".")+"//"+this.xlfile.Text+".xls");   
  104.   
  105. ds = null;   
  106. xBk.Close(falsenull,null);   
  107.   
  108. excel.Quit();   
  109. System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);   
  110. System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);   
  111. System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);   
  112. xBk = null;   
  113. excel = null;   
  114. xSt = null;   
  115. GC.Collect();   
  116. string path = Server.MapPath(this.xlfile.Text+".xls");   
  117.   
  118. System.IO.FileInfo file = new System.IO.FileInfo(path);   
  119. Response.Clear();   
  120. Response.Charset="GB2312";   
  121. Response.ContentEncoding=System.Text.Encoding.UTF8;   
  122. // 添加头信息,为"文件下载/另存为"对话框指定默认文件名    
  123. Response.AddHeader("Content-Disposition""attachment; filename=" + Server.UrlEncode(file.Name));   
  124. // 添加头信息,指定文件大小,让浏览器能够显示下载进度    
  125. Response.AddHeader("Content-Length", file.Length.ToString());   
  126.   
  127. // 指定返回的是一个不能被客户端读取的流,必须被下载    
  128. Response.ContentType = "application/ms-excel";   
  129.   
  130. // 把文件流发送到客户端    
  131. Response.WriteFile(file.FullName);   
  132. // 停止页面的执行    
  133.   
  134. Response.End();   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值