I am saving data on button\'s click event and below is code:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp;
Excel.Workbook xlWorkBoo
As I understand at Saving an Excel File Exception from HRESULT: 0x800A03EC Exception raised when arguments of method SaveAs are wrong. Please review your arguments at:
xlWorkBook.SaveAs(st1, XlFileFormat.xlExcel9795, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);
check for cell indices for sheet , starts from [1,1] sheet.cells[0,0] will throw com error.
@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 Save instead of SaveAs. As well as this, you are specifying ReadOnly as true when you are opening the workbook, so attempting to call save in any capacity isn't going to work.
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:
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();
I know this thread is old, but this may be of help to somebody. I had the same problem, and calling the Activate() function on the workbook fixed it for me:
yourWorkBookObject.Activate()