The simplest and fastest way to create an Excel file from C# is to use the Open XML Productivity ToolOpen XML Productivity Tool. The Open XML Productivity Tool comes with the Open XML SDK installation. The tool reverse engineers any Excel file into C# code. The C# code can then be used to re-generate that file.
An overview of the process involved is:
- Install the Open XML SDK with the tool Install the Open XML SDK with the tool.
- Create an Excel file using the latest Excel client with desired look. Name it
DesiredLook.xlsx
. - With the tool open
DesiredLook.xlsx
and click the Reflect Code button near the top. - The C# code for your file will be generated in the right pane of the tool. Add this to your C# solution and generate files with that desired look.
As a bonus, this method works for any Word and PowerPoint files. As the C# developer, you will then make changes to the code to fit your needs.
I have developed a simple WPF app on github which will run on Windows for this purpose. There is a placeholder class called GeneratedClass
where you can paste the generated code. If you go back one version of the file, it will generate an excel file like this: