Что я хочу сделать
Я пытаюсь использовать пространство имен Microsoft.Office.Interop.Excel, чтобы открыть файл Excel (XSL или CSV, но, к сожалению, не XSLX) и импортировать его в DataSet. У меня нет контроля над именами таблиц или столбцов, поэтому мне нужно разрешить их изменения.
Что я пробовал
Я пробовал метод OLEDB в прошлом и имел много проблем с ним (багги, медленные и требуется предварительное знание схемы файлов Excel), поэтому я хочу, чтобы это не повторилось. То, что я хотел бы сделать, это использовать Microsoft.Office.Interop.Excel для импорта рабочей книги непосредственно в DataSet или для циклического просмотра рабочих листов и загрузки каждого из них в DataTable.
Верьте или нет, мне не удалось найти ресурсы для этого. Несколько поисков в StackOverflow нашли в основном людей, пытающихся сделать обратное (DataSet = > Excel) или технику OLEDB. Google не намного полезнее.
Что у меня до сих пор
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;
}
Я в порядке, либо сразу импортирую всю книгу, либо прокручивая один лист за раз. Могу ли я сделать это с помощью Interop.Excel?