问题
here i m using ADO.Net for connect to Excel file using specified connectionString that work fine, my only concern is if sheet name has space character than its not return column names form corresponding sheet. my code :
if (extension == ".xls")
{
connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("~\\ExcelUpload\\Excelsheets\\temp.xls") + ";" +
@"Extended Properties=" + "\"Excel 8.0;HDR=YES;\"";
}
else if (extension == ".xlsx")
{
connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=" + Server.MapPath("~\\ExcelUpload\\Excelsheets\\temp.xlsx") + ";" +
@"Extended Properties=" + "\"Excel 12.0;HDR=YES;\"";
}
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = connectionString;
connection.Open();
DataTable dt;
String[] restrection = { null, null, sheetName + "$", null };
dt = connection.GetSchema("Columns", restrection);
so how can we paas excelsheet name that has space character?
回答1:
Maybe you can use something like this:
var dataset = new DataSet();
var connectionString = "connstring";
var connection = new OleDbConnection(connectionString);
connection.Open();
var sheets = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
if (sheets == null || sheets.Rows.Count < 1) throw new InvalidOperationException("CantReadWorksheets");
foreach (DataRow sheet in sheets.Rows)
{
var tableName = sheet["Table_Name"].ToString();
var sql = "SELECT * FROM [" + tableName + "]";
var adap = new OleDbDataAdapter(sql, connection);
adap.Fill(dataset, tableName);
}
connection.Close();
You can see how I'm getting the sheet names. (try..catch
blocks were snipped)
回答2:
Try
String[] restrictions = { null, null, "['" + sheetName + "$']", null };
So if you have a worksheet TEST WORKSHEET it became 'TEST WORKSHEET$'
It's escapes me why we have to follow such WIERD naming conventions
回答3:
Should be no issue with filename containing spaces. Try to check if file exist before reading from it:
System.IO.File.Exists(Server.MapPath("~\\ExcelUpload\\Excelsheets\\temp filename.xlsx"))
回答4:
The code...
String[] restrection = { null, null, sheetName + "$", null };
...appends a '$' onto the sheet name. Maybe the sheet name needs to be delimited, perhaps...
String[] restrection = { null, null, "[" + sheetName + "]$", null };
...or...
String[] restrection = { null, null, "\"" + sheetName + "\"$", null };
...?
回答5:
Try to replace this:
connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + Server.MapPath("~\\ExcelUpload\\Excelsheets\\temp.xls") + ";" +
@"Extended Properties=" + "\"Excel 8.0;HDR=YES;\"";
with this:
string conn = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + path +
";Extended Properties=\'Excel 8.0; HDR=YES; IMEX=1;\'";
Same for Excel 12.0. In my case, solve the case :)
来源:https://stackoverflow.com/questions/15383308/how-to-get-column-from-excel-sheet-if-excel-sheet-name-contain-space-character-i