Hi Leute,
ich möchte aus einer Exceldatei die Tabellen einlesen um diese entsprechend nutzen zu können. Es funktioniert eigentlich auch, aber bei einigen Exceldateien wird folgender Fehler geworfen:
System.Data.OleDb.OleDbException wurde nicht behandelt.
Message="Das Microsoft Jet-Datenbankmodul konnte das Objekt ''alle Analog$'_' nicht finden. Stellen Sie sicher, dass das Objekt existiert und dass die Namens- und Pfadangaben richtig eingegeben wurden."
Source="Microsoft JET Database Engine"
ErrorCode=-2147217865
StackTrace:
bei System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
bei System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
bei System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
bei System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
bei System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
bei System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
bei System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
bei System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
bei System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
bei System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable)
bei SUW_API_Schaltplangenerator.SUW_Form.GetExcelDataSet(String excelFile, Boolean headers) in C:\Dokumente und Einstellungen\Koerner\Eigene Dateien\Visual Studio 2008\Projects\SUW_API_Schaltplangenerator\SUW_API_Schaltplangenerator\SUW_Form.cs:Zeile 332.
bei SUW_API_Schaltplangenerator.SUW_Form.BT_ExcelFileOpen_Click(Object sender, EventArgs e) in C:\Dokumente und Einstellungen\Koerner\Eigene Dateien\Visual Studio 2008\Projects\SUW_API_Schaltplangenerator\SUW_API_Schaltplangenerator\SUW_Form.cs:Zeile 73.
bei System.Windows.Forms.Control.OnClick(EventArgs e)
bei System.Windows.Forms.Button.OnClick(EventArgs e)
bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
bei System.Windows.Forms.Control.WndProc(Message& m)
bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
bei System.Windows.Forms.Button.WndProc(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
bei System.Windows.Forms.Application.Run(Form mainForm)
bei SUW_API_Schaltplangenerator.Program.Main() in C:\Dokumente und Einstellungen\Koerner\Eigene Dateien\Visual Studio 2008\Projects\SUW_API_Schaltplangenerator\SUW_API_Schaltplangenerator\Program.cs:Zeile 18.
bei System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart()
InnerException:
Die Methode die ich für das einlesen benutze ist folgend:
private DataSet GetExcelDataSet(string excelFile, bool headers)
{
DataSet ds = new DataSet();
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;"+"Data Source=" + excelFile +";";
if (headers)
{
con.ConnectionString += @"Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";
}
else
{
con.ConnectionString += @"Extended Properties=""Excel 8.0;HDR=No;IMEX=1""";
}
con.Open();
DataTable sheets = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow sheet in sheets.Rows)
{
string tableName = sheet["Table_Name"].ToString();
string sql = "SELECT * FROM [" + tableName + "]";
OleDbDataAdapter adap = new OleDbDataAdapter(sql, con);
adap.Fill(ds, tableName);
}
con.Close();
return ds;
}
Ich habe jetzt schon einige Beiträge hier zu dem Thema gelesen, sehe aber keinen möglichen Fehler den ich mache. Vll. könnt ihr mir ja helfen?!
Grüße
JHK