使用 ASP.NET 将 HTML 表格导出到 Excel [英] Export HTML Table to Excel using ASP.NET

查看:32
本文介绍了使用 ASP.NET 将 HTML 表格导出到 Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 html 表(不是 Gridview)并且没有正确的标题和行.相反,它具有定制的结构和数据.我想将此表导出到 Excel.如何使用 ASP.NET?

I have an html table (Not Gridview) and that has no proper Header and rows. Instead it has customized structure and data. I want to export this table to Excel. How can I do using ASP.NET?

标签是固定文本,整数值来自数据库.所以表结构是固定的,只有整数/十进制值发生变化.

The labels are fixed text and the integer values are coming from database. So the table structure is fixed only the integer/decimal values change.

推荐答案

您希望使用 ASP.NET 将 HTML 表(非 Gridview)自定义结构和数据导出到 Excel.

You want Export HTML table (Not Gridview) customized structure and data to Excel using ASP.NET.

试试下面的方法

  1. 提供ID并添加runat="server"属性

添加以下代码

Response.ContentType = "application/x-msexcel"; 
Response.AddHeader("Content-Disposition", "attachment;
filename=ExcelFile.xls");
Response.ContentEncoding = Encoding.UTF8; 
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
tbl.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();

这篇关于使用 ASP.NET 将 HTML 表格导出到 Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆