更新值后保存Excel文件时出错

时间:2015-06-24 06:09:38

标签: c# .net excel excel-2007

我正在使用Excel Interop库来读取和编写Excel工作表。当我尝试按Save()方法编写并保存工作簿时,

  

HRESULT的异常:0x800A03EC

虽然Saveas()SaveCopyAs()方法工作正常。我想更新用于读取的同一文件。以下是我的代码: -

 excelApplication = new Excel.Application();
                        excelApplication.DisplayAlerts = false;
                        // excelApplication.ScreenUpdating = true;
                        excelApplication.Visible = false;
                        excelWorkbook = excelApplication.Workbooks.Open(filePath,
                        Type.Missing, false, Type.Missing, Type.Missing,
                        Type.Missing, true, Type.Missing, Type.Missing,
                        true, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlExtractData);

                        dict = new Dictionary<string, Excel.Worksheet>();

                        foreach (Excel.Worksheet worksheet in excelWorkbook.Worksheets)
                        {
                            dict.Add(worksheet.Name, worksheet);
                            Excel.Worksheet sheet = dict[worksheet.Name];
                        }
                        Excel.Worksheet sheetobj = dict["Risk Register"];
                         //sheetobj.Cells[10, 4]
                        ((Excel.Range)sheetobj.Cells[10, 4]).Value2 =123;

                        excelWorkbook.Save();

0 个答案:

没有答案