シートを移動すると、私は、ファイルを.csvに.xlsファイルを変換していますが、私は一枚だけしか書かれているこの問題が発生したのC#のMicrosoft Excel 2003私はSSISパッケージのC#スクリプトタスクに取り組んでいます
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書かれたが、2回目の反復にシート名が変更されないし、私はそれがまだシートsurot$
にアクセスadp.Fill(dt);
行にこのエラー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.
をencoutneredもそれはsurot$
EDIT 1用のCSVファイルの作成が完了しました:の行数をチェックすると3
の代わりに6
が含まれているのはなぜですか?またその名前はsurot$,surot$_
になります。それはExcelファイルを複製するようです。
DataTable dtSheet = cnn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new object[] { null, null, null, "TABLE" });