2

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

Koopakiller
  • 2,679
  • 3
  • 27
  • 44
Lata
  • 151
  • 2
  • 3
  • 9

5 Answers5

2

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);
Daniil
  • 403
  • 3
  • 9
2

check for cell indices for sheet , starts from [1,1] sheet.cells[0,0] will throw com error.

1

@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();
JMK
  • 24,985
  • 50
  • 147
  • 268
1

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() 
inspiration
  • 157
  • 3
  • 8
0

I had the same error while saving the Excel file line.

xlWorkBook.SaveAs(st);

I found out the folder name has been changed by someone cause an issue for me to save the file to the given file location at "st". I hope this helps someone to check first.

Grigory Zhadko
  • 860
  • 1
  • 12
  • 22
Krin
  • 1
  • 1