Gast
#3716153
Hallo zusammen,
ich steh vor einem Problem und hoffe das Ihr mir helfen könnt.
ich möchte bei einer Excel Tabelle die einzelen Worksheets auslesen und
dann mit etwas vergleichen. Aber ich hänge noch bei dem Auslesen.
private string[] GetWorksheetNames(string excelFile)
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = "Data Source=" + excelFile +
";Provider=Microsoft.Jet.OLEDB.4.0" +
";Extended Properties=Excel 8.0";
try
{
con.Open();
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Message);
}
DataTable tab = con.GetOleDbSchemaTable(
OleDbSchemaGuid.Tables,
new object[] { null, null, null, "TABLE" });
con.Close();
string[] tableNames = new string[tab.Rows.Count];
for (int i = 0; i < tab.Rows.Count; i++)
{
tableNames[i] = tab.Rows[i]
["Table_Name"].ToString();
}
return tableNames;
}
private void btn_Save_Click(object sender, EventArgs e)
{
// Wenn die Datei nicht exestiert dann neu erstellen
if (!File.Exists("c:\\Materialentnahme.xlsx"))
{
if (!string.IsNullOrEmpty(tBox_Name.Text) &&
!string.IsNullOrEmpty(tBox_Porjektnum.Text))
{
string worksheet_Name_Projektnummer =
tBox_Name.Text + tBox_Porjektnum.Text;
Export.Save("c:\\Materialentnahme.xlsx",
worksheet_Name_Projektnummer, Einkauf);
}
if (!string.IsNullOrEmpty(tBox_Name.Text) &&
!string.IsNullOrEmpty(tBox_kostenstelle.Text))
{
string worksheet_Name_Kostenstelle =
tBox_Name.Text + tBox_kostenstelle.Text;
Export.Save("c:\\Materialentnahme.xlsx",
worksheet_Name_Kostenstelle, Einkauf);
}
if(File.Exists( "c:\\Materialentnahme.xlsx" ) )
{
string[] str_ExcelTabeleNames =
GetWorksheetNames("c:\\Materialentnahme.xlsx");
System.Console.WriteLine(str_ExcelTabeleNames);
}
}