The code I posted in can be practically without any changes in any ASP.NET code written in C #. The auxiliary class DataForExcel (see DataForExcel.cs file) has a constructor
public DataForExcel(string[] headers, List<string[]> data, string sheetName)
or a slightly more advanced version
public DataForExcel(string[] headers, DataType[] colunmTypes, List<string[]> data, string sheetName)
which allow you to specify which columns have a numeric data type. The List<string[]> data parameter is the data that you want to export to Excel. The string[] headers parameter specifies the data for the first line of output.
The DataForExcel class has only one public method.
public void CreateXlsxAndFillData(Stream stream)
which populate the stream binary representation of the resulting Excel .XLSX file.
To return binary data from your ASP.NET method (e.g. ASHX handler), you just need to do the same thing as ExecuteResult from my answer:
- create a memory stream using
using (var stream = new MemoryStream()) {...} - create a
DataForExcel object necessary for export to Excel var dataExcel = new DataForExcel (new []{"Id", "Votes", "Title"}, "Questions.xlsx", "Name or Sheet"); response.AddHeader ("content-disposition", "attachment; filename=Questions.xlsx");response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";response.ContentEncoding = Encoding.UTF8;stream.WriteTo (response.OutputStream);response.Flush();
All.
UPDATED . I modified the previously published code to make it very easy to create an Excel file (in .xlsx format) from a grid. If you are using the OpenXML SDK 2.0 , you can use .NET 3.5. OpenXML SDK 2.5 requires .NET 4.0 or higher.
The proposed code allows you to convert string[][] data into binary Excel data and write the results to Stream. You can use a MemoryStream , as I described earlier, to return Excel from any ASP.NET application.
The proposed code contains an ExportToExcel static class with one open static method FillSpreadsheetDocument , which can be used as follows
var data = new[] { new [] {"Tom", "30", "x", "", "1974-06-16"}, new [] {"Margarita", "34", "x", "x", "1978-10-02"}, new [] {"Bob", "7", "", "", "2005-06-26"}, new [] {"Oleg", "48", "x", "x", "1964-09-11"}, new [] {"Frank", "29", "", "x", "1983-01-28"} }; using (var stream = new FileStream("Test.xlsx", FileMode.Create)) { ExportToExcel.FillSpreadsheetDocument(stream, new[] { new ColumnModel { Type = DataType.String, Alignment = HorizontalAlignment.Left, Header = "Name" }, new ColumnModel { Type = DataType.Integer, Header = "Age" }, new ColumnModel { Type = DataType.String, Header = "Is Married", Alignment = HorizontalAlignment.Center, IsRotatedHeader = true }, new ColumnModel { Type = DataType.String, Header = "Has Children", Alignment = HorizontalAlignment.Center, IsRotatedHeader = true }, new ColumnModel { Type = DataType.Date, Header = "Birthday", Alignment = HorizontalAlignment.Left } }, data, "Friends"); }
He creates "Test.xlsx" with one sheet of "Friends" that looks like

The width of the columns will not be set, but in two clicks (select all and double-click between the columns), the user can set the width of the columns to the optimal width, as in the one built above. All cells have formatted data (there is no "common" format). I used integers, date and blank lines. You can easily create columns with center alignment or right justification texts.
You can easily change the code to use different text formats. This is just an example of how to create a real Excel document with formatted cells.
Visual Studio 2008 working project you can download from here . Below you will find the source code from the demo:
using System; using System.Collections.Generic; using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadsheet; using System.Text; using System.IO; using System.Globalization; namespace ExportToExcel { public enum ExcelCellDataType { String, Integer, Date } public enum HorizontalAlignment { Left, Center, Right } public class ColumnModel { public ExcelCellDataType Type { set; get; } public HorizontalAlignment Alignment { set; get; } public string Header { set; get; } public bool IsRotatedHeader { set; get; } } public enum OutputCellFormat: uint { Text, Integer, Date, TextHeader, TextHeaderRotated, TextCenter, TextRight } public static class ExportToExcel { private static StringBuilder ConvertIntToColumnHeader (uint iCol) { var sb = new StringBuilder(); while (iCol > 0) { if (iCol <= 'Z' - 'A')