You might redirect me to other links for this one, but I'll be the first to tell you guys: I'VE USED THIS FOR MANY TIMES and I really don't know what's the problem this time.
My scenario is: I need to import Excel data into the database. Sounds easy, right? Now, first I need to know the sheet names. That's where my problem started. And I repeat, I've used it for multiple times, and I don't know what I'm doing wrong this time:
The 'it' I'm referring to is this piece of code:
public string[] GetSheetNames(string excelPath) {
try
{
string[] ar = null;
if (Path.GetExtension(excelPath) == ".xls")
{
conString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"");
}
else if (Path.GetExtension(excelPath) == ".xlsx")
{
conString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelPath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";");
}
using (oleConn = new OleDbConnection(conString))
{
oleConn.Open();
if (oleConn.State == ConnectionState.Open)
{
//DataTable dt = oleConn.GetSchema("Tables");
//DataTable dt = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] {null, null, null, "TABLE"});
DataTable dt = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
{
return null;
}
ar = new string[dt.Rows.Count];
int t = 0;
foreach(DataRow dr in dt.Rows){
ar[t] = dr["TABLE_NAME"].ToString();
t++;
}
}
return ar;
}
}
catch (Exception)
{
throw;
}
}
It throws an Exception, saying that the DataTable's row count is 0. I also used DataTable dt = oleConn.GetSchema("Tables")
and
DataTable dt = oleConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new Object[] {null, null, null, "TABLE"});
but they all returned the same.
What could be the problem? Could it be the Excel file? But I've tried other Excel files and they all failed.
try this code:
OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties='Excel 12.0 xml;HDR=YES;'");
connection.Open();
DataTable Sheets = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
foreach (DataRow dr in Sheets.Rows)
{
string sht = dr[2].ToString().Replace("'", "");
OleDbDataAdapter dataAdapter = new OleDbDataAdapter("select * from [" + sht + "]", connection);
}
I don't know if you still need options but this just happened to me and I got a solution.
My problem was the file itself, when copying to local folder I didn't add the extension so when the connection opens it creates an empty file with no sheets (that's no tables to read)
If you are moving your file to a local folder, take a look into the file and see if it's not corrupted and it does has something to read after the copy.
private DataSet GetExcelWorkSheet(string pathName, string fileName, int workSheetNumber)
{
OleDbConnection ExcelConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + @"\" + fileName + ";Extended Properties=Excel 8.0;");
OleDbCommand ExcelCommand = new OleDbCommand();
ExcelCommand.Connection = ExcelConnection;
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
ExcelConnection.Open();
DataTable ExcelSheets = ExcelConnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
string SpreadSheetName = "[" + ExcelSheets.Rows[workSheetNumber]["TABLE_NAME"].ToString() + "]";
DataSet ExcelDataSet = new DataSet();
ExcelCommand.CommandText = @"SELECT * FROM " + SpreadSheetName;
ExcelAdapter.Fill(ExcelDataSet);
ExcelConnection.Close();
return ExcelDataSet;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.