ASP表到Excel

时间:2013-05-21 07:50:43

标签: c# html asp.net excel

在网站上查看ASPX时,我在单元格中找到了一个文本很少的表格,例如:

tab.Rows[1].Cells[1].innerHtml = "Booked :"

(在很多行和单元格中,但每个单元格中有不同的文本)

现在我只想点击一个按钮,表格中的数据将被下载到Excel文件中。

表ID:标签

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //Table tbl = new Table();
        TableRow tr = new TableRow();
        TableCell tcel = new TableCell();
        tcel.Text = "id";
        tr.Cells.Add(tcel);

        TableCell tcel1 = new TableCell();
        tcel1.Text = "id1";
        tr.Cells.Add(tcel1);

        tab.Rows.Add(tr);
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    string filename = "ExportExcel.xls";
    System.IO.StringWriter tw = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

    //DataGrid dgGrid = new DataGrid();
    //dgGrid.DataSource = tbl;
    //dgGrid.DataBind();

    //Get the HTML for the control.             
    tab.RenderControl(hw);
    //Write the HTML back to the browser.
    //Response.ContentType = application/vnd.ms-excel;
    Response.ContentType = "application/vnd.ms-excel";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
    this.EnableViewState = false;
    Response.Write(tw.ToString());
    Response.End();
}

用watraplion回答修改,但仍然没有回答.. 错误在:

DataTable dt = dt; //Use of unassigned local variable 'dt'

2 个答案:

答案 0 :(得分:1)

试试这个:

aspx设计视图:

<body>
   <form id="form1" runat="server">
      <div>
          <asp:Table ID="Table1" runat="server">
           </asp:Table>
      </div>
      <div>
           <asp:Button ID="btnExport" onclick="btnExport_Click" Text="Export" runat="server">
      </div>
   </form>
</body>

aspx.cs(代码隐藏)

    protected void Page_Load(object sender, EventArgs e)
    {
        TableRow tr = new TableRow();
        TableCell tcel = new TableCell();
        tcel.Text = "id";
        tr.Cells.Add(tcel);

        TableCell tcel1 = new TableCell();
        tcel1.Text = "id1";
        tr.Cells.Add(tcel1);

        Table1.Rows.Add(tr);
    }

    protected void btn_Click(object sender, EventArgs e)
    {
        string filename = "ExportExcel.xls";
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

        //Get the HTML for the control.             
        Table1.RenderControl(hw);
        //Write the HTML back to the browser.
        //Response.ContentType = application/vnd.ms-excel;
        Response.ContentType = "application/vnd.ms-excel";
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
        this.EnableViewState = false;
        Response.Write(tw.ToString());
        Response.End();
     }

答案 1 :(得分:0)

首先要说的是,你要做的就是不要导出到excel,但你发送一个带有错误标题的html来欺骗浏览器用excel打开这个内容。

这个解决方案有很多问题,擅长自我会警告用户内容与扩展不同,因为你发送html并且你的响应标题说这是一个excel文件。我可以打赌,客户端上的一些反恶意软件或服务器上的类似软件会阻止此响应,因为提供的内容不同于头文件中声明的恶意软件行为。

使用专用的excel库会更好更容易,例如EPPlus,请查看ASP.NET ashx处理程序,它将DataTable导出到真正的excel文件:

https://stackoverflow.com/a/9569827/351383