5

Wondering what is a good library I can use with VS2005 to export data to a excel file. The file has some formatting like background colors and colspans.

Thanks

5 Answers 5

3

Here is some code that uses a trick to output HTML to an Excel file. I have found that you can trick excel into opening an HTML by setting the content type of the output to "application/excel".

In the code below secresults is an HTML div like so:

<div id="secresults" runat="server" visible="false" class="secresults">
Content or data here.
</div>

In code behind:

Response.ClearContent();
string filename = "Output" + istartDate.ToShortDateString() + ".xls";
Response.AddHeader("content-disposition", "attachment; filename=" + filename + ";");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);

secresults.RenderControl(htw);


Response.Write(sw.toString());
Response.End();

I have found that you can use some html formatting in excel. To test which formatting you can use you can create an html file and rename it to a .xls file, then open it with excel. You can get a pretty good idea about what HTML Excel will read.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice. Did not know you could do that.
3

I would recommend taking the class from the following website, and adapting it to your needs.

Mikes Knowledge Base - ExportToExcel

By default, this class takes a DataSet, DataTable or List<>, and exports it into a genuine Excel 2007 .xlsx file, using the OpenXML libraries (also provided).

It doesn't currently attempt to add any formating to the Excel cells (DataTables only store values, not formating, colors, horizontal alignment, etc !) but it should be a good place to start from.

All source code is provided, free of charge, so you can adapt it as required.

Good luck !

Comments

1

I have used this library in the past, but I normally just spit out a CSV file.

C# class library for exporting data to CSV/Excel file

2 Comments

I saw that library also. Can you do formatting with that library? Meaning create background colors, change the font of the text for some columns etc.
Not sure, I doubt it, sounds like you might need a reporting solution like SQL Reporting Services, Active Reports...etc
1

If you have predefined layout of the document, Templater will probably fit your description.

Take a look at the example on how to use it.

Comments

1

I've used this codeplex project (Excel Package). One technique is to start with a formatted template, then modify the template. That is much easier than applying a lot of styling commands starting with an empty spreadsheet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.