我在SSIS包C#腳本任務的工作,我將.xls文件爲.csv文件,我遇到了這個問題,只有一張紙只寫C#的Microsoft Excel 2003移動通過表
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();
}
第一板successfuly寫,但在第二次迭代的SHEETNAME不會改變我對adp.Fill(dt);
它仍在訪問表surot$
行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.
即使它完成了surot$
EDIT 1 csv文件的創建:當我檢查的行數時它包含6
而不是3
爲什麼會發生這種情況?也是命名變得surot$,surot$_
它看起來好像它重複的Excel文件。