ASP.NET输出EXCEL表格的几种方法(总结修改)
这几天要从数据库导出EXCEL表格,就找了N钟方法,经测试,下面的方法比较的好用一点。都是经过输入DataTable而导出的。不过导出的EXCEL都不是正规的EXCEL格式,只能说是HTML格式的,导出的再导入进数据库就会出现问题了。想导入的话用EXCEL打开另存为EXCEL格式就好了
1.利用DataRow直接输出,经测试,没有乱码。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
输出为:学院活动表.xls
活动名称 活动时间 活动地点 发起人 承担人 批准人 活动概况 参与人 备注
第5次9 2008-7-27 0:00:00 HBU_ 1 shenxian_ 1 jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次8 2008-7-27 0:00:00 HBU_ 2 shenxian_ 2 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次7 2008-7-27 0:00:00 HBU_ 3 shenxian_ 3 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次6 2008-7-27 0:00:00 HBU_ 4 shenxian_ 4 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次5 2008-7-27 0:00:00 HBU_ 5 shenxian_ 5 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次9 2008-7-27 0:00:00 HBU_ 1 shenxian_ 1 jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次8 2008-7-27 0:00:00 HBU_ 2 shenxian_ 2 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次7 2008-7-27 0:00:00 HBU_ 3 shenxian_ 3 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次6 2008-7-27 0:00:00 HBU_ 4 shenxian_ 4 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
第5次5 2008-7-27 0:00:00 HBU_ 5 shenxian_ 5 xianren jiaoren there where is a will,there is a way ren ren ren ren people haha,hehe,xixi
2.利用GridView输出,经测试,有的输出有乱码
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
输出为:工作表.xls
<div>
<table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
<tr>
<th scope="col">课程编号</th><th scope="col">课程名称</th><th scope="col">课序号</th><th scope="col">任课教师</th>
</tr><tr>
<td>ASP.NET-0</td><td>ASP.NET</td><td>0</td><td>shenxian</td>
</tr><tr>
<td>VB-0</td><td>VB</td><td>0</td><td>任志波</td>
</tr><tr>
<td>操作系统-0</td><td>操作系统</td><td>0</td><td>郝杰</td>
</tr><tr>
<td>计算机网络-0</td><td>计算机网络</td><td>0</td><td>杨秀丹</td>
</tr><tr>
<td>数据库-0</td><td>数据库</td><td>0</td><td>郝杰</td>
</tr><tr>
<td>数据库应用技术-0</td><td>数据库应用技术</td><td>0</td><td>wjy</td>
</tr><tr>
<td>数据库应用技术-1</td><td>数据库应用技术</td><td>1</td><td>wjy</td>
</tr>
</table>
</div>
3.利用DataGrid输出,导出没问题,有没有乱码的问题没注意
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
输出的内容为:1.xls
<table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">
<tr align="center" style="background-color:LightGrey;font-weight:bold;">
<td>课程编号</td><td>课程名称</td><td>课序号</td><td>任课教师</td>
</tr><tr align="center">
<td>ASP.NET-0</td><td>ASP.NET</td><td>0</td><td>shenxian</td>
</tr><tr align="center" style="background-color:LightGrey;">
<td>VB-0</td><td>VB</td><td>0</td><td>任老师</td>
</tr><tr align="center">
<td>操作系统-0</td><td>操作系统</td><td>0</td><td>郝老师</td>
</tr><tr align="center" style="background-color:LightGrey;">
<td>计算机网络-0</td><td>计算机网络</td><td>0</td><td>杨老师</td>
</tr><tr align="center">
<td>数据库-0</td><td>数据库</td><td>0</td><td>郝老师</td>
</tr><tr align="center" style="background-color:LightGrey;">
<td>数据库应用技术-0</td><td>数据库应用技术</td><td>0</td><td>wjy</td>
</tr><tr align="center">
<td>数据库应用技术-1</td><td>数据库应用技术</td><td>1</td><td>wjy</td>
</tr>
</table>