Sunday, April 5, 2009

Aspose Library for Creating Excel and Word Files

If you want to generate Excel and Word files using .NET or Java code, then the Aspose.Cells and Aspose.Words libraries are just what you are looking for. The Aspose.Cells library costs roughly $600 while the Aspose.Words library costs about $900, but you can get a discount of about $200 for purchasing both libraries in a single transaction. You might want to buy Aspose.Total, which is a package containing both Aspose.Cells and Aspose.Words along with several other useful libraries.

How to use Aspose.Cells for creating Excel files in .NET

Using Aspose.Cells is quite simple. You need to add a reference to the Aspose.Cells.dll, create an instance of the Workbook class, get a Worksheet instance (either use the existing sheet or create a new one) and use the Cells indexer to access a cell. Set the value of cells using the PutValue method. Finally, generate the Excel file using the Save method of the Workbook class. Here's an example:

//using Aspose.Cells; //add this to the top of your file
Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];
wb.Save("File1.xls", FileFormatType.Excel2003);

If you are using Aspose.Cells in an ASP.NET application, you can simply save the document for the user to download by replacing the above statement with the one below:
wb.Save("File1.xls", FileFormatType.Excel2003, SaveType.OpenInExcel, Response);

You can also use Aspose.Cells to directly save a DataTable (System.Data.DataTable) to an Excel file by inserting values into the worksheet like this:
ws.Cells.ImportDataTable(myDataTable, true, "B2");
The "true" value indicates that the column names of the data table will also be written to the worksheet and the "B2" indicates the cell from which the copy will begin.

How to use Aspose.Words for creating Word files in .NET

Using Aspose.Words was a little challenging for me to get started as the demos provided on the Aspose website do not provide examples of creating MS Word files from scratch, but rather focus on demonstrating the ability to load an existing Word file as a template and building upon it. As is the case with the Workbook object in Aspose.Cells, you have to begin by creating a Document object and creating a DocumentBuilder. You can then use the Write, Writeln, and InsertHtml methods of the DocumentBuilder class to create the document and the Save method of the Document class to write to a file or a Respose stream.

Here's an example:
//using Aspose.Words; //add this to the top of your file
Document doc = new Document();
DocumentBuilder bld = new DocumentBuilder(doc);
bld.Writeln("Nitin Reddy");
bld.InsertHtml("<b>Location:</b> Dubai<br />");
doc.Save("File1.doc", SaveFormat.Doc);

When using this in an ASP.NET application to serve files to client browsers, simply replace the above doc.Save method call with the following:
doc.Save("File1.doc", SaveFormat.Doc, SaveType.OpenInWord, Respose);


  1. Do you want to write more articles and posts about Aspose.Words? Contact me at roman dot korchagin at aspose dot com.

  2. My wife has quite big experience in computer sphere. But only she couldn't help in my trouble. I used the Inet and luckily observed there a good tool, which worked out my issue for seconds and would settle any question also - how can repair word doc.