繁体   English   中英

从视图填充 excel 工作簿

[英]populate excel workbook from a view

从视图中填充 excel 工作簿的最佳方法是什么?

我想在视图中设计一个文本框“StudentId”和一个“显示成绩”按钮。

一旦学生输入“StudentId”并单击“显示成绩”,我想从“StudentTable”中为该特定“StudentId”提取“成绩”并将其显示在 Excel 工作簿中。

我怎么能这样做?

提前致谢

如果导出到 Excel 2007+ (.xlsx) 适合您的情况,您可以查看 EPPlus 库。 它对我来说效果很好。 你不需要安装office。

http://epplus.codeplex.com/

http://epplus.codeplex.com/wikipage?title=WebapplicationExample

从网站导出到 excel 可能会很棘手。 我还没有找到一种干净的方法来做到这一点。 主要是因为它需要在服务器上安装office。

这里有一些例子。

http://forums.asp.net/t/1038105.aspx/1

http://madskristensen.net/post/Export-a-DataTable-to-Excel-in-ASPNET.aspx

但我建议简单地导出 in.csv

昂贵但令人难以置信的完整选项: Office Writer

另一种选择是填充 Gridview 然后更改然后调整响应标头以使浏览器下载结果 Excel 工作簿。

我使用以下内容生成一个简单的电子表格:

public class GridViewExportUtil
{
    /// <summary>Exports a Gridview to Excel</summary>
    /// <param name="fileName">File Name For The Excel file.</param>
    /// <param name="gv">Gridview to Export</param>
    public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        bool dataAdded = false;

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();

                //Add Lines
                table.GridLines = gv.GridLines;


                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);

                    //Add Some Basic Formatting
                    foreach (TableCell tc in table.Rows[table.Rows.Count - 1].Cells)
                    {
                        tc.Style.Add(HtmlTextWriterStyle.FontWeight, "bold");
                        tc.BackColor = System.Drawing.Color.Black;
                        tc.ForeColor = System.Drawing.Color.White;
                    }
                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                    dataAdded = true;
                }

                //If no data added add row with the Gridviews' no data message
                if (!dataAdded)
                {
                    TableCell cell = new TableCell();
                    cell.Text = gv.EmptyDataText;
                    cell.Style.Add(HtmlTextWriterStyle.FontWeight, "bold");

                    TableRow tmpRow = new TableRow();
                    tmpRow.Cells.Add(cell);

                    table.Rows.Add(tmpRow);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }                    

                //  render the table into the htmlwriter
                table.RenderControl(htw);

                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

    /// <summary>
    /// Replace any of the contained controls with literals
    /// </summary>
    /// <param name="control"></param>
    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }

            if (current.HasControls())
            {
                GridViewExportUtil.PrepareControlForExport(current);
            }
        }
    }
}

这根本没有额外的要求服务器端。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM