代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
DataSet中的数据导出到Excel
作者:
Dezai.CN
/ 发布于
2012/10/9
/
699
DataSet中的数据导出到Excel
<div>方法A:</div> <div>static class ExportingCellByCellMethod {</div> <div> public static void ExportToExcel(DataSet dataSet, string outputPath) { // Create the Excel Application object ApplicationClass excelApp = new ApplicationClass();</div> <div> // Create a new Excel Workbook Workbook excelWorkbook = excelApp.Workbooks.Add(Type.Missing);</div> <div> int sheetIndex = 0;</div> <div> // Copy each DataTable as a new Sheet foreach (System.Data.DataTable dt in dataSet.Tables) {</div> <div> // Create a new Sheet Worksheet excelSheet = (Worksheet) excelWorkbook.Sheets.Add( excelWorkbook.Sheets.get_Item(++sheetIndex), Type.Missing, 1, XlSheetType.xlWorksheet);</div> <div> excelSheet.Name = dt.TableName;</div> <div> // Copy the column names (cell-by-cell) for (int col = 0; col < dt.Columns.Count; col++) { excelSheet.Cells[1, col + 1] = dt.Columns[col].ColumnName; } <div> ((Range) excelSheet.Rows[1, Type.Missing]).Font.Bold = true;</div> <div> // Copy the values (cell-by-cell) for (int col = 0; col < dt.Columns.Count; col++) { for (int row = 0; row < dt.Rows.Count; row++) { excelSheet.Cells[row + 2, col + 1] = dt.Rows[row].ItemArray[col]; } } <div> } <div> // Save and Close the Workbook excelWorkbook.SaveAs(outputPath, XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); excelWorkbook.Close(true, Type.Missing, Type.Missing); excelWorkbook = null;</div> <div> // Release the Application object excelApp.Quit(); excelApp = null;</div> <div> // Collect the unreferenced objects GC.Collect(); GC.WaitForPendingFinalizers();</div> <div> } } <div></div> <div></div> <div>方法B:</div> <div></div> <div>public static void ExportToExcel(DataSet dataSet, string outputPath) { // Create the Excel Application object ApplicationClass excelApp = new ApplicationClass();</div> <div> // Create a new Excel Workbook Workbook excelWorkbook = excelApp.Workbooks.Add(Type.Missing);</div> <div> int sheetIndex = 0;</div> <div> // Copy each DataTable foreach (System.Data.DataTable dt in dataSet.Tables) {</div> <div> // Copy the DataTable to an object array object[,] rawData = new object[dt.Rows.Count + 1, dt.Columns.Count];</div> <div> // Copy the column names to the first row of the object array for (int col = 0; col < dt.Columns.Count; col++) { rawData[0, col] = dt.Columns[col].ColumnName; } <div> // Copy the values to the object array for (int col = 0; col < dt.Columns.Count; col++) { for (int row = 0; row < dt.Rows.Count; row++) { rawData[row + 1, col] = dt.Rows[row].ItemArray[col]; } } <div> // Calculate the final column letter string finalColLetter = string.Empty; string colCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; int colCharsetLen = colCharset.Length;</div> <div> if (dt.Columns.Count > colCharsetLen) { finalColLetter = colCharset.Substring( (dt.Columns.Count - 1) / colCharsetLen - 1, 1); } <div> finalColLetter += colCharset.Substring( (dt.Columns.Count - 1) % colCharsetLen, 1);</div> <div> // Create a new Sheet Worksheet excelSheet = (Worksheet) excelWorkbook.Sheets.Add( excelWorkbook.Sheets.get_Item(++sheetIndex), Type.Missing, 1, XlSheetType.xlWorksheet);</div> <div> excelSheet.Name = dt.TableName;</div> <div> // Fast data export to Excel string excelRange = string.Format("A1:{0}{1}", finalColLetter, dt.Rows.Count + 1);</div> <div> excelSheet.get_Range(excelRange, Type.Missing).Value2 = rawData;</div> <div> // Mark the first row as BOLD ((Range) excelSheet.Rows[1, Type.Missing]).Font.Bold = true; } <div> // Save and Close the Workbook excelWorkbook.SaveAs(outputPath, XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); excelWorkbook.Close(true, Type.Missing, Type.Missing); excelWorkbook = null;</div> <div> // Release the Application object excelApp.Quit(); excelApp = null;</div> <div> // Collect the unreferenced objects GC.Collect(); GC.WaitForPendingFinalizers(); } <div></div> <div>在使用上述方法是需要引进Microsoft.Office.Interop.Excel,并且将该dll的Embed Interop Types属性设置为false。</div> <div></div>
试试其它关键字
导出到Excel
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3