0

I have made a code in C# which creates an excel file. Here is the code

    Excel.Application xlApp;
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;

    xlApp = new Excel.ApplicationClass();
    xlWorkBook = xlApp.Workbooks.Add(misValue);
    xlWorkBook.CheckCompatibility = false;
    xlWorkBook.DoNotPromptForConvert = true;

    xlApp.Visible = true; // ensure that the excel app is visible.
    xlWorkSheet = (Excel.Worksheet)xlApp.ActiveSheet; // Get the current active worksheet.
    Microsoft.Office.Interop.Excel.Worksheet worksheet2 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2); //Get more work sheet if neccessary

    xlWorkSheet.Cells[3, 1] = "Name";

    xlWorkBook.SaveAs(filepath + fileName, 51, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);  //For excel 2007 and above
    xlWorkBook.Close(true, misValue, misValue);
    xlApp.Quit();

Now, the problem is that when i open this file again, then i see that the "Merge & Center" option remains disabled. So, when i opens this file through C# code, then i cannot merge the cells. How can i achieve that ?

Thanks

Rohan
  • 304
  • 4
  • 16
  • I recommend you to use EPPlus library for excel data manipulations, it's easy to use and it works faster. You can download it [here](https://epplus.codeplex.com/releases/view/89923) Merging example for EPPlus described [here](http://stackoverflow.com/questions/6172488/merge-cells-using-epplus) – Fragment Feb 09 '15 at 08:26
  • `new Excel.ApplicationClass();`? no `new Excel.Application();`? – dovid Feb 09 '15 at 08:34

1 Answers1

1

this is becuse you use Excel.XlSaveAsAccessMode.xlShared. This applies to many restrictions, see here. use instead Excel.XlSaveAsAccessMode.xlNoChange:

xlWorkBook.SaveAs(filepath + fileName, 51, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlNoChange, misValue, misValue, misValue, misValue, misValue);  
dovid
  • 6,354
  • 3
  • 33
  • 73