Open xml sdk excel tutorial. When creating an excel file with open xml sdk theres no need to install microsoft office so this library can be used without excel installation. In order to compile the following code you will need the microsoftopenxml sdk 20 documentformatopenxml nuget package and reference to microsoftofficeinteropexcel used for opening the excel app to recalculate formulas. Stylesheet can accept different elements such as borders colors fills and etc. Stylesheet class is used to add custom style to a spreadsheet.
As its child elements which specifies the look of a spreadsheet. Its main features are. If you have more than 1 sheet it only reads the first sheet and the generated excel has only the data of the first sheet of the excel file uploaded. Hence in this example i am going to show how to get the basic four types of important data such as excel work book name worksheet count in that workbook name of the worksheets and finally the value of the first cell in that worksheet.
In this tutorial we are going work with our sample in the part 2 and we are going to add style and do some customization on that. For this add openxml from nuget packages by right clicking the project and selecting manage nuget package and search openxml. From the list select documentformatopenxml as shown below and install it. Office interop needs excel driver to be installed on the machine which is using it.
You start with creating a spreadsheetdocument which represents an excel document. A few days back i got a requirement to read ms excel files and store those values in the sql server database. Using the classes in the open xml sdk using the classes in the open xml sdk 25 is simple. The open xml sdk 25 encapsulates many common tasks that developers perform on open xml packages so that you can perform complex operations with just a few lines of code.
This article presents an example on how to read and write microsoft excel with open xml sdk. The open xml sdk 25 productivity tool for microsoft office openxmlsdktoolexe does just that. The excel utility classes in the article only process the first sheet of the excel file. With openxml you are simply constructing an xml structure which represents an excel file.
See the structure of a file which xml parts does it contain navigate the xml in each of these parts. So lets start writing functions for creating an excel file using openxml. Then in your project or application add references to the following components.