现在的位置: 首页 > 综合 > 正文

5 Solutions to Export Data to Excel for ASP.NET

2016年01月24日 ⁄ 综合 ⁄ 共 14200字 ⁄ 字号 评论关闭

Introduction

Excel is so powerful that it can easily successfully open not only excel itself, but also CSV format files, Tab format, website table format and other file formats. It’s so smart that it can automatically recognize number, character string and can easily automatically transfer 11+ digital natural numbers to scientific notation. For example, if you input number ”123456789012” in a cell, the number will be changed as “1.23457E+11”. 

Background 

 

Because Excel is powerful and useful, people always export data to Excel for future use. Here I am introducing several methods of exporting data to Excel. Export Data to Excel through ASP.NET. Store the output file in a folder of server and put the file address on browser. Or directly write the file string on browser. When Response exporting,data divided by “t” equals columns and “n” means rows. Now let’s enjoy the show…

Using the code 

Solution 1, Export all HTML data to Excel 

This method will transfer all the content in HTML, such as button, table, images and others to Excel. 

  

Here we make use of “ContentType” property which the default set is text/HTML. And hypertext will be exported to client. If we change it to ms-excel, it will be exported as Excel format and your browser will remind you download and store it. The property of “ContentType” includes image/JPEG, text/HTML, image/GIF and vnd.ms-excel/msword. 

Solution 2, Export Data from DataGrid to Excel 

 

Although the method above can help you export data, it exports all data information such as button, images from HTML which we may not want to export. Usually, we only need export data stored in DatGrid.

 

If you have pages in your DataGrid, it only exports the information of the first page, maybe not all information you select.

Actually we can write like this:

Method:DGToExcel(datagrid1);

Solution 3, Export Data from DataSet to Excel

According to the methods above, we can easily export data from DataSet to Excel. We just need response rows information of DataSet table as ms-excel format to http string. Note: ds should be DataSet with information filled Datatable. File name should be full including suffix. For example, execl2006.xls

Solution 4, Export Data from Datview to Excel

If you want to export data to Excel with irregular rows or columns, you can use this method.

Solution 5, Export Data to Excel without Automation

By using this method you need download a free .net component and then do as the following codes showing(part) below:

 

Full Code Here

 

When you execute the codes above, you will get:

 

This solution can help you export data to Excel file which enables you open, write and modify with excel 2010. Although some special features of excel 2010 are still available, it's useful for Excel 2010.

2 more solutions via WinForm

Solution 6

 

Solution 7


</
ds1.tables[0].columns.count;i++)>

Points of Interest  

You may need some time to read this story

History

Keep a running update of any changes or improvements you've made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

loveyou999

抱歉!评论已关闭.