Trouble in Excel.Workbook.SaveAs

眉间皱痕 提交于 2019-12-11 19:01:07

问题


When I save an excel workbook :

xlWorkbook.SaveAs("C:\\output\\excelfile.xls", Excel.XlFileFormat.xlCSV, 
  misValue, misValue, false, false, Excel.XlSaveAsAccessMode.xlNoChange, 
  misValue, misValue, misValue, misValue, misValue);

it have no proplem.

But

String str = "C:\\output\\excelfile.xls";// or get file path from saveFileDialog
xlWorkbook.SaveAs(str, Excel.XlFileFormat.xlCSV,
  misValue, misValue, false, false, Excel.XlSaveAsAccessMode.xlNoChange,
  misValue, misValue, misValue, misValue, misValue);

I found "comexception was unhandled cannot access *.xls", why is that?


回答1:


I just tried it and it works for me:

private void button1_Click(object sender, EventArgs e)
{
    var xlApp = ((TaskPaneView)this.Parent).currentInstance;

    Workbook xlWorkbook = xlApp.Workbooks[1];
    var misValue = Type.Missing;
    xlWorkbook.SaveAs("C:\\temp\\excelfile.xls", XlFileFormat.xlCSV,
misValue, misValue, false, false, XlSaveAsAccessMode.xlNoChange,
misValue, misValue, misValue, misValue, misValue);

    xlWorkbook.SaveAs("C:\\temp\\excelfile.xls", XlFileFormat.xlCSV,
misValue, misValue, false, false, XlSaveAsAccessMode.xlNoChange,
misValue, misValue, misValue, misValue, misValue);

}

On the second SaveAs I get this prompt:



来源:https://stackoverflow.com/questions/16755659/trouble-in-excel-workbook-saveas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!