如何将数据转储到ExcelWorkBook中?
我有一项任务将数据集数据转储到 Excel WorkBook 中。假设如果有多个表,那么我必须动态创建更多工作表。为此,我做了一个小应用程序。但我无法创建任何工作表。有人可以帮忙吗我?下面是代码
private void btn_export2excel_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.Tables.Add("Categories");
ds.Tables.Add("Employee");
try
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet[] xlWorkSheet = new Excel.Worksheet[2];
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet[0] = (Excel.Worksheet) xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet[1] = (Excel.Worksheet) xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet[0].Cells[1, 1] = "www.google.com";
xlWorkSheet[1].Cells[1, 1] = "www.yahoo.com";
xlWorkBook.SaveAs("Sample.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet[0]);
releaseObject(xlWorkSheet[1]);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message from form");
}
finally
{
GC.Collect();
}
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
I have a task to dump the dataset data into Excel WorkBook.Suppose if have more than one table then i have to Create More WorkSheets dynamically.For that i did a small application.But i was stuckup with creating no of WorkSheets.Could anyone help me ?Below is the Code
private void btn_export2excel_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.Tables.Add("Categories");
ds.Tables.Add("Employee");
try
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet[] xlWorkSheet = new Excel.Worksheet[2];
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet[0] = (Excel.Worksheet) xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet[1] = (Excel.Worksheet) xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet[0].Cells[1, 1] = "www.google.com";
xlWorkSheet[1].Cells[1, 1] = "www.yahoo.com";
xlWorkBook.SaveAs("Sample.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet[0]);
releaseObject(xlWorkSheet[1]);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Message from form");
}
finally
{
GC.Collect();
}
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个 ExportHelper。本文清楚地解释了如何将具有多个表的数据集导出到 Excel 中的多个工作表。
Try this ExportHelper. This article clearly explains how to export a dataset with multiple table to multiple worksheets in excel.