Здравствуйте, Аноним, Вы писали:
А>Как можно работать с excell-ем из C#? в частности интересует как можно из excell-файла вытащить из каждой закладки DataTable с данными?
я делаю примерно так.
private const int SheetNumber = 0;
private static string GetConnectionString(string path)
{
return string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=No;IMEX=1""", path);
}
private static string GetSheetName(OleDbConnection connection)
{
System.Data.DataTable sheets = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[]{null, null, null, "TABLE"});
string result = "[" + sheets.Rows[SheetNumber]["TABLE_NAME"].ToString() + "]";
return result;
}
private static OleDbCommand GetCommand(OleDbConnection connection)
{
string commandText = "SELECT * FROM " + GetSheetName(connection);
OleDbCommand result = new OleDbCommand(commandText, connection);
return result;
}
public static DataTable GetExcelTable(OleDbConnection connection)
{
connection.Open();
OleDbCommand command = GetCommand(connection);
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds.Tables[0];
}