5 Solutions to Export Data to Excel for ASP.NET

13 篇文章 0 订阅

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值