I know that this should be easy but how do I export/save a DataGridView to excel?
相关问题
- 'System.Threading.ThreadAbortException' in
- Excel sunburst chart: Some labels missing
- how to use special characters like '<'
- Error handling only works once
- Excel formula in VBA code
相关文章
- vb.net 关于xps文件操作问题
- Checking for DBNull throws a StrongTypingException
- Get column data by Column name and sheet name
- Using the typical get set properties in C#… with p
- programmatically excel cells to be auto fit width
- Unregister a XLL in Excel (VBA)
- Load a .NET assembly from the application's re
- C# equivalent of VB DLL function declaration (Inte
First Import COM library Microsoft Excel Object
Sample Code:
I Use SaveFileDialog to Create the excel in the specific location
Does it need to be a native XLS file? Your best bet is probably just to export the data to a CSV file, which is plain text and reasonably easy to generate. CSVs open in Excel by default for most users so they won't know the difference.
Here some code we use to do it across lots of our apps. We have a special method to clean up "not exportable" column. Additionally, we don't export cols without headers but you can adjust that logic to your needs.
Edit: The code formatter doesn't love vb.net - you can copy/paste into visual studio and it will be fine.
I use this all the time:
Try this out, it's a touch simpler than Brendans but not as 'feature rich':
I'd warn again doing a double for loop to pull out each datacell's data, and writing out individually to an excel cell. Instead, use a 2D object array, and loop through your datagrid saving all your data there. You'll then be able to set an excel range equal to that 2D object array.
This will be several orders of magnitude faster than writing excel cell by cell. Some reports that I've been working on that used to take two hours simply to export have been cut down to under a minute.