Excerpt |
---|
ExcelApplication can open and modify an existing spreadsheet. The new spreadsheet can either be saved to disk (overwriting the original file or saved with a new name) or streamed to the browser. This allows you to use a preset format for similar spreadsheets, rather than recreate the format for each. |
Alternatively, use an ExcelWriter template.
...
To modify an existing spreadsheet:
Create an instance of ExcelApplication:
Code Block c# c# ExcelApplication xla = new ExcelApplication();
Open an Excel workbook from a file path or a System.IO.Stream:
Code Block c# c# //--- Open a workbook from a file path: Workbook wb = xla.Open(@"C:\Reports\Report.xlsx");
Or:
Code Block c# c# //--- Open a workbook from a Stream: FileStream fs = new FileStream(@"C:\Sales\2003\June.xlsx", FileMode.Open); Workbook wb = xla.Open(fs);
Return a Worksheet object:
Code Block c# c# Worksheet ws = wb.Worksheet[0];
Modify the worksheet. For example, add a cell value:
Code Block c# c# ws.Cells[0, 0].Value = "Welcome to SoftArtisans OfficeWriter";
Save the modified file with a new name, or stream it to the client:
Code Block c# c# //--- Save the file on the server: xla.Save(wb, @"C:\temp\out.xlsx");
Or:
Code Block c# c# //--- Stream the file to the client via HTTP: //--- false means Open In Excel, true means Open In Browser (IE only) xla.Save(wb, Page.Response, "out.xlsx", false);
For more information on save options, see Output Options.
\