c# Excel 互操作 _Workbooks.Open
c# Excel Interop _Workbooks.Open
我正在 Microsoft Visual Studio 中编写一个 c# WinForms 应用程序。我们的数据团队每天给我一份 .csv 格式的报告,我需要直观地显示结果。然而,这个程序最初是为处理 .xlsx 文件而编写的,所以我决定将文件转换为 .xlsx 然后保留其余代码,而不是重写我的所有代码来处理 .csv,这样会更容易.
因此,我修改了 drawCharts() 方法的第一部分,该方法负责从 xlsx 收集信息并将其呈现为 excel 图表。
我的研究向我展示了两种将外部文件读入 Excel 工作簿的方法,第一种使用 Workbooks.Open,第二种使用 Workbooks.OpenText。
这是我的代码:
private void drawCharts()
{
//Declare variables for use
//These first three values are going to be used later and can be ignored.
Excel.Application xlApp = null;
Excel.Workbook xlWorkbook = null;
Excel.Worksheet xlWorksheet = null;
object misValue = System.Reflection.Missing.Value;
string path = "\\Data\Departmental Data\TechSupport\_Report"
//define the actual file path. Separate because I use the path variable elsewhere
string source = Path.Combine(path, "report.csv");
//define the destination file. Using Environment.UserName so multiple people can run this program at the same time, all using unique files except when they first load. There's probably a better way of doing this.
string use = Path.Combine(path, "Report-" + Environment.UserName + ".xlsx");
//A boolean telling the program to exit if there was an error
bool shouldExit = false;
Excel.Application app = null; //Temporary application used for opening the csv
try
{
MessageBox.Show("Opening file " + source);
app = new Excel.Application();
Excel.Workbook wb = app.Workbooks.Open(source); //source points to the csv. This is the line that fails.
MessageBox.Show(String.Format("Saving {0} as {1}", source, use));
wb.SaveAs(use, Excel.XlFileFormat.xlOpenXMLWorkbook);
wb.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
shouldExit = true;
}
finally
{
if (app != null) { app.Quit(); }
releaseObject(app);
if (shouldExit) { Environment.Exit(1); }
}
//Rest of the code...
}
违规行是 Excel.Workbook wb = app.Workbooks.Open(source);
。执行此行时,Excel 会抛出一个错误,指出无法打开该文件,因为找不到它。对此进行研究,我发现 this article 说我应该将其更改为 Excel.Workbook wb = app.Workbooks.OpenText(source)
。但是,这只会导致编译器错误,指出 Cannot implicitly convert type 'void' to 'Microsoft.Office.Interop.Excel.Workbook'
.
如有任何想法,我们将不胜感激。
所以,经过一番研究,我发现了我的错误。文件路径已更改,因为 .csv 所在的文件夹已重命名。在检查文件路径是否正确时,我一直在我的测试目录而不是生产目录中查找,所以一切看起来都是正确的。
总是小事....
我正在 Microsoft Visual Studio 中编写一个 c# WinForms 应用程序。我们的数据团队每天给我一份 .csv 格式的报告,我需要直观地显示结果。然而,这个程序最初是为处理 .xlsx 文件而编写的,所以我决定将文件转换为 .xlsx 然后保留其余代码,而不是重写我的所有代码来处理 .csv,这样会更容易.
因此,我修改了 drawCharts() 方法的第一部分,该方法负责从 xlsx 收集信息并将其呈现为 excel 图表。
我的研究向我展示了两种将外部文件读入 Excel 工作簿的方法,第一种使用 Workbooks.Open,第二种使用 Workbooks.OpenText。
这是我的代码:
private void drawCharts()
{
//Declare variables for use
//These first three values are going to be used later and can be ignored.
Excel.Application xlApp = null;
Excel.Workbook xlWorkbook = null;
Excel.Worksheet xlWorksheet = null;
object misValue = System.Reflection.Missing.Value;
string path = "\\Data\Departmental Data\TechSupport\_Report"
//define the actual file path. Separate because I use the path variable elsewhere
string source = Path.Combine(path, "report.csv");
//define the destination file. Using Environment.UserName so multiple people can run this program at the same time, all using unique files except when they first load. There's probably a better way of doing this.
string use = Path.Combine(path, "Report-" + Environment.UserName + ".xlsx");
//A boolean telling the program to exit if there was an error
bool shouldExit = false;
Excel.Application app = null; //Temporary application used for opening the csv
try
{
MessageBox.Show("Opening file " + source);
app = new Excel.Application();
Excel.Workbook wb = app.Workbooks.Open(source); //source points to the csv. This is the line that fails.
MessageBox.Show(String.Format("Saving {0} as {1}", source, use));
wb.SaveAs(use, Excel.XlFileFormat.xlOpenXMLWorkbook);
wb.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
shouldExit = true;
}
finally
{
if (app != null) { app.Quit(); }
releaseObject(app);
if (shouldExit) { Environment.Exit(1); }
}
//Rest of the code...
}
违规行是 Excel.Workbook wb = app.Workbooks.Open(source);
。执行此行时,Excel 会抛出一个错误,指出无法打开该文件,因为找不到它。对此进行研究,我发现 this article 说我应该将其更改为 Excel.Workbook wb = app.Workbooks.OpenText(source)
。但是,这只会导致编译器错误,指出 Cannot implicitly convert type 'void' to 'Microsoft.Office.Interop.Excel.Workbook'
.
如有任何想法,我们将不胜感激。
所以,经过一番研究,我发现了我的错误。文件路径已更改,因为 .csv 所在的文件夹已重命名。在检查文件路径是否正确时,我一直在我的测试目录而不是生产目录中查找,所以一切看起来都是正确的。
总是小事....