C#关于导出excel的方法
一说到导出excel可能很多人都会觉得说直接利用npoi 导入导出excel格式的文件,那样非常方便,但是可能有些时候有的浏览器不支持,那么该怎么办呢,现在介绍一种纯C#的导出excel的方法,代码如下:
1 public void excel_output1(DataTable dt) 2 { 3 Response.Clear(); 4 5 Response.Buffer = true; 6 7 Response.Charset = "utf-8 "; 8 Response.AddHeader("Content-Disposition", "attachment;filename=" + 9 HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls", System.Text.Encoding.UTF8)); 10 Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); 11 Response.ContentType = "application/ms-excel"; 12 this.EnableViewState = false; 13 System.IO.StringWriter sw = new System.IO.StringWriter(); 14 15 sw.WriteLine("<?xml version=\"1.0\"?>"); 16 sw.WriteLine("<?mso-application progid=\"Excel.Sheet\"?>"); 17 sw.WriteLine("<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\""); 18 sw.WriteLine(" xmlns:o=\"urn:schemas-microsoft-com:office:office\""); 19 sw.WriteLine(" xmlns:x=\"urn:schemas-microsoft-com:office:excel\""); 20 sw.WriteLine(" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\""); 21 sw.WriteLine(" xmlns:html=\"http://www.w3.org/TR/REC-html40\">"); 22 sw.WriteLine(" <DocumentProperties xmlns=\"urn:schemas-microsoft-com:office:office\">"); 23 sw.WriteLine(" <Created>1996-12-17T01:32:42Z</Created>"); 24 sw.WriteLine(" <LastSaved>2011-05-29T05:59:50Z</LastSaved>"); 25 sw.WriteLine(" <Version>11.6360</Version>"); 26 sw.WriteLine(" </DocumentProperties>"); 27 sw.WriteLine(" <OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\">"); 28 sw.WriteLine(" <RemovePersonalInformation/>"); 29 sw.WriteLine(" </OfficeDocumentSettings>"); 30 sw.WriteLine(" <ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\">"); 31 sw.WriteLine(" <WindowHeight>4530</WindowHeight>"); 32 sw.WriteLine(" <WindowWidth>8505</WindowWidth>"); 33 sw.WriteLine(" <WindowTopX>480</WindowTopX>"); 34 sw.WriteLine(" <WindowTopY>120</WindowTopY>"); 35 sw.WriteLine(" <AcceptLabelsInFormulas/>"); 36 sw.WriteLine(" <ProtectStructure>False</ProtectStructure>"); 37 sw.WriteLine(" <ProtectWindows>False</ProtectWindows>"); 38 sw.WriteLine(" </ExcelWorkbook>"); 39 sw.WriteLine(" <Styles>"); 40 sw.WriteLine(" <Style ss:ID=\"Default\" ss:Name=\"Normal\">"); 41 sw.WriteLine(" <Alignment ss:Vertical=\"Bottom\"/>"); 42 sw.WriteLine(" <Borders/>"); 43 sw.WriteLine(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\"/>"); 44 sw.WriteLine(" <Interior/>"); 45 sw.WriteLine(" <NumberFormat/>"); 46 sw.WriteLine(" <Protection/>"); 47 sw.WriteLine(" </Style>"); 48 sw.WriteLine(" <Style ss:ID=\"s28\">"); 49 sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Bottom\"/>"); 50 sw.WriteLine(" <Borders>"); 51 sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 52 sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 53 sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 54 sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 55 sw.WriteLine(" </Borders>"); 56 sw.WriteLine(" </Style>"); 57 sw.WriteLine(" <Style ss:ID=\"s29\">"); 58 sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>"); 59 sw.WriteLine(" <Borders>"); 60 sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 61 sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 62 sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 63 sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 64 sw.WriteLine(" </Borders>"); 65 sw.WriteLine(" </Style>"); 66 sw.WriteLine(" <Style ss:ID=\"s31\">"); 67 sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>"); 68 sw.WriteLine(" <Borders>"); 69 sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 70 sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 71 sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 72 sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 73 sw.WriteLine(" </Borders>"); 74 sw.WriteLine(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"24\" ss:Bold=\"1\"/>"); 75 sw.WriteLine(" </Style>"); 76 sw.WriteLine(" <Style ss:ID=\"s32\">"); 77 sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Bottom\"/>"); 78 sw.WriteLine(" <Borders>"); 79 sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 80 sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 81 sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 82 sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 83 sw.WriteLine(" </Borders>"); 84 sw.WriteLine(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\" ss:Bold=\"1\"/>"); 85 sw.WriteLine(" </Style>"); 86 sw.WriteLine(" <Style ss:ID=\"s33\">"); 87 sw.WriteLine(" <Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/>"); 88 sw.WriteLine(" <Borders>"); 89 sw.WriteLine(" <Border ss:Position=\"Bottom\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 90 sw.WriteLine(" <Border ss:Position=\"Left\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 91 sw.WriteLine(" <Border ss:Position=\"Right\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 92 sw.WriteLine(" <Border ss:Position=\"Top\" ss:LineStyle=\"Continuous\" ss:Weight=\"1\"/>"); 93 sw.WriteLine(" </Borders>"); 94 sw.WriteLine(" <Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\" ss:Bold=\"1\"/>"); 95 sw.WriteLine(" </Style>"); 96 sw.WriteLine(" </Styles>"); 97 sw.WriteLine(" <Worksheet ss:Name=\"Sheet1\">"); 98 //sw.WriteLine(" <Table ss:ExpandedColumnCount=\"9\" ss:ExpandedRowCount=\"9\" x:FullColumns=\"1\""); 99 100 string dt_str = "<Table ss:ExpandedColumnCount=\"13\" ss:ExpandedRowCount=\""; 101 int dt_rCount = dt.Rows.Count + 5; 102 dt_str += dt_rCount.ToString() + "\" x:FullColumns=\"1\""; 103 sw.WriteLine(dt_str); 104 105 106 sw.WriteLine(" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\">"); 107 108 sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"93.75\"/>"); 109 sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"111.75\" ss:Span=\"1\"/>"); 110 sw.WriteLine(" <Column ss:Index=\"4\" ss:AutoFitWidth=\"0\" ss:Width=\"102\"/>"); 111 sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"135.75\" ss:Span=\"3\"/>"); 112 sw.WriteLine(" <Column ss:Index=\"9\" ss:AutoFitWidth=\"0\" ss:Width=\"183.75\"/>"); 113 sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"63.75\"/>"); 114 sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"183.75\"/>"); 115 sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"63.75\"/>"); 116 sw.WriteLine(" <Column ss:AutoFitWidth=\"0\" ss:Width=\"163.75\"/>"); 117 sw.WriteLine(" <Row ss:Index=\"2\">"); 118 sw.WriteLine(" <Cell ss:MergeAcross=\"12\" ss:MergeDown=\"2\" ss:StyleID=\"s31\"><Data"); 119 sw.WriteLine(" ss:Type=\"String\">回访详单</Data></Cell>"); 120 sw.WriteLine(" </Row>"); 121 sw.WriteLine(" <Row ss:Index=\"5\">"); 122 sw.WriteLine(" <Cell ss:StyleID=\"s32\"><Data ss:Type=\"String\">营业区</Data></Cell>"); 123 sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">任务总数</Data></Cell>"); 124 sw.WriteLine(" <Cell ss:StyleID=\"s32\"><Data ss:Type=\"String\">明确表示不使用</Data></Cell>"); 125 sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">迁移无线</Data></Cell>"); 126 sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">改网通</Data></Cell>"); 127 sw.WriteLine(" <Cell ss:StyleID=\"s32\"><Data ss:Type=\"String\">改电信</Data></Cell>"); 128 sw.WriteLine(" <Cell ss:StyleID=\"s32\"><Data ss:Type=\"String\">联系不上的1个月以上无上网记录</Data></Cell>"); 129 sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">联系不上有上网记录</Data></Cell>"); 130 sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">对网络不满意</Data></Cell>"); 131 sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">对服务不满意</Data></Cell>"); 132 sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">考虑中</Data></Cell>"); 133 sw.WriteLine(" <Cell ss:StyleID=\"s33\"><Data ss:Type=\"String\">已续费</Data></Cell>"); 134 sw.WriteLine(" </Row>"); 135 136 int cs_i = 1; 137 DataRow[] dt_myRow = dt.Select(""); 138 139 foreach (DataRow dt_row in dt_myRow) 140 { 141 sw.WriteLine(" <Row>"); 142 143 sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[0].ToString() + "</Data></Cell>"); 144 sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[1].ToString() + "</Data></Cell>"); 145 sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[2].ToString() + "</Data></Cell>"); 146 sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[2].ToString() + "</Data></Cell>"); 147 sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[4].ToString() + "</Data></Cell>"); 148 sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[5].ToString() + "</Data></Cell>"); 149 sw.WriteLine(" <Cell ss:StyleID=\"s29\"><Data ss:Type=\"String\">" + dt_row[6].ToString() + "</Data></Cell>"); 150 sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[7].ToString() + "</Data></Cell>"); 151 sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[8].ToString() + "</Data></Cell>"); 152 sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[9].ToString() + "</Data></Cell>"); 153 sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[10].ToString() + "</Data></Cell>"); 154 sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[11].ToString() + "</Data></Cell>"); 155 //sw.WriteLine(" <Cell ss:StyleID=\"s28\"><Data ss:Type=\"String\">" + dt_row[12].ToString() + "</Data></Cell>"); 156 sw.WriteLine(" </Row>"); 157 cs_i++; 158 } 159 160 161 162 //sw.WriteLine(" <Row>"); 163 //sw.WriteLine(" <Cell ss:MergeAcross=\"5\" ss:StyleID=\"s33\"><Data ss:Type=\"String\">合计</Data></Cell>"); 164 //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>"); 165 //sw.WriteLine(" <Cell ss:MergeAcross=\"1\" ss:StyleID=\"s32\" ><Data ss:Type=\"Number\"></Data></Cell>"); 166 //sw.WriteLine(" </Row>"); 167 sw.WriteLine(" </Table>"); 168 sw.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">"); 169 sw.WriteLine(" <Print>"); 170 sw.WriteLine(" <ValidPrinterInfo/>"); 171 sw.WriteLine(" <PaperSizeIndex>9</PaperSizeIndex>"); 172 sw.WriteLine(" <HorizontalResolution>600</HorizontalResolution>"); 173 sw.WriteLine(" <VerticalResolution>600</VerticalResolution>"); 174 sw.WriteLine(" </Print>"); 175 sw.WriteLine(" <Selected/>"); 176 sw.WriteLine(" <Panes>"); 177 sw.WriteLine(" <Pane>"); 178 sw.WriteLine(" <Number>3</Number>"); 179 sw.WriteLine(" <ActiveRow>8</ActiveRow>"); 180 sw.WriteLine(" <ActiveCol>12</ActiveCol>"); 181 sw.WriteLine(" </Pane>"); 182 sw.WriteLine(" </Panes>"); 183 sw.WriteLine(" <ProtectObjects>False</ProtectObjects>"); 184 sw.WriteLine(" <ProtectScenarios>False</ProtectScenarios>"); 185 sw.WriteLine(" </WorksheetOptions>"); 186 sw.WriteLine(" </Worksheet>"); 187 sw.WriteLine(" <Worksheet ss:Name=\"Sheet2\">"); 188 sw.WriteLine(" <Table ss:ExpandedColumnCount=\"0\" ss:ExpandedRowCount=\"0\" x:FullColumns=\"1\""); 189 sw.WriteLine(" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\"/>"); 190 sw.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">"); 191 sw.WriteLine(" <ProtectObjects>False</ProtectObjects>"); 192 sw.WriteLine(" <ProtectScenarios>False</ProtectScenarios>"); 193 sw.WriteLine(" </WorksheetOptions>"); 194 sw.WriteLine(" </Worksheet>"); 195 sw.WriteLine(" <Worksheet ss:Name=\"Sheet3\">"); 196 sw.WriteLine(" <Table ss:ExpandedColumnCount=\"0\" ss:ExpandedRowCount=\"0\" x:FullColumns=\"1\""); 197 sw.WriteLine(" x:FullRows=\"1\" ss:DefaultColumnWidth=\"54\" ss:DefaultRowHeight=\"14.25\"/>"); 198 sw.WriteLine(" <WorksheetOptions xmlns=\"urn:schemas-microsoft-com:office:excel\">"); 199 sw.WriteLine(" <ProtectObjects>False</ProtectObjects>"); 200 sw.WriteLine(" <ProtectScenarios>False</ProtectScenarios>"); 201 sw.WriteLine(" </WorksheetOptions>"); 202 sw.WriteLine(" </Worksheet>"); 203 sw.WriteLine("</Workbook>"); 204 205 Response.Write(sw); 206 Response.End(); 207 208 }
转载于:https://www.cnblogs.com/HoverM/p/4085676.html
C#关于导出excel的方法相关推荐
- 懒人小工具1:winform自动生成Model,Insert,Select,Delete以及导出Excel的方法
懒人小工具2:T4自动生成Model,Insert,Select,Delete以及导出Excel的方法 github地址:https://github.com/Jimmey-Jiang/J ...
- csv和excel php 解析_PHP 高效导入导出Excel(csv)方法之fgetcsv()和fputcsv()函数
CSV,是Comma Separated Value(逗号分隔值)的英文缩写,通常都是纯文本文件. 一.CSV数据导入函数fgetcsv() fgetcsv() 函数从文件指针中读入一行并解析 CSV ...
- PHP导出excel的方法总结
PHP导出excel的方法总结 1.两个步骤定义导出方法和内容 public function exportExcel($expTitle,$expCellName,$expTableData,$st ...
- java excel 导出_java导出Excel通用方法实例
数据导出到Excel几乎是所有客户都会提出的一个需求.下面我就分享一下我的代码. 首先需要引入的jar包: 然后就是正式代码了. package lcy._41_50; import java.io. ...
- 用OleDb写的一个导出Excel的方法
为什么80%的码农都做不了架构师?>>> //前几天遇到个导出Excel的问题,然后上网找了很久,终于写出这个方法了,所以分享一下. /// <summary> / ...
- 最简单的php导出excel文件方法
网上有很多php操作excel或其他文件的类库,也做的很完善.比如无比风骚的PHPExcel,官方网站:http://www.codeplex.com/PHPExcel ,pear的Spreadshe ...
- 前端table导出excel表格方法汇总
在做一些后台管理系统中,经常会遇到table 组件:然后需要导出excel.开发中实现方法如下: 第一类:后端来做导出功能(后端做更合适) 1. 后端生成excel,返回一个url地址:前端直接利用浏 ...
- vue导出excel(简单方法完整介绍)
1.安装插件 cnpm install vue-json-excel 2.min.js中引入使用 import JsonExcel from 'vue-json-excel' Vue.componen ...
- 前端 table 导出excel表格方法
需求:获取后台数据,动态生成table后,导出excel表格,要求格式与table格式一致 直接上代码: <a id="export">导出</a> < ...
最新文章
- 因为数据库正在使用,所以无法获得对数据库的独占访问权(转)
- 【循序渐进学Python】1. Python基础知识
- 定义和使用含有泛型的接口
- 优麒麟在linux下安装教程,在优麒麟Ubuntu Kylin系统中安装百度网盘Linux版.deb的方法...
- 【计蒜客 - 2019南昌邀请赛网络赛 - M】Subsequence(字典树,dp预处理)
- node进阶| 解决表单enctype=multipart/form-data 时获取不到Input值的问题
- 【分布式缓存系列】Redis实现分布式锁的正确姿势
- 7 php 内存泄漏_php 大数组导致内存泄露问题
- nginx 学习记录:IP+端口号配置网站、隐藏端口号、自签名证书配置Https、强制Https、自定义404
- 如何下载matlab安装包_MATLAB安装时应该选择哪些模块
- java离线安装包_java8电脑版安装包下载
- JS打开新窗口(window.open() 、href)
- iometer测试方法linux,IOmeter测试指导手册.pdf
- matlab imshow伪彩色,实验7:彩色图像及伪彩色处理
- vs怎么换背景颜色?
- 计算机学报论文审稿流程,计算机学报投稿.docx
- php 云片网对接,如何实现php调用云片网接口发送短信
- 软件下载地址链接收藏
- 威斯康星麦迪逊计算机专业排名,威斯康星大学麦迪逊分校计算机工程类专业排名...
- 不能装载文档控件。请在检查浏览器的选项中检查浏览器的安全设置_【网络安全知识系列(五)】如何正确设置浏览器!...
热门文章
- 垃圾污渍纹理PS笔刷
- java web 登录_javaWeb实现登录功能
- win 7 我的计算机,从“我的电脑”到“此电脑”,Win7/Win10为啥这么改?
- apscheduler调度器异常错误:skipped: maximum number of running instances reached (1)
- 论测试猿如何优雅的甩锅
- 按键精灵简单的限时找图并点击
- 常用的Linux发行版
- 台式计算机网线插哪里,电脑主机网线插哪里?
- excel打印宏_使用Excel宏打印发票
- java hid_Java和HID通讯