为什么本应保存的文件并没有真正保存?
Why does the supposedly-saved file not really get saved?
以下代码取自 this tutorial,用于使用 C# 创建和保存 Excel 文件:
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsFormsAppExcelTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonCreateExcelFile_Click(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
xlWorkBook.SaveAs("csharp-Excel.xls",
Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file c:\csharp-Excel.xls");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
} // class
} // namespace
它似乎 运行 很好 -- 我看到了 MessageBox 信息,我可以逐步完成它,没有任何问题的迹象。但是该文件并没有像它应该的那样保存到硬盘驱动器。为什么不呢?
保存到特定的非根位置(子文件夹)有效,例如这个:
String savefullpath = @"C:\misc\csharpExcelTest.xls";
xlWorkBook.SaveAs(savefullpath,
Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
文件确实保存到那个地方。
显然唯一的问题是试图保存到 C/root。甚至尝试显式保存到 root,如下所示:
String savefullpath = @"C:\csharpExcelTest.xls";
...(而不是简单地提供一个裸文件名)可耻地失败了,告诉我:
System.Runtime.InteropServices.COMException was unhandled
HelpLink=C:\Program Files (x86)\Microsoft Office\Office1233\XLMAIN11.CHM
HResult=-2146827284
Message=Microsoft Office Excel cannot access the file 'C:'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
. . .
所以:保存到root以外的地方可以避免这个问题。
更新
此外,当我没有提供 folder/full 路径时,它似乎被保存到我的 "Documents library",并假设它会保存到 C:。我今天早上刚好查看那个文件夹,在那里看到了文件。
以下代码取自 this tutorial,用于使用 C# 创建和保存 Excel 文件:
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsFormsAppExcelTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonCreateExcelFile_Click(object sender, EventArgs e)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "http://csharp.net-informations.com";
xlWorkBook.SaveAs("csharp-Excel.xls",
Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file c:\csharp-Excel.xls");
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
} // class
} // namespace
它似乎 运行 很好 -- 我看到了 MessageBox 信息,我可以逐步完成它,没有任何问题的迹象。但是该文件并没有像它应该的那样保存到硬盘驱动器。为什么不呢?
保存到特定的非根位置(子文件夹)有效,例如这个:
String savefullpath = @"C:\misc\csharpExcelTest.xls";
xlWorkBook.SaveAs(savefullpath,
Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
文件确实保存到那个地方。
显然唯一的问题是试图保存到 C/root。甚至尝试显式保存到 root,如下所示:
String savefullpath = @"C:\csharpExcelTest.xls";
...(而不是简单地提供一个裸文件名)可耻地失败了,告诉我:
System.Runtime.InteropServices.COMException was unhandled
HelpLink=C:\Program Files (x86)\Microsoft Office\Office1233\XLMAIN11.CHM
HResult=-2146827284
Message=Microsoft Office Excel cannot access the file 'C:'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
. . .
所以:保存到root以外的地方可以避免这个问题。
更新
此外,当我没有提供 folder/full 路径时,它似乎被保存到我的 "Documents library",并假设它会保存到 C:。我今天早上刚好查看那个文件夹,在那里看到了文件。