How to Read and Write to OpenXML files from your .NET applications?
Following example shows how to read or write to OpenXML files by using only GemBox.Spreadsheet .NET component.
GemBox.Spreadsheet is a .NET component which provides easy and high performance
way to write, read or convert native Microsoft Excel files (XLS, CSV or XLSX) and
HTML files without the need for Microsoft Excel on either the developer or client
machines. GemBox.Spreadsheet Free comes free of charge while GemBox.Spreadsheet
Professional is a commercial version (licensed per developer). Find more information
about GemBox.Spreadsheet features or reasons why our component is better then Excel Automation.
Example reads OpenXML file, changes the value of the cell "A1" and writes the new
file:
|
ExcelFile ef =
new ExcelFile();
// Loads OpenXML file.
ef.LoadXlsx("filename.xlsx",
XlsxOptions.None);
// Selects first worksheet.
ExcelWorksheet ws = ef.Worksheets[0];
// Change the value of the cell "A1".
ws.Cells["A1"].Value = "Hello world!";
// Saves the file in OpenXML format.
ef.SaveXlsx("NewFile.xlsx");
|
|
Dim ef
As New ExcelFile
' Loads OpenXML file.
ef.LoadXlsx("filename.xlsx",
XlsxOptions.None)
' Selects first worksheet.
Dim ws As
ExcelWorksheet = ef.Worksheets(0)
' Change the value of the cell "A1".
ws.Cells("A1").Value = "Hello world!"
' Saves the file in OpenXML format.
ef.SaveXlsx("NewFile.xlsx")
|
To read/write the file to another format you only need to replace the LoadXls/SaveXls
method with preferred one (LoadXlsx/SaveXlsx, LoadCsv/SaveCsv or SaveHtml).
|