Introduction

Generating Excel files from web pages or other applications is a major subject in many articles. I'd like to present here a solution that does not require MS Excel to be installed on the target computer. This article shows how to create Microsoft Excel Binary Interchange File Format (BIFF) without using Microsoft Excel. BIFF is the native file format for Excel data, and can be viewed and modified in Microsoft Excel 97 or later.

Approaches and problems

The technique that is most frequently used to transfer data to an Excel workbooks is Automation. With Automation, you can call methods and properties that are specific to Excel tasks, but this solution has many drawbacks. Some of them are described in the Microsoft Knowledge Base. Additionally, you have to manage the lifetime of the temporary XLS files created on the server. Also, it is slow, because Excel runs in a separate process.

ExcelWriter class

The solution presented here is to write directly to a stream in Excel binary file format. For this purpose, I created very small C# class called ExcelWriter.

Using the code

The ExcelWriter sample is a console application that writes an XLS file to the name and location specified. The XLS file that is created is simply an empty spreadsheet. Then, you can start to write cell values by calling the WriteCell method with the row number, column number, and a cell value as parameters:

Comments and Discussions

I am able to create a file with extension .xls using ExcelWriter class provided above. But for any field if i have large data(like 500 chars or more) it creates file without any error, but when i open the file in MS excel it gives error on open saying "File Error: you may have lost some data."

Clearly Barbo you are out of touch with reality. The article is obviously a solution or provides a direction to many people, read the feed back. Your comments lack reasons and examples which make you look like a fool. Looking forward to reading some of your articles.

You can create nicely formatted Excel files very easily using this library, by reusing parts from a secondary Excel file (even entire worksheets): http://officehelper.codeplex.com/documentation
Microsoft Office does not need to be installed on the target machine!