简体   繁体   中英

c# exporting data from datagridview into excel

i have a winforms application and i want to export data from the datagridview into excel. how would i do this? thank u for any help

i want the simplest approach.

maybe exporting to CSV is easier?

You have a number of options

  1. Write the data to a comma separated value (csv) file
  2. Use OleDB to write to create an Excel file and write the data to it
  3. Use the OOXML SDK to create an Excel file
  4. Write the data to an XML file that can be read by Excel
  5. Use the NPOI library to create and write to the Excel file

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. However it does not currenly support the xlsx file format, only standard 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.

The simplest approach would be to write the data to an XML file and then import that into 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.

It has the added advantage that you can import the data into other XML aware packages as well.

There are many ways if you search Google. Here is one:

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. This way it can be opened in Excel, OpenOffice, LibreOffice, etc.
http://odftoolkit.org/projects/aodl/downloads
Here is a link to code snippets for the toolkit
http://odftoolkit.org/projects/aodl/pages/AODL-examples
Link to example creating a spreadsheet
http://odftoolkit.org/projects/aodl/pages/AODL-example-1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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