简体   繁体   English

c#将数据从datagridview导出到excel

[英]c# exporting data from datagridview into excel

i have a winforms application and i want to export data from the datagridview into excel. 我有一个winforms应用程序,我想将数据从datagridview导出到excel。 how would i do this? 我该怎么办? thank u for any help 谢谢你的帮助

i want the simplest approach. 我想要最简单的方法。

maybe exporting to CSV is easier? 也许导出为CSV更容易?

You have a number of options 您有很多选择

  1. Write the data to a comma separated value (csv) file 将数据写入逗号分隔值(csv)文件
  2. Use OleDB to write to create an Excel file and write the data to it 使用OleDB编写以创建Excel文件并将数据写入其中
  3. Use the OOXML SDK to create an Excel file 使用OOXML SDK创建一个Excel文件
  4. Write the data to an XML file that can be read by Excel 将数据写入可以由Excel读取的XML文件
  5. Use the NPOI library to create and write to the Excel file 使用NPOI库创建并写入Excel文件

Those are a few options off the top of my head. 这些是我无法解决的几个选择。 Personally I would go with NPOI, I have used this in server based solutions and it is really easy to work with and quite fast. 就我个人而言,我会使用NPOI,我已经在基于服务器的解决方案中使用了它,并且确实很容易使用并且非常快。 However it does not currenly support the xlsx file format, only standard xls. 但是,它目前不支持xlsx文件格式,仅支持标准xls。 The library can be found here 图书馆可以在这里找到

I also found EPPlus , supports xlsx, but I cannot vouch for it since I have not personally used it. 我还找到了EPPlus ,支持xlsx,但由于我个人没有使用过,因此无法担保。

The simplest approach would be to write the data to an XML file and then import that into Excel. 最简单的方法是将数据写入XML文件,然后将其导入Excel。

There's a WriteXML method on the DataTable class that will write the schema out as well so when you import it into Excel all your column headings will be imported as well. DataTable类上有一个WriteXML方法,也可以将模式写出,因此,当您将其导入Excel时,所有列标题也将被导入。

It has the added advantage that you can import the data into other XML aware packages as well. 它的另一个优点是,您也可以将数据导入其他XML感知包中。

There are many ways if you search Google. 搜索Google有很多方法。 Here is one: 这是一个:

http://www.c-sharpcorner.com/UploadFile/hrojasara/2904/ http://www.c-sharpcorner.com/UploadFile/hrojasara/2904/

Another option is to use something like the OpenDocument toolkit to create a "Spreadsheet" using the OpenDocument format. 另一个选择是使用类似OpenDocument工具包的东西来使用OpenDocument格式创建“电子表格”。 This way it can be opened in Excel, OpenOffice, LibreOffice, etc. 这样可以在Excel,OpenOffice,LibreOffice等中打开它。
http://odftoolkit.org/projects/aodl/downloads http://odftoolkit.org/projects/aodl/downloads
Here is a link to code snippets for the toolkit 这是该工具包的代码片段的链接
http://odftoolkit.org/projects/aodl/pages/AODL-examples http://odftoolkit.org/projects/aodl/pages/AODL-examples
Link to example creating a spreadsheet 链接到创建电子表格的示例
http://odftoolkit.org/projects/aodl/pages/AODL-example-1 http://odftoolkit.org/projects/aodl/pages/AODL-example-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM