我正在使用WindowsForms并编写代码,该代码将:
我在参考资料中有六本Excel工作簿,它们都都名为excelTest1.xlsx到excelTest6.xlsm,并且代码运行良好。然后,我需要将excelTest2.xlsx转换为excelTest2.xlsm。
我做到了,将新文件添加到资源中,并删除了旧文件,并在分配方法中更改了名称,但是现在当我运行它时,它仅对.xlsm抛出NullExceptionReference。其他的都正常工作。
以下是代码:
分配方法
private string AssignFileName(int buttonNumber)
{
string fileName = "";
switch (buttonNumber)
{
case 1:
fileName = "excelTest.xlsx";
break;
case 2:
fileName = "excelTest2.xlsm";
break;
case 3:
fileName = "excelTest3.xlsx";
break;
case 4:
fileName = "excelTest4.xlsx";
break;
case 5:
fileName = "excelTest5.xlsx";
break;
case 6:
fileName = "excelTest6.xlsx";
break;
}
return fileName;
}
创建方法
private void CreateFile(string fileName)
{
Assembly asm = Assembly.GetExecutingAssembly();
Stream s = asm.GetManifestResourceStream("WindowsFormsApplication1.Resources." + fileName);
try
{
FileStream fs = new FileStream("C:\\CLDB\\" + fileName, FileMode.OpenOrCreate);
s.CopyTo(fs);
fs.Close();
}
catch (System.IO.DirectoryNotFoundException exception)
{
Directory.CreateDirectory("C:\\CLDB");
FileStream fs = new FileStream("C:\\CLDB\\" + fileName, FileMode.OpenOrCreate);
s.CopyTo(fs);
fs.Close();
}
s.Close();
}
该异常发生在.CopyTo(fs);
。我似乎无法理解问题。任何帮助将不胜感激!