C#关于导出excel的方法
一说到导出excel可能很多人都会觉得说直接利用npoi 导入导出excel格式的文件,那样非常方便,但是可能有些时候有的浏览器不支持,那么该怎么办呢,现在介绍一种纯C#的导出excel的方法,代码如下:
public void excel_output1(DataTable dt)
{
Response.Clear(); Response.Buffer = true; Response.Charset = "utf-8 ";
Response.AddHeader("Content-Disposition", "attachment;filename=" +
HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls", System.Text.Encoding.UTF8));
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter(); sw.WriteLine("<?xml version=\"1.0\"?>");
sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>");
sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\"");
sw.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\"");
sw.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"");
sw.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">");
sw.WriteLine(" <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">");
sw.WriteLine(" <Created>1996-12-17T01:32:42Z</Created>");
sw.WriteLine(" <LastSaved>2011-05-29T05:59:50Z</LastSaved>");
sw.WriteLine(" <Version>11.6360</Version>");
sw.WriteLine(" </DocumentProperties>");
sw.WriteLine(" <OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\">");
sw.WriteLine(" <RemovePersonalInformation/>");
sw.WriteLine(" </OfficeDocumentSettings>");
sw.WriteLine(" <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine(" <WindowHeight>4530</WindowHeight>");
sw.WriteLine(" <WindowWidth>8505</WindowWidth>");
sw.WriteLine(" <WindowTopX>480</WindowTopX>");
sw.WriteLine(" <WindowTopY>120</WindowTopY>");
sw.WriteLine(" <AcceptLabelsInFormulas/>");
sw.WriteLine(" <ProtectStructure>False</ProtectStructure>");
sw.WriteLine(" <ProtectWindows>False</ProtectWindows>");
sw.WriteLine(" </ExcelWorkbook>");
sw.WriteLine(" <Styles>");
sw.WriteLine(" <Style ss:ID=\"Default\" ss:Name=\"Normal\">");
sw.WriteLine(" <Alignment ss:Vertical=\"Bottom\"/>");
sw.WriteLine(" <Borders/>");
sw.WriteLine(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\"/>");
sw.WriteLine(" <Interior/>");
sw.WriteLine(" <NumberFormat/>");
sw.WriteLine(" <Protection/>");
sw.WriteLine(" </Style>");
sw.WriteLine(" <Style ss:ID=\"s28\">");
sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Bottom\"/>");
sw.WriteLine(" <Borders>");
sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" </Borders>");
sw.WriteLine(" </Style>");
sw.WriteLine(" <Style ss:ID=\"s29\">");
sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>");
sw.WriteLine(" <Borders>");
sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" </Borders>");
sw.WriteLine(" </Style>");
sw.WriteLine(" <Style ss:ID=\"s31\">");
sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>");
sw.WriteLine(" <Borders>");
sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" </Borders>");
sw.WriteLine(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"24\" ss:Bold=\"1\"/>");
sw.WriteLine(" </Style>");
sw.WriteLine(" <Style ss:ID=\"s32\">");
sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Bottom\"/>");
sw.WriteLine(" <Borders>");
sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" </Borders>");
sw.WriteLine(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\" ss:Bold=\"1\"/>");
sw.WriteLine(" </Style>");
sw.WriteLine(" <Style ss:ID=\"s33\">");
sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>");
sw.WriteLine(" <Borders>");
sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>");
sw.WriteLine(" </Borders>");
sw.WriteLine(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\" ss:Bold=\"1\"/>");
sw.WriteLine(" </Style>");
sw.WriteLine(" </Styles>");
sw.WriteLine(" <Worksheet ss:Name=\"Sheet1\">");
//sw.WriteLine(" <Table ss:ExpandedColumnCount=\"9\" ss:ExpandedRowCount=\"9\" x:FullColumns=\"1\""); string dt_str = "<Table ss:ExpandedColumnCount=\"13\" ss:ExpandedRowCount=\"";
int dt_rCount = dt.Rows.Count + ;
dt_str += dt_rCount.ToString() + "\" x:FullColumns=\"1\"";
sw.WriteLine(dt_str); sw.WriteLine(" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\">"); sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"93.75\"/>");
sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"111.75\" ss:Span=\"1\"/>");
sw.WriteLine(" <Column ss:Index=\"4\" ss:AutoFitWidth=\"0\" ss:Width=\"102\"/>");
sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"135.75\" ss:Span=\"3\"/>");
sw.WriteLine(" <Column ss:Index=\"9\" ss:AutoFitWidth=\"0\" ss:Width=\"183.75\"/>");
sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"63.75\"/>");
sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"183.75\"/>");
sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"63.75\"/>");
sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"163.75\"/>");
sw.WriteLine(" <Row ss:Index=\"2\">");
sw.WriteLine(" <Cell ss:MergeAcross=\"12\" ss:MergeDown=\"2\" ss:StyleID=\"s31\"><Data");
sw.WriteLine(" ss:Type=\"String\">回访详单</Data></Cell>");
sw.WriteLine(" </Row>");
sw.WriteLine(" <Row ss:Index=\"5\">");
sw.WriteLine(" <Cell ss:StyleID=\"s32\"><Data ss:Type=\"String\">营业区</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">任务总数</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s32\"><Data ss:Type=\"String\">明确表示不使用</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">迁移无线</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">改网通</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s32\"><Data ss:Type=\"String\">改电信</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s32\"><Data ss:Type=\"String\">联系不上的1个月以上无上网记录</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">联系不上有上网记录</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">对网络不满意</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">对服务不满意</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">考虑中</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">已续费</Data></Cell>");
sw.WriteLine(" </Row>"); int cs_i = ;
DataRow[] dt_myRow = dt.Select(""); foreach (DataRow dt_row in dt_myRow)
{
sw.WriteLine(" <Row>"); sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[].ToString() + "</Data></Cell>");
//sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[12].ToString() + "</Data></Cell>");
sw.WriteLine(" </Row>");
cs_i++;
} //sw.WriteLine(" <Row>");
//sw.WriteLine(" <Cell ss:MergeAcross=\"5\" ss:StyleID=\"s33\"><Data ss:Type=\"String\">合计</Data></Cell>");
//sw.WriteLine(" <Cell ss:StyleID=\"s32\" ss:Formula=\"=SUM(OFFSET(INDIRECT(ADDRESS(ROW(),COLUMN())),6-ROW(),0,ROW()-6,1))\"><Data ss:Type=\"Number\">1</Data></Cell>");
//sw.WriteLine(" <Cell ss:MergeAcross=\"1\" ss:StyleID=\"s32\" ><Data ss:Type=\"Number\"></Data></Cell>");
//sw.WriteLine(" </Row>");
sw.WriteLine(" </Table>");
sw.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine(" <Print>");
sw.WriteLine(" <ValidPrinterInfo/>");
sw.WriteLine(" <PaperSizeIndex>9</PaperSizeIndex>");
sw.WriteLine(" <HorizontalResolution>600</HorizontalResolution>");
sw.WriteLine(" <VerticalResolution>600</VerticalResolution>");
sw.WriteLine(" </Print>");
sw.WriteLine(" <Selected/>");
sw.WriteLine(" <Panes>");
sw.WriteLine(" <Pane>");
sw.WriteLine(" <Number>3</Number>");
sw.WriteLine(" <ActiveRow>8</ActiveRow>");
sw.WriteLine(" <ActiveCol>12</ActiveCol>");
sw.WriteLine(" </Pane>");
sw.WriteLine(" </Panes>");
sw.WriteLine(" <ProtectObjects>False</ProtectObjects>");
sw.WriteLine(" <ProtectScenarios>False</ProtectScenarios>");
sw.WriteLine(" </WorksheetOptions>");
sw.WriteLine(" </Worksheet>");
sw.WriteLine(" <Worksheet ss:Name=\"Sheet2\">");
sw.WriteLine(" <Table ss:ExpandedColumnCount=\"0\" ss:ExpandedRowCount=\"0\" x:FullColumns=\"1\"");
sw.WriteLine(" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\"/>");
sw.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine(" <ProtectObjects>False</ProtectObjects>");
sw.WriteLine(" <ProtectScenarios>False</ProtectScenarios>");
sw.WriteLine(" </WorksheetOptions>");
sw.WriteLine(" </Worksheet>");
sw.WriteLine(" <Worksheet ss:Name=\"Sheet3\">");
sw.WriteLine(" <Table ss:ExpandedColumnCount=\"0\" ss:ExpandedRowCount=\"0\" x:FullColumns=\"1\"");
sw.WriteLine(" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\"/>");
sw.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">");
sw.WriteLine(" <ProtectObjects>False</ProtectObjects>");
sw.WriteLine(" <ProtectScenarios>False</ProtectScenarios>");
sw.WriteLine(" </WorksheetOptions>");
sw.WriteLine(" </Worksheet>");
sw.WriteLine("</Workbook>"); Response.Write(sw);
Response.End(); }
C#关于导出excel的方法的更多相关文章
- asp.net中导出Excel的方法
一.asp.net中导出Excel的方法: 本文转载 在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出 ...
- ASP.NET导出excel表方法汇总
asp.net里导出excel表方法汇总 1.由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) ...
- 懒人小工具:自动生成Model,Insert,Select,Delete以及导出Excel的方法
在开发的过程中,我们为了节约时间,往往会将大量重复机械的代码封装,考虑代码的复用性,这样我们可以节约很多时间来做别的事情.最近跳槽到一节webform开发的公司,主要是开发自己公司用的ERP.开始因为 ...
- 懒人小工具:T4自动生成Model,Insert,Select,Delete以及导出Excel的方法
之前写了篇文章,懒人小工具:[自动生成Model,Insert,Select,Delete以及导出Excel的方法](http://www.jianshu.com/p/d5b11589174a),但是 ...
- 懒人小工具:T4生成实体类Model,Insert,Select,Delete以及导出Excel的方法
由于最近公司在用webform开发ERP,用到大量重复机械的代码,之前写了篇文章,懒人小工具:自动生成Model,Insert,Select,Delete以及导出Excel的方法,但是有人觉得这种方法 ...
- 懒人小工具1:winform自动生成Model,Insert,Select,Delete以及导出Excel的方法
懒人小工具2:T4自动生成Model,Insert,Select,Delete以及导出Excel的方法 github地址:https://github.com/Jimmey-Jiang/J ...
- java根据xml配置文件导出excel通用方法
java web项目中时常会用到导出功能,而导出excel几乎是每个项目必备的功能之一.针对形形色色的导出方法及个人平时的工作经验,特将导出excel方法整理成通用的方法,根据xml配置来实现特定的导 ...
- 一个通用的DataGridView导出Excel扩展方法(支持列数据格式化)
假如数据库表中某个字段存放的值“1”和“0”分别代表“是”和“否”,要在DataGridView中显示“是”和“否”,一般用两种方法,一种是在sql中直接判断获取,另一种是在DataGridView的 ...
- 导出excel 的方法及示例
一.基本知识 1.Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 2. HSSF 是Horribl ...
随机推荐
- Lua自己实现string.split功能
local function split(str, d) --str是需要查分的对象 d是分界符 local lst = { } local n = string.len(str)--长度 local ...
- strits2初始配置总结
**************************************************************************************************** ...
- 使用NPOI导入导出标准的Excel
关于NPOI NPOI是POI项目的.NET版本,是由@Tony Qu(http://tonyqus.cnblogs.com/)等大侠基于POI开发的,可以从http://npoi.codeplex. ...
- ASP.NET 管道事件与HttpModule, HttpHandler简单理解
BeginRequest 指示请求处理开始 AuthenticateRequest 封装请求身份验证过程 AuthorizeRequest 封装检查是否能利用以前缓存的输出页面处理请求的过程 Reso ...
- linux下配置redis
安装redis 1.下载文件 wget http://download.redis.io/releases/redis-2.8.12.tar.gz 2.解压文件 tar zxvf redis-2.8 ...
- 你不知道的Java类
1.StrSubstitutor StrSubstitutor是一个很特殊的类,在String中使用${key}的方法在形成了一个可配置的模板,new一个HashMap使用HashMap替换Strin ...
- 【转】VS2010中 C++创建DLL图解
转载地址:http://blog.csdn.net/g710710/article/details/7255744 一.DLL的创建 创建项目: Win32->Win32项目,名称:MyDLL ...
- [转]error: 'retainCount' is unavailable: not available in automatic reference counting mode
转载地址:http://choijing.iteye.com/blog/1860761 后来发现是编译选项的问题: 1.点击工程名 打开编译选项 2.在编译选项中,选择Bulid Settin ...
- 转义字符_MySQL识别下面的转义序列
转义字符.MySQL识别下面的转义序列 在字符串中,某些序列具有特殊含义.这些序列均用反斜线('\')开始,即所谓的转义字符.MySQL识别下面的转义序列: \0 ASCII 0(NUL)字符. \' ...
- logging模块转载博客
转载自:http://blog.csdn.net/zyz511919766/article/details/25136485 简单将日志打印到屏幕: [python] view plain copy ...