i've got a problem with the import of Excel Worksheets into a summary Workbook.
First i try to import the data as DataTable, using:
`Dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
tableName = GetTableNames(Dt);
com = new OleDbDataAdapter(SqlQuery(tableName[0]), conn);
com.Fill(DtSet);
Dt = DtSet.Tables[0];`
works fine except the fact, that some of the headline got lost, after copy the data to a new sheet:
private static void ImportData(DataTable source, string p)
{
int pos = Globals.ThisWorkbook.Sheets.Count;
object lastSheet = Globals.ThisWorkbook.Sheets.get_Item(pos);
Globals.ThisWorkbook.Sheets.Add(Type.Missing, lastSheet, Type.Missing, Type.Missing);
Excel.Worksheet ws = (Excel.Worksheet)Globals.ThisWorkbook.ActiveSheet;
ws.Name = p;
// column headings
for (var i = 0; i < source.Columns.Count; i++)
{
ws.Cells[1, i + 1] = source.Columns[i].ColumnName;
}
// rows
for (var i = 0; i < source.Rows.Count; i++)
{
// to do: format datetime values before printing
for (var j = 0; j < source.Columns.Count; j++)
{
ws.Cells[i + 2, j + 1] = source.Rows[i][j];
}
}
}
Second, i hoped to import fix that by importing it via interop so i implemented this one:
public Excel.Worksheet GetWorksheet(string path)
{
Excel.Application vApp = new Excel.Application();
Excel.Workbook vBook = vApp.Workbooks.Open(path);
Excel.Worksheet ws = vBook.Sheets.get_Item(1);
vBook.Close();
vApp.Quit();
return ws;
}
seems to work also fine, until i try to copy this sheet to the active Workbook:
private static void ImportData(Excel.Worksheet source, string p)
{
int pos = Globals.ThisWorkbook.Sheets.Count;
source.Copy(Globals.ThisWorkbook.Worksheets[pos]);
}
this should copy the source to the last position within Globals.ThisWorkbook... but all i got is a COMException
Now i run out of ideas how to fix this. Hope you can help.
Mirko