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 ...
随机推荐
- C++ Primer Plus学习:第十五章
第十五章 友元.异常和其他 友元 友元类 表 0-1 class Tv { public: friend class Remote; } Remote类可以使用Tv的数据成员,Remote类在Tv类后 ...
- 第17章 程序管理与SELinux初探
什么是进程 触发任何一个事件时,系统都会将它定义为一个进程,并且给予这个进程一个ID,称为PID,同时依据触发这个进程的用户与相关属性关系,给予这个进程一组有效的权限设置. 进程与程序 进程:执行一个 ...
- css新增UI方案
一.文本新增样式 opacity 不透明度 h1{ margin: 100px auto; opacity: 0.5; } </style> </head> <body& ...
- C关键字volatile总结
做嵌入式C开发的相信都使用过一个关键字volatile,特别是做底层开发的.假设一个GPIO的数据寄存器地址是0x50000004,我们一般会定义一个这样的宏: #define GDATA *((vo ...
- CCF——图像旋转201503-1
问题描述 旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆时针旋转90度. 计算机中的图像表示可以用一个矩阵来表示,为了旋转一个图像,只需要将对应的矩阵旋转即可. 输入格式 输入的第一行包含 ...
- 【BioCode】Elm格式中提取位点信息
说明: ①Elm格式: PLMD ID Uniprot Accession Position Type Sequence Species PMIDsPlMD编号 ...
- java 基础 --File
1, 创建文件 File file = new File(path); file.createNewFile(); //如果路径不存在,会抛异常 file.mkdir();//如果路径不存在,返回fa ...
- 第123天:移动web开发中的常见问题
一.函数库 underscoreJS _.template: <ol class="carousel-indicators"> <!--渲染的HTML字符串--& ...
- C++解析(20):智能指针与类型转换函数
0.目录 1.智能指针 2.转换构造函数 3.类型转换函数 4.小结 1.智能指针 内存泄漏(臭名昭著的Bug): 动态申请堆空间,用完后不归还 C++语言中没有垃圾回收机制 指针无法控制所指堆空间的 ...
- ubuntu成功安装搜狗输入法
在安装之前,我们要先了解一个事实,那就是linux下安装软件和Windows是非常不同的,并不是简单地双击安装包就可以安装了.linux很多软件都有自己的一个依赖源,如果不先安装好这些依赖源,你是无法 ...