5

I'm trying to modify and save data in Excel. With the code below, I'm accessing the sheet, performing modifications, then saving the file. I'm unable to save the file. Here is my code:

Application excel = new Application();
        excel.Visible=true;
        Workbook wb = (Workbook)excel.Workbooks.Open(@"C:\Users\dnyanesh.wagh\Desktop\BookExcel1.xlsx");
        Worksheet ws = (Worksheet)wb.Worksheets[1];
        ws.Cells[1, 1] = "sagar";
        ws.Cells[2, 1] = "sagar";
        ws.Cells[3, 1] = "sagar";
        wb.Save();
        wb.close();

I'm receiving this error: "the file named 'BookExcel1.xlsx' already exists in this location. Do you want to replace it?"

So I changed the code to:

Workbook wb = (Workbook)excel.Workbooks.Open(@"C:\Users\dnyanesh.wagh\Desktop\BookExcel1.xlsx",0, false, 5, "", "",
            false, XlPlatform.xlWindows, "", true, false,
            0, true, false, false););

Then error is: "BookExcel1.xlsx is being modified by user_name.open as read only". If I click the 'cancel' button, I receive the exception above with "Exception from HRESULT: 0x800A03EC"

I have also tried:

wb.SaveAs(@"C:\Users\dnyanesh.wagh\Desktop\BookExcel1.xlsx");
wb.Close(true,null,null);

From that I receive the same error, with the above file showing the modifications.

Can anybody tell me how can I save the file with modifications?

MAbraham1
  • 1,534
  • 4
  • 25
  • 40
dnyaneshwar
  • 1,076
  • 3
  • 12
  • 26
  • Don't really know, but just a quick thought... are you sure it is ok to use the same file path with `SaveAs`? Usually this creates a new document. Is there a `Save()` function instead you could use? – musefan Aug 27 '13 at 12:38
  • Don't make it visible. – Hans Passant Aug 27 '13 at 12:38
  • 1
    I don't see you releasing COM objects created.. Remember to always release every single Excel object you create/use. If you get an error and don't close workbook, that could remain opened, so readonly for new instances... More: excel is not closed (using `excel.Quit()`) so you can have a lot of Excel processes in memory... – Marco Aug 27 '13 at 12:40
  • @musefan i try it,firstly but not able to save it.and even i try to make it invisible but still getting error ''BookExcel1.xlsx' is read-only. To save a copy, click OK, then give the workbook a new name in the Save As dialog box.' – dnyaneshwar Aug 27 '13 at 12:41
  • @Marco i try it,still not able to save it. – dnyaneshwar Aug 27 '13 at 12:44
  • what @Marco said, open up task manager and check if you already have other Excel.exe processes running. Also, it doesn't matter if it's visable or not. – Alex Aug 27 '13 at 13:38

3 Answers3

3

Check that you don't already have an Excel.exe process runnning. Also, you should open the workbook so that it's editable.

This code works:

string txtLocation = Path.GetFullPath(InputFile);

object _missingValue = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application excel = new   Microsoft.Office.Interop.Excel.Application();
Excel.Workbook theWorkbook = excel.Workbooks.Open(txtLocation,
                                                        _missingValue,
                                                        false,
                                                        _missingValue,
                                                        _missingValue,
                                                        _missingValue,
                                                        true,
                                                        _missingValue,
                                                        _missingValue,
                                                        true,
                                                        _missingValue,
                                                        _missingValue,
                                                        _missingValue);

//refresh and calculate to modify
theWorkbook.RefreshAll();
excel.Calculate();
theWorkbook.Save();
theWorkbook.Close(true);
excel.Quit();
Alex
  • 787
  • 1
  • 8
  • 24
1

This happens because the file I was trying to open was already open in excel application. In my code, I was not closing the excel application for some conditions. That's why it was opening it with read only permissions. So I was not able to save that file after updating. To open it with read-write, you have to first close that file. After that, you could open it and easily perform read and write operations on the file.

You can close and see all the excel applications which are already open by using following code:

Application excel = (Application)Marshal.GetActiveObject("Excel.Application");
Workbooks wbs = excel.Workbooks;
foreach (Workbook wb in wbs)
{
    Console.WriteLine(wb.Name); // print the name of excel files that are open
    wb.Save();
    wb.Close();
}
excel.Quit();

At the top, add the following code and make reference to Microsoft.Office.Interop.Excel

using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
Simple Sandman
  • 834
  • 3
  • 10
  • 33
dnyaneshwar
  • 1,076
  • 3
  • 12
  • 26
0

This problem is for backwards compatible sheet (a .xls) instead of a .xlsx.

To allow sheets to be opened in pre office 2007 version it can't contain more than 65k rows. You can check the number of rows in your sheet by using ctrl+arrowdown till you hit the bottom. If you try to get a range larger than that number of rows it will create an error

Community
  • 1
  • 1