Hello Devz,
It is common to have to export data to an Excel file. But sometimes the code have to run on a server where Excel is not or can’t be installed. And anyway, to be honest the Excel Interop is a real piece of crap to use…
So Aspose did a good work by redoing it completely without the need of Excel or its Interop. Easy to use and fast, what else…
There is a free version available on NuGet which will add an extra worksheet saying it’s an evaluation version, but you will have all the functionalities and with no time limitation.
using Aspose.Cells; using System.Diagnostics; using System.Drawing; namespace AsposeExportToExcel { class Program { static void Main(string[] args) { //Create the workbook and the worksheet Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets.Add("My new sheet"); sheet.Name = "Renamed Sheet"; //Set a title and format its style var cell = sheet.Cells["A1"]; cell.PutValue("This is my column title"); Style style = cell.GetStyle(); style.HorizontalAlignment = TextAlignmentType.Center; style.Font.Size = 15; style.Font.IsBold = true; style.ForegroundColor = Color.AliceBlue; style.Pattern = BackgroundType.Solid; cell.SetStyle(style); //Put some values and format the number with 2 digits cell = sheet.Cells["A2"]; cell.PutValue(123.456); style = cell.GetStyle(); StyleFlag flag = new StyleFlag { NumberFormat = true }; style.Custom = "0.00"; //round the value to 2 digits cell.SetStyle(style, flag); //Set the columns to fit the size of their content sheet.AutoFitColumns(); //Save the Excel workbook var fileName = "test.xlsx"; workbook.Save(fileName, SaveFormat.Xlsx); //Display the Excel file Process.Start(fileName); } } }
Happy coding! 🙂