ASP.NET导入EXCEL方法汇总
- 1、由dataset生成
- public void CreateExcel(DataSet ds,string typeid,string FileName)
- {
- HttpResponse resp;
- resp = Page.Response;
- resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
- resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
- string colHeaders= "", ls_item="";
- int i=0;
- //定义表对象与行对像,同时用DataSet对其值进行初始化
- DataTable dt=ds.Tables[0];
- DataRow[] myRow=dt.Select("");
- // typeid=="1"时导出为EXCEL格式文件;typeid=="2"时导出为XML格式文件
- if(typeid=="1")
- {
- //取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
- for(i=0;i colHeaders+=dt.Columns[i].Caption.ToString()+"\t";
- colHeaders +=dt.Columns[i].Caption.ToString() +"\n";
- //向HTTP输出流中写入取得的数据信息
- resp.Write(colHeaders);
- //逐行处理数据
- foreach(DataRow row in myRow)
- {
- //在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
- for(i=0;i ls_item +=row[i].ToString() + "\t";
- ls_item += row[i].ToString() +"\n";
- //当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
- resp.Write(ls_item);
- ls_item="";
- }
- }
- else
- {
- if(typeid=="2")
- {
- //从DataSet中直接导出XML数据并且写到HTTP输出流中
- resp.Write(ds.GetXml());
- }
- }
- //写缓冲区中的数据到HTTP头文件中
- resp.End();
- }
- 2、由datagrid生成
- public void ToExcel(System.Web.UI.Control ctl)
- {
- HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=Excel.xls");
- HttpContext.Current.Response.Charset ="UTF-8";
- HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
- HttpContext.Current.Response.ContentType ="application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.msexcel/
- msword
- ctl.Page.EnableViewState =false;
- System.IO.StringWriter tw = new System.IO.StringWriter() ;
- System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
- ctl.RenderControl(hw);
- HttpContext.Current.Response.Write(tw.ToString());
- HttpContext.Current.Response.End();
- }
- 用法:ToExcel(datagrid1);
- 3、这个用dataview
- public void OutputExcel(DataView dv,string str)
- {
- //
- // TODO: 在此处添加构造函数逻辑
- //
- //dv为要输出到Excel的数据,str为标题名称
- GC.Collect();
- Application excel;// = new Application();
- int rowIndex=4;
- int colIndex=1;
- _Workbook xBk;
- _Worksheet xSt;
- excel= new ApplicationClass();
- xBk = excel.Workbooks.Add(true);
- xSt = (_Worksheet)xBk.ActiveSheet;
- //
- //取得标题
- //
- foreach(DataColumn col in dv.Table.Columns)
- {
- colIndex++;
- excel.Cells[4,colIndex] = col.ColumnName;
- xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置标题格
- 式为居中对齐
- }
- //
- //取得表格中的数据
- //
- foreach(DataRowView row in dv)
- {
- rowIndex ++;
- colIndex = 1;
- foreach(DataColumn col in dv.Table.Columns)
- {
- colIndex ++;
- if(col.DataType == System.Type.GetType("System.DateTime"))
- {
- excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
- xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment =
- XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
- }
- else
- if(col.DataType == System.Type.GetType("System.String"))
- {
- excel.Cells[rowIndex,colIndex] = "’"+row[col.ColumnName].ToString();
- xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment =
- XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
- }
- else
- {
- excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
- }
- }
- }
- //
- //加载一个合计行
- //
- int rowSum = rowIndex + 1;
- int colSum = 2;
- excel.Cells[rowSum,2] = "合计";
- xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
- //
- //设置选中的部分的颜色
- //
- xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
- xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有
- 56种
- //
- //取得整个报表的标题
- //
- excel.Cells[2,2] = str;
- //
- //设置整个报表的标题格式
- //
- xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
- xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
- //
- //设置报表表格为最适应宽度
- //
- xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
- xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
- //
- //设置整个报表的标题为跨列居中
- //
- xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
- xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
- //
- //绘制边框
- //
- xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
- xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight =
- XlBorderWeight.xlThick;//设置左边线加粗
- xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight =
- XlBorderWeight.xlThick;//设置上边线加粗
- xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight =
- XlBorderWeight.xlThick;//设置右边线加粗
- xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight =
- XlBorderWeight.xlThick;//设置下边线加粗
- //
- //显示效果
- //
- excel.Visible=true;
- //xSt.Export(Server.MapPath(".")
- +"file://"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportForma
- t.ssExportHTML/);
- xBk.SaveCopyAs(Server.MapPath(".")+"file://"+this.xlfile.Text+".xls/ ");
- ds = null;
- xBk.Close(false, null,null);
- excel.Quit();
- System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
- System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
- System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
- xBk = null;
- excel = null;
- xSt = null;
- GC.Collect();
- string path = Server.MapPath(this.xlfile.Text+".xls");
- System.IO.FileInfo file = new System.IO.FileInfo(path);
- Response.Clear();
- Response.Charset="GB2312";
- Response.ContentEncoding=System.Text.Encoding.UTF8;
- // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
- Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
- // 添加头信息,指定文件大小,让浏览器能够显示下载进度
- Response.AddHeader("Content-Length", file.Length.ToString());
- // 指定返回的是一个不能被客户端读取的流,必须被下载
- Response.ContentType = "application/ms-excel";
- // 把文件流发送到客户端
- Response.WriteFile(file.FullName);
- // 停止页面的执行
- Response.End();
- }
ASP.NET导入EXCEL方法汇总的更多相关文章
- .Net MVC 导入导出Excel总结(三种导出Excel方法,一种导入Excel方法) 通过MVC控制器导出导入Excel文件(可用于java SSH架构)
.Net MVC 导入导出Excel总结(三种导出Excel方法,一种导入Excel方法) [原文地址] 通过MVC控制器导出导入Excel文件(可用于java SSH架构) public cl ...
- ASP.NET 导入EXCEL文档
鉴于教务一般都是手动输入学生信息,在未了解本校数据库的客观情况之下,我们准备设计一个导入excel文档中学生信息如数据库的功能.结合网上各类大牛的综合版本出炉.. 首先具体的实现思想如下: 1.先使用 ...
- ASP.NET导入Excel到SQL数据库
protected void btnChange_Click(object sender, EventArgs e) { UserInfoClass tClass = (UserInfoClass)S ...
- asp.net导入Excel表
一.导入Excel的界面这个界面很简单,代码就不列出来了.二.导入的代码我分了两部分,第一部分是点击查看数据的代码,这个是将数据导入到DataTable里面,但是还没有导入到数据库里.这里需要注意的是 ...
- ASP.NETCore -----导入Excel文件
前端上传excel文件利用npoi读取数据转换成datatable(netcore坑爹啊,用的vs2017竟然不能可视化) 前端界面 @{ Layout = null; } <!DOCTYPE ...
- ASP.NET常用导出Excel方法汇总
本文转载:http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html http://geekswithblogs.net/a ...
- Asp.net 导入Excel数据
前台代码: <body> <form id="form1" runat="server"> <div> <asp:Fi ...
- asp.net 导入Excel记录到数据库中
常用到的一个数据库导入功能,这样的话就省了很大一部分时间来处理程序上的问题而不是无休止的重复复制粘贴动作. 其他的废话不多说,直接上代码: 前提条件: 根目录下建立uploadfiles文件夹(用于保 ...
- asp.netDataTable导出excel方法(1)
先来写一段代码,这段代码也是我在网上找的,但是他那个原先有点问题,我对他那个进行了修改,现在这个代码是我修改改过的,应该没有问题的. public int StreamExport(System.Da ...
随机推荐
- asp.net登录状态验证
文章:ASP.NET 登录验证 文章:ASP.NET MVC下判断用户登录和授权状态方法 文章:.net学习笔记---HttpHandle与HttpModule 第一篇文章,介绍了 1)早期的Base ...
- TCP系列47—拥塞控制—10、FACK下的快速恢复与PRR
一.概述 FACK下的重传我们在之前的重传部分已经进行了介绍,这里简单介绍一下随着FACK提出的拥塞控制算法的改进及随后的进一步改进. 从我们之前介绍的RFC2582和RFC5681中可以看到,快速恢 ...
- 【BioCode】将多个蛋白质序列分成单个的txt文档
代码说明: fasta格式的蛋白质序列,一个txt里面有很多蛋白质序列,计算ss.pssm或disorder score时候都需要单条计算,需要分开. 分割前: 分割后: show you the c ...
- UEditor前端配置项说明
UEditor 的配置项分为两类:前端配置项 和 后端配置项 后端配置项具体看这个文档L:后端配置项说明 本文档介绍如何通过设置前端配置项,定制编辑器的特性,配置方法主要通过修改ueditor.con ...
- 【C++】为多态基类声明virtual析构函数
来自<Effective C++>条款07:为多态声明virtual析构函数 当derived class对象经由一个base class指针被删除,而该base class带着一个non ...
- Apache Hadoop YARN – NodeManager--转载
原文地址:http://zh.hortonworks.com/blog/apache-hadoop-yarn-nodemanager/ The NodeManager (NM) is YARN’s p ...
- 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 ...
- nvidia 无显示选项怎么设置全屏游戏
转自:2楼 http://nbbbs.zol.com.cn/41/218_408871.html 网上搜的方法: 1.按键盘上那个windows键+R,输入regedit 2.然后就是下面的步骤了 ...
- Hyperledger Fabric 实战(十二): Fabric 源码本地调试
借助开发网络调试 fabric 源码本地调试 准备工作 IDE Goland Go 1.9.7 fabric-samples 模块 chaincode-docker-devmode fabric 源码 ...
- 【JQuery】数据
一.前言 接着前一章的内容,继续本章的学习 二.内容 queue 显示或操作在匹配元素上执行的函数队列 .queue(queueName) 操作在匹配元素上执行的函数队列 .queue( ...