1. 1、由dataset生成
  2. public void CreateExcel(DataSet ds,string typeid,string FileName)
  3. {
  4. HttpResponse resp;
  5. resp = Page.Response;
  6. resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
  7. resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
  8. string colHeaders= "", ls_item="";
  9. int i=0;
  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. 2、由datagrid生成
  44. public void ToExcel(System.Web.UI.Control ctl)
  45. {
  46. HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
  47. HttpContext.Current.Response.Charset ="UTF-8";
  48. HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
  49. HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.msexcel/
  50. msword
  51. ctl.Page.EnableViewState =false;
  52. System.IO.StringWriter tw = new System.IO.StringWriter() ;
  53. System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
  54. ctl.RenderControl(hw);
  55. HttpContext.Current.Response.Write(tw.ToString());
  56. HttpContext.Current.Response.End();
  57. }
  58. 用法:ToExcel(datagrid1);
  59. 3、这个用dataview
  60. public void OutputExcel(DataView dv,string str)
  61. {
  62. //
  63. // TODO: 在此处添加构造函数逻辑
  64. //
  65. //dv为要输出到Excel的数据,str为标题名称
  66. GC.Collect();
  67. Application excel;// = new Application();
  68. int rowIndex=4;
  69. int colIndex=1;
  70. _Workbook xBk;
  71. _Worksheet xSt;
  72. excel= new ApplicationClass();
  73. xBk = excel.Workbooks.Add(true);
  74. xSt = (_Worksheet)xBk.ActiveSheet;
  75. //
  76. //取得标题
  77. //
  78. foreach(DataColumn col in dv.Table.Columns)
  79. {
  80. colIndex++;
  81. excel.Cells[4,colIndex] = col.ColumnName;
  82. xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格
  83. 式为居中对齐
  84. }
  85. //
  86. //取得表格中的数据
  87. //
  88. foreach(DataRowView row in dv)
  89. {
  90. rowIndex ++;
  91. colIndex = 1;
  92. foreach(DataColumn col in dv.Table.Columns)
  93. {
  94. colIndex ++;
  95. if(col.DataType == System.Type.GetType("System.DateTime"))
  96. {
  97. excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
  98. xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment =
  99. XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
  100. }
  101. else
  102. if(col.DataType == System.Type.GetType("System.String"))
  103. {
  104. excel.Cells[rowIndex,colIndex] = "’"+row[col.ColumnName].ToString();
  105. xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment =
  106. XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
  107. }
  108. else
  109. {
  110. excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
  111. }
  112. }
  113. }
  114. //
  115. //加载一个合计行
  116. //
  117. int rowSum = rowIndex + 1;
  118. int colSum = 2;
  119. excel.Cells[rowSum,2] = "合计";
  120. xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
  121. //
  122. //设置选中的部分的颜色
  123. //
  124. xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
  125. xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有
  126. 56种
  127. //
  128. //取得整个报表的标题
  129. //
  130. excel.Cells[2,2] = str;
  131. //
  132. //设置整个报表的标题格式
  133. //
  134. xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
  135. xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
  136. //
  137. //设置报表表格为最适应宽度
  138. //
  139. xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
  140. xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
  141. //
  142. //设置整个报表的标题为跨列居中
  143. //
  144. xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
  145. xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
  146. //
  147. //绘制边框
  148. //
  149. xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
  150. xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight =
  151. XlBorderWeight.xlThick;//设置左边线加粗
  152. xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight =
  153. XlBorderWeight.xlThick;//设置上边线加粗
  154. xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight =
  155. XlBorderWeight.xlThick;//设置右边线加粗
  156. xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight =
  157. XlBorderWeight.xlThick;//设置下边线加粗
  158. //
  159. //显示效果
  160. //
  161. excel.Visible=true;
  162. //xSt.Export(Server.MapPath(".")
  163. +"file://"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportForma
  164. t.ssExportHTML/);
  165. xBk.SaveCopyAs(Server.MapPath(".")+"file://"+this.xlfile.Text+".xls/ ");
  166. ds = null;
  167. xBk.Close(false, null,null);
  168. excel.Quit();
  169. System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
  170. System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
  171. System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
  172. xBk = null;
  173. excel = null;
  174. xSt = null;
  175. GC.Collect();
  176. string path = Server.MapPath(this.xlfile.Text+".xls");
  177. System.IO.FileInfo file = new System.IO.FileInfo(path);
  178. Response.Clear();
  179. Response.Charset="GB2312";
  180. Response.ContentEncoding=System.Text.Encoding.UTF8;
  181. // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
  182. Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
  183. // 添加头信息,指定文件大小,让浏览器能够显示下载进度
  184. Response.AddHeader("Content-Length", file.Length.ToString());
  185. // 指定返回的是一个不能被客户端读取的流,必须被下载
  186. Response.ContentType = "application/ms-excel";
  187. // 把文件流发送到客户端
  188. Response.WriteFile(file.FullName);
  189. // 停止页面的执行
  190. Response.End();
  191. }

ASP.NET导入EXCEL方法汇总的更多相关文章

  1. .Net MVC 导入导出Excel总结(三种导出Excel方法,一种导入Excel方法) 通过MVC控制器导出导入Excel文件(可用于java SSH架构)

    .Net MVC  导入导出Excel总结(三种导出Excel方法,一种导入Excel方法) [原文地址] 通过MVC控制器导出导入Excel文件(可用于java SSH架构)   public cl ...

  2. ASP.NET 导入EXCEL文档

    鉴于教务一般都是手动输入学生信息,在未了解本校数据库的客观情况之下,我们准备设计一个导入excel文档中学生信息如数据库的功能.结合网上各类大牛的综合版本出炉.. 首先具体的实现思想如下: 1.先使用 ...

  3. ASP.NET导入Excel到SQL数据库

    protected void btnChange_Click(object sender, EventArgs e) { UserInfoClass tClass = (UserInfoClass)S ...

  4. asp.net导入Excel表

    一.导入Excel的界面这个界面很简单,代码就不列出来了.二.导入的代码我分了两部分,第一部分是点击查看数据的代码,这个是将数据导入到DataTable里面,但是还没有导入到数据库里.这里需要注意的是 ...

  5. ASP.NETCore -----导入Excel文件

    前端上传excel文件利用npoi读取数据转换成datatable(netcore坑爹啊,用的vs2017竟然不能可视化) 前端界面 @{ Layout = null; } <!DOCTYPE ...

  6. ASP.NET常用导出Excel方法汇总

    本文转载:http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html http://geekswithblogs.net/a ...

  7. Asp.net 导入Excel数据

    前台代码: <body> <form id="form1" runat="server"> <div> <asp:Fi ...

  8. asp.net 导入Excel记录到数据库中

    常用到的一个数据库导入功能,这样的话就省了很大一部分时间来处理程序上的问题而不是无休止的重复复制粘贴动作. 其他的废话不多说,直接上代码: 前提条件: 根目录下建立uploadfiles文件夹(用于保 ...

  9. asp.netDataTable导出excel方法(1)

    先来写一段代码,这段代码也是我在网上找的,但是他那个原先有点问题,我对他那个进行了修改,现在这个代码是我修改改过的,应该没有问题的. public int StreamExport(System.Da ...

随机推荐

  1. asp.net登录状态验证

    文章:ASP.NET 登录验证 文章:ASP.NET MVC下判断用户登录和授权状态方法 文章:.net学习笔记---HttpHandle与HttpModule 第一篇文章,介绍了 1)早期的Base ...

  2. TCP系列47—拥塞控制—10、FACK下的快速恢复与PRR

    一.概述 FACK下的重传我们在之前的重传部分已经进行了介绍,这里简单介绍一下随着FACK提出的拥塞控制算法的改进及随后的进一步改进. 从我们之前介绍的RFC2582和RFC5681中可以看到,快速恢 ...

  3. 【BioCode】将多个蛋白质序列分成单个的txt文档

    代码说明: fasta格式的蛋白质序列,一个txt里面有很多蛋白质序列,计算ss.pssm或disorder score时候都需要单条计算,需要分开. 分割前: 分割后: show you the c ...

  4. UEditor前端配置项说明

    UEditor 的配置项分为两类:前端配置项 和 后端配置项 后端配置项具体看这个文档L:后端配置项说明 本文档介绍如何通过设置前端配置项,定制编辑器的特性,配置方法主要通过修改ueditor.con ...

  5. 【C++】为多态基类声明virtual析构函数

    来自<Effective C++>条款07:为多态声明virtual析构函数 当derived class对象经由一个base class指针被删除,而该base class带着一个non ...

  6. Apache Hadoop YARN – NodeManager--转载

    原文地址:http://zh.hortonworks.com/blog/apache-hadoop-yarn-nodemanager/ The NodeManager (NM) is YARN’s p ...

  7. DIH增量、定时导入并检索数据--转载

    原文地址:http://www.ifunit.com/984/solr%E5%AD%A6%E4%B9%A0%EF%BC%88%E4%BA%94%EF%BC%89dih%E5%A2%9E%E9%87%8 ...

  8. nvidia 无显示选项怎么设置全屏游戏

    转自:2楼   http://nbbbs.zol.com.cn/41/218_408871.html 网上搜的方法: 1.按键盘上那个windows键+R,输入regedit 2.然后就是下面的步骤了 ...

  9. Hyperledger Fabric 实战(十二): Fabric 源码本地调试

    借助开发网络调试 fabric 源码本地调试 准备工作 IDE Goland Go 1.9.7 fabric-samples 模块 chaincode-docker-devmode fabric 源码 ...

  10. 【JQuery】数据

    一.前言        接着前一章的内容,继续本章的学习 二.内容 queue 显示或操作在匹配元素上执行的函数队列 .queue(queueName) 操作在匹配元素上执行的函数队列 .queue( ...