转换DataSet中的多个表为Excel中的多个Sheets
第一种方法:
1. 在设计页面,有一个button按钮,当用户单击按钮的时候,发生转换
<asp:Button ID="Export" runat="server" Text="Export" OnClick="Export_Click" />
2. 发生转换的代码
protected void Export_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DemosDatabaseConnectionString"].ConnectionString))
{
DataSet ds = new System.Data.DataSet(); string sql1 = "select * from Customers";
SqlDataAdapter sda1 = new SqlDataAdapter(sql1, conn);
sda1.Fill(ds, "Table1"); string sql2 = "select * from Users";
SqlDataAdapter sda2 = new SqlDataAdapter(sql2, conn);
sda2.Fill(ds, "Table2"); string sq3 = "select * from AccordionContent";
SqlDataAdapter sda3 = new SqlDataAdapter(sq3, conn);
sda3.Fill(ds, "Table3"); //The file save path
string FileName = "D:\\Testing.xls";
Application ExcelApp = new Application();
Workbook ExcelWorkBook = null;
Worksheet ExcelWorkSheet = null;
ExcelApp.Visible = true;
ExcelWorkBook = ExcelApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
List<string> SheetNames = new List<string>();
SheetNames.Add("Customers Information");
SheetNames.Add("Users Information");
SheetNames.Add("AccordionContent Information");
try
{
for (int i = ; i < ds.Tables.Count; i++)
ExcelWorkBook.Worksheets.Add(); //Adding New sheet in Excel Workbook
for (int i = ; i < ds.Tables.Count; i++)
{
int r = ; // Initialize Excel Row Start Position = 1
ExcelWorkSheet = ExcelWorkBook.Worksheets[i + ];
//Writing Columns Name in Excel Sheet
for (int col = ; col < ds.Tables[i].Columns.Count; col++)
ExcelWorkSheet.Cells[r, col] = ds.Tables[i].Columns[col - ].ColumnName;
r++;
//Writing Rows into Excel Sheet
for (int row = ; row < ds.Tables[i].Rows.Count; row++) //r stands for ExcelRow and col for ExcelColumn
{
// Excel row and column start positions for writing Row=1 and Col=1
for (int col = ; col < ds.Tables[i].Columns.Count; col++)
ExcelWorkSheet.Cells[r, col] = ds.Tables[i].Rows[row][col - ].ToString();
r++;
}
ExcelWorkSheet.Name = SheetNames[i];//Renaming the ExcelSheets
}
ExcelWorkBook.SaveAs(FileName);
ExcelWorkBook.Close();
ExcelApp.Quit();
Marshal.ReleaseComObject(ExcelWorkSheet);
Marshal.ReleaseComObject(ExcelWorkBook);
Marshal.ReleaseComObject(ExcelApp);
}
catch (Exception exHandle)
{
Console.WriteLine("Exception: " + exHandle.Message);
Console.ReadLine();
}
finally
{
foreach (Process process in Process.GetProcessesByName("Excel"))
process.Kill();
}
}
Response.Write("Successfully!");
}
资料来源:
http://www.c-sharpcorner.com/Blogs/10767/generate-excel-with-multiple-sheet-from-dataset.aspx
第二种方法:
2. 发生转换的方法
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DemosDatabaseConnectionString"].ConnectionString))
{
DataSet ds = new System.Data.DataSet(); string sql1 = "select * from Customers";
SqlDataAdapter sda1 = new SqlDataAdapter(sql1, conn);
sda1.Fill(ds, "Table1"); string sql2 = "select * from Users";
SqlDataAdapter sda2 = new SqlDataAdapter(sql2, conn);
sda2.Fill(ds, "Table2"); string sq3 = "select * from AccordionContent";
SqlDataAdapter sda3 = new SqlDataAdapter(sq3, conn);
sda3.Fill(ds, "Table3"); var excel = new Microsoft.Office.Interop.Excel.Application();
var workbook = excel.Workbooks.Add(true); AddExcelSheet(ds.Tables[], workbook);
AddExcelSheet(ds.Tables[], workbook); workbook.SaveAs(@"D:\MyExcelWorkBook2.xls");
workbook.Close(); } }
资料来源:
转换DataSet中的多个表为Excel中的多个Sheets的更多相关文章
- 如何使用免费控件将Word表格中的数据导入到Excel中
我通常使用MS Excel来存储和处理大量数据,但有时候经常会碰到一个问题—我需要的数据存储在word表格中,而不是在Excel中,这样处理起来非常麻烦,尤其是在数据比较庞大的时候, 这时我迫切地需要 ...
- c#.net循环将DataGridView中的数据赋值到Excel中,并设置样式
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel ...
- phpexcel的写操作将数据库中的数据导入到excel中
这个版本据说是可以支持excel2007,但是我使用2007编辑的xlsx是无法获得该库的支持.于是乎我就将它转化为2003.感觉支持地很好. 下面介绍一下具体的使用: require_once('. ...
- 将Datagridview中的数据导出至Excel中
首先添加一个模块ImportToExcel,并添加引用 然后导入命名空间: Imports Microsoft.Office.Interop Imports System.Da ...
- 如何将存储在MongoDB数据库中的数据导出到Excel中?
将MongoDB数据库中的数据导出到Excel中,只需以下几个步骤: (1)首先,打开MongoDB安装目录下的bin文件夹,(C:\Program Files (x86)\MongoDB\Serve ...
- 小技巧之“将Text文件中的数据导入到Excel中,这里空格为分割符为例”
1.使用场景 将数据以文本导出后,想录入到Excel中,的简便方案, 起因:对于Excel的导出,Text导出明显会更方便些 2.将Text文件中的数据导入到Excel中,这里空格为分割符为例的步骤 ...
- Excel中如何匹配另外一个Excel中的数据
场景: 我在Excel中想展示通过一列匹配到另外Excel中的数据.对于程序员来说,就是left join 出 B表的数据. 但是在Excel中怎么做呢,我又不想每次都在把数据导入到数据库中操作. 这 ...
- [转] JAVA中读取网络中的图片资源导入到EXCEL中
需求 导出人员的信息并且加上人员的照片至EXCEL中 完整的代码 //创建一个表格 HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...
- JS将页面中表格,导出到Excel中(IE中)
原文地址:http://blog.csdn.net/sinat_15114467/article/details/51098522 var idTmr; function getExplorer() ...
随机推荐
- CI(-)框架结构
一 CI 是什么 CodeIgniter is an Application Development Framework - a toolkit - for people who build web ...
- python,redis简单订阅
python连接redis import redis r =redis.Redis(host='192.168.199.11',port = 6379 ,db = 0) r.publish('chan ...
- iOS 使用xib创建cell的两种初始化方式
曾几何时,被自己坑过,为了防止下次继续被自己坑,我决定了!在每个我能看到的地方,都把问题写一遍!!! 方法一: ? 1 2 3 4 第一步: [self.collectionView register ...
- SeaJS 简单试用
http://seajs.org/docs/#quick-start 感觉seajs的语法有点罗嗦... 它既有RequireJS的特点也有NodeJS引入模块的特点 例子是抄的官方的例子 在官方的 ...
- QML开源游戏
http://google.github.io/VoltAir/doc/main/html/index.htmlhttp://blog.qt.io/blog/2010/02/26/qt-box2d-i ...
- C# DLL文件注册问题(涉及AxInterop.WMPLib.dll等)
近日遇到问题,给客户安装软件涉及视频等音影播放,安装软件启动过程遇到这样问题: 分析报错原因: 没有注册类别 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG ...
- YY前端笔试总结
1.一个元素float以后.为什么要清除浮动?清除浮动的方法有哪些? 浮动确实是经经常使用,也知道清除浮动的必要性.但要我道个所以然,还是得绞尽脑汁.我个人的理解是,当一个元素float以后,就脱离正 ...
- codeforces #260 DIV 2 C题Boredom(DP)
题目地址:http://codeforces.com/contest/456/problem/C 脑残了. .DP仅仅DP到了n. . 应该DP到10w+的. . 代码例如以下: #include & ...
- js动画学习(五)
九.多属性同时运动 前面的例子都是每个属性单独运动,如果想要多属性同时运动怎么办?比如,我想要一个div的onmouseover事件中宽和高同时变化.下面这个函数是单独变宽: window.onloa ...
- hdu 2815 Mod Tree 高次方程,n不为素数
Accepted 406MS 8576K 2379 B C++/** 这里加了一点限制,,大体还是一样的,, **/ #include <iostream> #include <cs ...