C# HRESULT 异常:0x800A03EC 保存 Excel 文件时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15597490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception from HRESULT: 0x800A03EC Error while saving Excel file
提问by user1594950
I am saving data on button's click event and below is code:
我正在保存按钮点击事件的数据,下面是代码:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
object misValue = System.Reflection.Missing.Value;
String st = System.IO.Directory.GetCurrentDirectory() + "\A.xlsx";
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Open(st, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
int i = 6;
for (i = 6; i < 10; i++)
{
xlWorkBook.SaveAs(st, XlFileFormat.xlExcel9795, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);
MessageBox.Show(xlWorkSheet.get_Range("L" + @i, "L" + @i).Value2.ToString());
}
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
When I am saving it, it gives me error:
当我保存它时,它给了我错误:
HRESULT: 0x800A03EC Error while saving Excel file
HRESULT: 0x800A03EC 保存 Excel 文件时出错
回答by Daniil
As I understand at Saving an Excel File Exception from HRESULT: 0x800A03ECException raised when arguments of method SaveAs are wrong. Please review your arguments at:
正如我在从 HRESULT 保存 Excel 文件异常中所理解的:0x800A03EC当方法 SaveAs 的参数错误时引发异常。请在以下位置查看您的论点:
xlWorkBook.SaveAs(st1, XlFileFormat.xlExcel9795, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);
回答by JMK
@Sebastian is correct in that you are calling SaveAs four times, saving to the same location without closing. This isn't going to work, you need to first of all move this out of the loop. But looking at your code more closely, you aren't changing anything in the workbook, so there is no need to save, and if you did change something, you would be better calling Saveinstead of SaveAs. As well as this, you are specifying ReadOnlyas true when you are opening the workbook, so attempting to call save in any capacity isn't going to work.
@Sebastian 是正确的,因为您四次调用 SaveAs,保存到同一位置而不关闭。这是行不通的,您首先需要将其移出循环。但是更仔细地查看您的代码,您不会更改工作簿中的任何内容,因此无需保存,如果您确实更改了某些内容,最好调用Save而不是 SaveAs。除此之外,您在打开工作簿时将ReadOnly指定为 true,因此尝试以任何容量调用 save 都不行。
Finally, if you are using >= C# 4, you can use optional parameters, so all of those misValue's are unnecessary. I tidied up your code below:
最后,如果您使用的是 >= C# 4,您可以使用可选参数,因此所有这些misValue都是不必要的。我整理了下面的代码:
using Excel = Microsoft.Office.Interop.Excel;
var st = System.IO.Directory.GetCurrentDirectory() + "\A.xlsx";
var xlApp = new Excel.Application();
var xlWorkBook = xlApp.Workbooks.Open(st);
var xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.Item[1];
for (var i = 6; i < 10; i++)
{
MessageBox.Show(xlWorkSheet.Range["L" + @i, "L" + @i].Value2.ToString());
}
//make some changes here
xlWorkBook.Save();
xlWorkBook.Close();
xlApp.Quit();
回答by user3052469
check for cell indices for sheet , starts from [1,1] sheet.cells[0,0] will throw com error.
检查 sheet 的单元格索引,从 [1,1] sheet.cells[0,0] 开始会抛出 com 错误。