GemBox Software Home  
 
 
  FAQ
  Help
  Bugs & Features
  Articles  
 



How to write a Report in Excel or HTML files from your .NET applications?

Following example shows how to write a report in Excel or HTML 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 template Excel file (in XLS format), fills it with report data and writes the new file:

C# code:

ExcelFile ef = new ExcelFile();

// Loads the template file.
ef.LoadXls("InvoiceTemplate.xls");

// Selects the first worksheet.
ExcelWorksheet ws = ef.Worksheets[0];

// Fills the data.
ws.Cells["J5"].Value = 14;
ws.Cells["J6"].Value = new DateTime(2008, 4, 20);
ws.Cells["J6"].Style.NumberFormat = "m/dd/yyyy";

ws.Cells["D12"].Value = "ACME Corp";
ws.Cells["D13"].Value = "240 Old Country Road, Springfield, IL";
ws.Cells["D14"].Value = "USA";
ws.Cells["D15"].Value = "Joe Smith";

ws.Cells["E18"].Value = "6-APR-08 until 7-APR-08.";

ws.Cells[21, 1].Value = new DateTime(2008, 4, 6);
ws.Cells[21, 1].Style.NumberFormat = "dddd, mmmm dd, yyyy";
ws.Cells[21, 4].Value = 8;

ws.Cells["B36"].Value = "Payment via check.";

// Saves the file.
ef.SaveXls("NewReport.xls");


Visual Basic .NET code:

Dim ef As New ExcelFile

' Loads the template file.
ef.LoadXls("InvoiceTemplate.xls")

' Selects the first worksheet.
Dim ws As ExcelWorksheet = ef.Worksheets(0)

' Fills the data.
ws.Cells("J5").Value = 14
ws.Cells("J6").Value = New DateTime(2008, 4, 20)
ws.Cells("J6").Style.NumberFormat = "m/dd/yyyy"

ws.Cells("D12").Value = "ACME Corp"
ws.Cells("D13").Value = "240 Old Country Road, Springfield, IL"
ws.Cells("D14").Value = "USA"
ws.Cells("D15").Value = "Joe Smith"

ws.Cells("E18").Value = "6-APR-08 until 7-APR-08."

ws.Cells(&H15, 1).Value = New DateTime(2008, 4, 6)
ws.Cells(&H15, 1).Style.NumberFormat = "dddd, mmmm dd, yyyy"
ws.Cells(&H15, 4).Value = 8

ws.Cells("B36").Value = "Payment via check."

' Saves the file.
ef.SaveXls("NewReport.xls")


To save the file in another format you only need to replace the SaveXls method with preferred one (SaveXlsx, SaveCsv or SaveHtml).

Template file had no report data and looked like this:

Template report file

New file was created by using the code above and looks like this:

Created report file

















 

© GemBox Software. All rights reserved.