locked
Edit an existing Excel sheet RRS feed

  • Question

  • User2056967973 posted

    I need to open an excel template, edit it, and save it as a new file.  I've never worked with excel before so I'm stunbling around.  here's what I have so far:

     

    Excel.Application ExcelApp = new Excel.ApplicationClass();
    
    ExcelApp.Visible = true;
    
    string workbookPath="template.xls";
    
    Excel.Workbook excelWorkbook = ExcelApp.Workbooks.Open(workbookPath,0,false,5,"","",false,Excel.xlPlatform.xlWindows,"",true,false,0,true,false,false);
    
    Excel.Sheets excelSheets=excelWorkbook.Worksheets;
    
    string currentSheet="Sheet1";
    
    Excel.Worksheet excelWorksheet = (Excel.Worksheet) excelSheets.get_Item(currentSheet);
    
    excelWorksheet.Cells[1,1]="Test";
    
    excelApp.Workbooks.Close();
    
    


     

    Excel.Application ExcelApp = new Excel.ApplicationClass();
    
    ExcelApp.Visible = true;
    
    string workbookPath="template.xls";
    
    Excel.Workbook excelWorkbook = ExcelApp.Workbooks.Open(workbookPath,0,false,5,"","",false,Excel.xlPlatform.xlWindows,"",true,false,0,true,false,false);
    
    Excel.Sheets excelSheets=excelWorkbook.Worksheets;
    
    string currentSheet="Sheet1";
    
    Excel.Worksheet excelWorksheet = (Excel.Worksheet) excelSheets.get_Item(currentSheet);
    
    excelWorksheet.Cells[1,1]="Test";
    
    excelApp.Workbooks.Close();
    
    


    So I don't know if this is editing anything, but I do know it is not closing the sheet because I can't manually open it after I run this.  And I need to save it as something new like "Output.xls"

     

    Thanks,

    Saturday, June 5, 2010 7:04 AM

Answers

  • User-2139482507 posted

    Hi,

    I think you need to call SaveAs. Here's an example:

                wkBook.SaveAs(
                    "myfile.xls", Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value,
                    Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value);
    

    Joe

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 5, 2010 3:04 PM
  • User-1199946673 posted

    As already mentioned so many times, you shouldn't use Microsoft.Office.Interop in a web environment!

    "Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment."

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2 

    Instead, try

    http://npoi.codeplex.com/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 5, 2010 6:11 PM

All replies

  • User-2139482507 posted

    Hi,

    I think you need to call SaveAs. Here's an example:

                wkBook.SaveAs(
                    "myfile.xls", Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value,
                    Excel.XlSaveAsAccessMode.xlShared, Missing.Value, Missing.Value,
                    Missing.Value, Missing.Value, Missing.Value);
    

    Joe

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 5, 2010 3:04 PM
  • User-1199946673 posted

    As already mentioned so many times, you shouldn't use Microsoft.Office.Interop in a web environment!

    "Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment."

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2 

    Instead, try

    http://npoi.codeplex.com/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, June 5, 2010 6:11 PM