public void CreateFileOutput(object parameter)
{
string workSheetName, targetFile;
workSheetName = "data"; targetFile = "data.csv";
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + SourceAppFilePath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1;';";
OleDbConnection conn = null;
StreamWriter wrtr = null;
OleDbCommand cmd = null;
OleDbDataAdapter da = null;
try
{
conn = new OleDbConnection(strConn);
conn.Open();
cmd = new OleDbCommand("SELECT * FROM [" + workSheetName + "$]", conn);
cmd.CommandType = CommandType.Text;
wrtr = new StreamWriter(targetFile);
da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
for (int x = 0; x < dt.Rows.Count; x++)
{
string rowString = "";
for (int y = 0; y < dt.Columns.Count; y++)
{
rowString += "\"" + dt.Rows[x][y].ToString() + "\",";
}
wrtr.WriteLine(rowString);
}
Console.WriteLine();
Console.WriteLine("Done! Your " + SourceAppFilePath + " has been converted into " + targetFile + ".");
Console.WriteLine();
}
catch (Exception exc)
{
Console.WriteLine(exc.ToString());
Console.ReadLine();
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Dispose();
cmd.Dispose();
da.Dispose();
wrtr.Close();
wrtr.Dispose();
}
}
XLS 文件正在转换为 csv。我可以在 for 循环中看到它wrtr.WriteLine(rowString);
但是我想在桌面位置看到最终输出文件“Data.csv”,因为我正在从桌面获取源 .xls 文件。给我一个解决方案。谢谢。