locked
how to open excel ,update specific cell value and save it as pdf RRS feed

  • Question

  • User-102500655 posted

    hi ,

    i am trying to edit excel in c# and then export it as pdf but it is not updating my excel.

    here is the code

     string paramExportFilePath = Server.MapPath(@"./test.pdf");
            XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF;
            XlFixedFormatQuality paramExportQuality =     XlFixedFormatQuality.xlQualityStandard;
    
            bool paramOpenAfterPublish = false;
            bool paramIncludeDocProps = true;
            bool paramIgnorePrintAreas = true;
            object paramFromPage = Type.Missing;
            object paramToPage = Type.Missing;
            object paramMissing = Type.Missing;
    
    
            string XlsPath = "";
           Worksheet oSheet;
            XlsPath = Server.MapPath(@"./G1_Field.xls");
            object _missingValue = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            Workbook theWorkbook = excel.Workbooks.Open(XlsPath,_missingValue, false, _missingValue, _missingValue, _missingValue, true, _missingValue, _missingValue, true, _missingValue, _missingValue, _missingValue,_missingValue,_missingValue);
            Sheets sheets = (Sheets)theWorkbook.Worksheets;
            oSheet = (Worksheet)sheets[1];
            
            oSheet.Cells[5, 8] = "80000";
            oSheet.Cells[5, 3] = "shamila";
    
           // theWorkbook.SaveAs("C:\\MyExcelTestTest.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, false, System.Reflection.Missing.Value, false, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
    
            //theWorkbook.RefreshAll();
            //string tmpName = Path.GetTempFileName();
            //File.Delete(tmpName);
            //theWorkbook.SaveAs("C:\\test.pdf", paramExportFormat, _missingValue, _missingValue, _missingValue, _missingValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, _missingValue, _missingValue, _missingValue, _missingValue, _missingValue);
            //theWorkbook.Close(false, _missingValue, _missingValue); 
            //excel.Quit();
            //File.Delete(XlsPath);
            //File.Move(tmpName, XlsPath);
    
    
            if (theWorkbook != null)
            {
                theWorkbook.ExportAsFixedFormat(paramExportFormat,
                         paramExportFilePath, paramExportQuality,
                         paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage,
                         paramToPage, paramOpenAfterPublish,
                         paramMissing);
    
    
            }

    now  oSheet.Cells[5, 3] = "shamila"; this line works but  oSheet.Cells[5, 8] = "80000"; 
     is not working...where i am calculating on the bases of cell[5,8] value.

    please reply


    Monday, March 11, 2013 6:33 AM

Answers