After searching in Internet and trying some codes i exported data from Excel to Datatable with Interop. The Problem is, it's very slow. Can someone give me a key how can i make it quicker with Interop, not OLEDB or anything else?
My code:
object misValue = System.Reflection.Missing.Value;
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(userSelectedFilePath2);
Excel._Worksheet xlWorksheet1 = xlWorkbook.Sheets[1];
Excel.Range xlRange1 = xlWorksheet1.UsedRange;
DataTable excelTb1 = new DataTable();
for (int j = 1; j <= xlRange1.Columns.Count; j++) // Header Names
{
excelTb1.Columns.Add(xlRange1.Cells[1, j].Value2.ToString());
}
DataRow dataRow = null;
for (int row = 2; row < xlRange1.Rows.Count + 1; row++)
{
dataRow = excelTb1.NewRow();
for (int col = 1; col <= xlRange1.Columns.Count; col++)
{
dataRow[col - 1] = (xlRange1.Cells[row, col] as Excel.Range).Value2;
}
excelTb1.Rows.Add(dataRow);
}
xlWorkbook.Close(true, misValue, misValue);
xlApp.Quit();
dataGridView1.DataSource = excelTb1;