Alex Rivera | Logout

What's the easiest way to create an Excel table with C#?

Asked 2011-01-22T04:39:39.807
11

I have some tabular data that I'd like to turn into an Excel table.

Software available:

  • .NET 4 (C#)
  • Excel 2010 (using the Excel API is OK)
  • I prefer not to use any 3rd party libraries

Information about the data:

  • A couple million rows
  • 5 columns, all strings (very simple and regular table structure)
  • In my script I'm currently using a nested List data structure but I can change that
  • Performance of the script is not critical

Searching online gives many results, and I'm confused whether I should use OleDb, ADO RecordSets, or something else. Some of these technologies seem like overkill for my scenario, and some seem like they might be obsolete.

What is the very simplest way to do this?

Edit: this is a one-time script I intend to run from my attended desktop.

Edit
Report

1 Answer

1

I agree that a 3rd party dll would be cleaner than the com, but if you go the interop route...

Hands down the best way to populate an excel sheet is to first put the data in a 2 dimensional string array, then get an excel range object with the same dimensions and set it (range.set_value2(oarray) I think). Using any other method is hideously slow.

Also be sure you use the appropriate cleanup code in your finally block.

answered 2011-01-22T04:49:06.973

Your Answer