C#微软excel 2003年移动表

我正在处理一个SSIS包C#脚本任务,我正在将.xls文件转换为.csv文件,我遇到了这个问题,只有一张纸只写

string fileFullPath = ""; //Get one Book(Excel file at a time) foreach (FileInfo file in files) { string filename = ""; fileFullPath = SourceFolderPath + "\\" + file.Name; filename = file.Name.Replace(".xls", ""); //MessageBox.Show(fileFullPath); //Create Excel Connection string ConStr; string HDR; HDR = "YES"; ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileFullPath + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=1\""; OleDbConnection cnn = new OleDbConnection(ConStr); bool isDouble; double dbl; //Get Sheet Name cnn.Open(); DataTable dtSheet = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); string sheetname; sheetname = ""; foreach (DataRow drSheet in dtSheet.Rows) { if (drSheet["TABLE_NAME"].ToString().Contains("$")) { sheetname = drSheet["TABLE_NAME"].ToString(); //Display Sheet Name , you can comment it out // MessageBox.Show(sheetname); //Load the DataTable with Sheet Data OleDbCommand oconn = new OleDbCommand("select * from [" + sheetname + "]", cnn); //cnn.Open(); OleDbDataAdapter adp = new OleDbDataAdapter(oconn); DataTable dt = new DataTable(); adp.Fill(dt); //drop $from sheet name sheetname = sheetname.Replace("$", ""); //Create CSV File and load data to it from Sheet StreamWriter sw = new StreamWriter(DestinationFolderPath + "\\" + filename + "_" + sheetname + ".csv", false); int ColumnCount = dt.Columns.Count; string[] columnName = new string[ColumnCount]; // Write the Header Row to File for (int i = 0; i < ColumnCount; i++) { sw.Write(dt.Columns[i]); columnName[i] = dt.Columns[i].ToString(); if (i < ColumnCount - 1) { sw.Write(FileDelimited); } } sw.Write(sw.NewLine); // Write All Rows to the File foreach (DataRow dr in dt.Rows) { for (int i = 0; i < ColumnCount; i++) { if (!Convert.IsDBNull(dr[i])) { if (columnName[i] == "DATE_TIME") { isDouble = Double.TryParse(dr[i].ToString(), out dbl); if (isDouble) { sw.Write(DateTime.FromOADate(dbl)); } else { sw.Write(dr[i].ToString()); } } else { sw.Write(dr[i].ToString()); } } if (i < ColumnCount - 1) { sw.Write(FileDelimited); } } sw.Write(sw.NewLine); } sw.Close(); } } cnn.Close(); } 

第一张纸成功写入,但在第二次迭代sheetname不会改变,我encoutnered此错误The Microsoft Jet database engine could not find the object 'surot$_'. Make sure the object exists and that you spell its name and the path name correctly. The Microsoft Jet database engine could not find the object 'surot$_'. Make sure the object exists and that you spell its name and the path name correctly. 上线adp.Fill(dt); 它仍然访问表surot$甚至完成了surot$的csv文件的创build

编辑1:当我检查dtsheet的行数包含6而不是3为什么会发生这种情况? 也是命名成surot$,surot$_ ,好像它复制了excel文件。

我有一个类似的问题。

检查IF语句中最后一个字符是否不是下划线“_”。

由于某种原因,它总是这样倍增。

尝试按照MSDN获取架构,方法是在参数中指定第二个参数,如下所示:

 DataTable dtSheet = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });