如何将数据转储到ExcelWorkBook中?

发布于 2024-09-18 14:16:35 字数 1655 浏览 4 评论 0原文

我有一项任务将数据集数据转储到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁杀 2024-09-25 14:16:35

试试这个 ExportHelper。本文清楚地解释了如何将具有多个表的数据集导出到 Excel 中的多个工作表。

Try this ExportHelper. This article clearly explains how to export a dataset with multiple table to multiple worksheets in excel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文