这里我使用 ADO.Net 使用指定的 connectionString 连接到 Excel 文件,它工作正常,我唯一担心的是工作表名称是否有空格字符而不是它不从相应的工作表返回列名。我的代码:
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);
那么我们如何才能将具有空格字符的 excelsheet 名称命名为 paas 呢?