Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I import from Excel to a DataSet using Microsoft.Office.Interop.Excel?
    primarykey
    data
    text
    <h1>What I want to do</h1> <p>I'm trying to use the <a href="http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel%28v=office.14%29.aspx" rel="nofollow noreferrer"><strong>Microsoft.Office.Interop.Excel</strong> namespace</a> to open an Excel file (XSL or CSV, but sadly <strong>not</strong> XSLX) and import it into a DataSet. I don't have control over the worksheet or column names, so I need to allow for changes to them.</p> <h1>What I've tried</h1> <p>I've tried the <a href="http://www.c-sharpcorner.com/uploadfile/yuanwang200409/102242008174401pm/1.aspx" rel="nofollow noreferrer">OLEDB method</a> of this in the past, and had a lot of problems with it (buggy, slow, and required prior knowledge of the Excel file's schema), so I want to avoid doing that again. What I'd like to do is use Microsoft.Office.Interop.Excel to import the workbook directly to a DataSet, or loop through the worksheets and load each one into a DataTable.</p> <p>Believe it or not, I've had trouble finding resources for this. <a href="https://stackoverflow.com/search?page=3&amp;tab=relevance&amp;q=c#%20excel%20dataset">A few searches on StackOverflow</a> have found mostly people trying to do the reverse (DataSet => Excel), or the OLEDB technique. Google hasn't been much more helpful.</p> <h1>What I've got so far</h1> <pre><code> public void Load(string filename, Excel.XlFileFormat format = Excel.XlFileFormat.xlCSV) { app = new Excel.Application(); book = app.Workbooks.Open(Filename: filename, Format: format); DataSet ds = new DataSet(); foreach (Excel.Worksheet sheet in book.Sheets) { DataTable dt = new DataTable(sheet.Name); ds.Tables.Add(dt); //??? Fill dt from sheet } this.Data = ds; } </code></pre> <p>I'm fine with either importing the entire book at once, or looping through one sheet at a time. Can I do this with Interop.Excel?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload