我整个上午一直在研究,找不到合适的解决方案。我希望有人能告诉我答案。
我让用户使用FileUpload上传.xls文件,然后我想从4张纸中选择所有数据并放入4个单独的数据集。这是我正在使用的代码:
string strConn = "Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" + FileUpload1.PostedFile.FileName;
string query = "SELECT * FROM [{0}]";
DataSet excelDataSet = new DataSet();
OdbcDataAdapter da = new OdbcDataAdapter(query, strConn);
所以我有2个问题,
第一个问题:如何在不知道名字的情况下选择一张纸?上面的代码不起作用,如果我用MyFirstSheet $替换{0},那么它可以工作。我需要知道在没有我知道名字的情况下如何选择。我得到的错误是:
ERROR [42S02] [Microsoft][ODBC Excel Driver] The Microsoft Jet database engine could not find the object '0}'. Make sure the object exists and that you spell its name and the path name correctly.
第二个问题:有没有办法一次选择所有4个并将它们放入4个单独的数据集中?目前我正计划4次阅读该文件。
建议代码:
String connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileUpload1.PostedFile.FileName.ToString() + ";Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\"";
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
String sheet = conn.GetSchema("Tables").Rows[0]["TABLE_NAME"].ToString();
String selectCommand = String.Format(@"SELECT * FROM [{0}]", sheet);
DataSet excelDataSet = new DataSet();
OdbcDataAdapter da = new OdbcDataAdapter(selectCommand, "Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" + FileUpload1.PostedFile.FileName);
da.Fill(excelDataSet);
btnLoadCsv.Text = excelDataSet.Tables[0].Rows[10][1].ToString();
答案 0 :(得分:2)
您可以从工作簿中获取一系列工作表:
OleDbConnection conn = new OleDbConnection("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=" + FileUpload1.PostedFile.FileName);
conn.Open();
StringBuilder sb = new StringBuilder();
foreach (DataRow row in conn.GetSchema("Tables").Rows)
sb.Append(String.Format(@"SELECT * FROM [{0}];", row["TABLE_NAME"]);
OdbcDataAdapter da = new OdbcDataAdapter(sb.ToString(), conn);
DataSet excelDataSet = new DataSet();
da.Fill(excelDataSet);
excelDataSet.Tables[0].Name = "myTable1";
excelDataSet.Tables[1].Name = "myTable2";
excelDataSet.Tables[2].Name = "myTable3";
//etc...however many sheets you got from Excel (you could loop this too)
此示例遍历架构中“Tables”的行,这等同于工作簿中的工作表。使用复合SQL SELECT
,您可以将所有表一次性放入DataSet
,您可以通过索引(excelDataSet.Tables[0]
)引用,或者如果指定一个,则可以通过名称引用。
答案 1 :(得分:0)
为什么不尝试excel数据阅读器,这是一个关于codeplex的开源项目?您可以在此处查看/下载库http://exceldatareader.codeplex.com/。您需要做的是从FileUploader获取流,并将其传递给库。
以下是所需代码的示例摘录
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read); //1. Reading from a binary Excel file ('97-2003 format; *.xls) IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream); //... //2. Reading from a OpenXml Excel file (2007 format; *.xlsx) IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); //... //3. DataSet - The result of each spreadsheet will be created in the result.Tables DataSet result = excelReader.AsDataSet(); //... //4. DataSet - Create column names from first row excelReader.IsFirstRowAsColumnNames = true; DataSet result = excelReader.AsDataSet(); //5. Data Reader methods while (excelReader.Read()) { //excelReader.GetInt32(0); } //6. Free resources (IExcelDataReader is IDisposable) excelReader.Close();
下面是将所有工作表加载到一个数据集中的多个数据表中的代码。
//3. DataSet - The result of each spreadsheet will be created in the result.Tables DataSet result = excelReader.AsDataSet();