将数据从数据集导出到 Excel

发布于 2024-11-03 20:08:08 字数 58 浏览 4 评论 0原文

我正在尝试将数据从数据集导出到 Excel 并将其直接保存到给定路径,而不给我打开、保存或取消的选项。

I am trying to export data from dataset to excel and save it directly to a given path without giving me the option to open,save or cancel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

白馒头 2024-11-10 20:08:08

使用 ExcelLibrary 这是一个单行...

DataSet myDataSet;
... populate data set ...
ExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls", myDataSet);

另请参阅从 C# 创建 Excel(.XLS 和 .XLSX)文件

Using ExcelLibrary this is a one liner ...

DataSet myDataSet;
... populate data set ...
ExcelLibrary.DataSetHelper.CreateWorkbook("MyExcelFile.xls", myDataSet);

See also Create Excel (.XLS and .XLSX) file from C#

じее 2024-11-10 20:08:08

C# Excel 库也可用于导出数据集。有关如何导出的更多详细信息,请参阅此处

ExcelDocument xls = new ExcelDocument();
xls.easy_WriteXLSFile_FromDataSet("ExcelFile.xls", dataset, 
                 new ExcelAutoFormat(Styles.AUTOFORMAT_EASYXLS1), "Sheet Name");

This C# Excel library can also be used to export the dataset. More details about how to export can be found here.

ExcelDocument xls = new ExcelDocument();
xls.easy_WriteXLSFile_FromDataSet("ExcelFile.xls", dataset, 
                 new ExcelAutoFormat(Styles.AUTOFORMAT_EASYXLS1), "Sheet Name");
ㄟ。诗瑗 2024-11-10 20:08:08

这不是最好的解决方案,但这就是我所做的,它打开一个新的 Excel 文档,然后复制数据集中的内容,您所需要做的就是整理列并保存它。

顺便说一句,这是我回答问题的第一篇文章,希望它有所帮助

        private void cmdExport_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("excel.exe");
            try
            {
                copyAlltoClipboard();
                Microsoft.Office.Interop.Excel.Application xlexcel;
                Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;
                xlexcel = new Excel.Application();
                xlexcel.Visible = true;
                xlWorkBook = xlexcel.Workbooks.Add(misValue);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
                CR.Select();
                xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }
        private void copyAlltoClipboard()
        {
            dataGridViewItems.SelectAll();
            DataObject dataObj = dataGridViewItems.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
        }

It's not the greatest solution but here is what I did, it opens a new excel document then copies what is in the dataset, all you need to do is sort out the columns and save it.

Btw totes my first post to answer a question, hope it helps

        private void cmdExport_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("excel.exe");
            try
            {
                copyAlltoClipboard();
                Microsoft.Office.Interop.Excel.Application xlexcel;
                Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
                Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;
                xlexcel = new Excel.Application();
                xlexcel.Visible = true;
                xlWorkBook = xlexcel.Workbooks.Add(misValue);
                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
                Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
                CR.Select();
                xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error :" + ex.Message);
            }
        }
        private void copyAlltoClipboard()
        {
            dataGridViewItems.SelectAll();
            DataObject dataObj = dataGridViewItems.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
        }
感性不性感 2024-11-10 20:08:08

检查此 DataSetToExcel

c# (WinForms-App) 将数据集导出到 Excel

在第一个链接中,更改代码如下:

删除最初启动的所有代码并尝试以下操作

using (StringWriter sw = new StringWriter("Your Path to save"))
{
  using (HtmlTextWriter htw = new HtmlTextWriter(sw))
  {
    // instantiate a datagrid
    DataGrid dg = new DataGrid();
    dg.DataSource = ds.Tables[0];
    dg.DataBind();
    dg.RenderControl(htw);
  }
}

Check this DataSetToExcel

and c# (WinForms-App) export DataSet to Excel

In the first link change the code as follows:

Remove the all code that initially starts and try the following

using (StringWriter sw = new StringWriter("Your Path to save"))
{
  using (HtmlTextWriter htw = new HtmlTextWriter(sw))
  {
    // instantiate a datagrid
    DataGrid dg = new DataGrid();
    dg.DataSource = ds.Tables[0];
    dg.DataBind();
    dg.RenderControl(htw);
  }
}
忆梦 2024-11-10 20:08:08

这是另一个 C# 库,它允许您使用 OpenXML 库将数据集导出到 Excel 2007 .xlsx 文件。

http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

所有源代码以及演示应用程序均免费提供,您可以在 ASP.Net、WPF 和 WinForms 应用程序中使用它。

将类添加到应用程序后,只需调用一个函数即可将数据导出到 Excel 文件中。

CreateExcelFile.CreateExcelDocument(myDataSet, "C:\\Sample.xlsx");

没有比这更容易的了。

祝你好运 !

Here's another C# library, which lets you export from a DataSet to an Excel 2007 .xlsx file, using the OpenXML libraries.

http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

All of the source code is provided, free of charge, along with a demo application, and you can use this in your ASP.Net, WPF and WinForms applications.

Once you've added the class to your application, it just takes one function call to export your data into an Excel file.

CreateExcelFile.CreateExcelDocument(myDataSet, "C:\\Sample.xlsx");

It doesn't get much easier than that.

Good luck !

别闹i 2024-11-10 20:08:08

嗨,我找到了一个完美的解决方案 此处

只需将代码中的 'missing.value' 替换为 System.Type.Missing 即可。同时删除

oWB.Close(System.Type.Missing, System.Type.Missing, System.Type.Missing);

oXL.Quit();
从代码中。否则你的Excel一打开就会自动关闭。

Hi i found a perfect solution Here

Just replace 'missing.value' with System.Type.Missing in the code. Also remove

oWB.Close(System.Type.Missing, System.Type.Missing, System.Type.Missing);
and

oXL.Quit();
from the code. Otherwise your excel will get closed automatically as soon as it open.

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