Asp.Net - 将两个Gridview导出为ex​​cel或pdf

时间:2013-05-14 11:38:57

标签: c# asp.net gridview

我有一个页面并排显示两个网格; “费用”和“收入”。 我希望用户能够将其导出为ex​​cel或pdf,或者按照网页上显示的方式将其打印出来。

我该怎么办? 什么是最佳做法?

感谢。

4 个答案:

答案 0 :(得分:0)

可能Reporting Services (SSRS)
但是在sql server

的情况下

答案 1 :(得分:0)

protected void btnExportExcel_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";

        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.AllowPaging = false;

        // Re-Bind data to GridView 

        using (CompMSEntities1 CompObj = new CompMSEntities1())
        {
            Start = Convert.ToDateTime(txtStart.Text);
            End = Convert.ToDateTime(txtEnd.Text);

            GridViewSummaryReportCategory.DataSource = CompObj.SP_Category_Summary(Start, End);
            SP_Category_Summary_Result obj1 = new SP_Category_Summary_Result();
            GridView1.DataBind();
           GridView1.Visible = true;
            ExportTable.Visible = true;
        }

        //Change the Header Row back to white color

        GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF");


       GridView1.Style.Add(" font-size", "10px");




        //Apply style to Individual Cells

        GridView1.HeaderRow.Cells[0].Style.Add("background-color", "green");
        GGridView1.HeaderRow.Cells[1].Style.Add("background-color", "green");
        GridView1.HeaderRow.Cells[2].Style.Add("background-color", "green");
        GridView1.HeaderRow.Cells[3].Style.Add("background-color", "green");
        GridView1.HeaderRow.Cells[4].Style.Add("background-color", "green");

        for (int i = 1; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];

            //Change Color back to white

            row.BackColor = System.Drawing.Color.White;

            //Apply text style to each Row

        //    row.Attributes.Add("class", "textmode");

            //Apply style to Individual Cells of Alternating Row

            if (i % 2 != 0)
            {
                row.Cells[0].Style.Add("background-color", "#C2D69B");
                row.Cells[1].Style.Add("background-color", "#C2D69B");
                row.Cells[2].Style.Add("background-color", "#C2D69B");
                row.Cells[3].Style.Add("background-color", "#C2D69B");
                row.Cells[4].Style.Add("background-color", "#C2D69B");
            }
        }
        GridView1.RenderControl(hw);

        //style to format numbers to string

        string style = @"<style> .textmode { mso-number-format:\@; } </style>";

        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }

答案 2 :(得分:0)

对于打印,您可以使用CSS中的@media print { }块指定特殊样式,并在浏览器中手动启动打印或在JavaScript中调用window.print()

要导出到Excel,您可以使用,例如http://closedxml.codeplex.com/

要导出为PDF,您可以使用http://sourceforge.net/projects/itextsharp/

答案 3 :(得分:0)

protected void btnExportToExcel_Click(object sender, EventArgs e)

{

    if (RadioButtonList1.SelectedIndex == 0)

    {

        GridView1.ShowHeader = true;

        GridView1.GridLines = GridLines.Both;

        GridView1.AllowPaging = false;

        GridView1.DataBind();

    }

    else

    {

        GridView1.ShowHeader = true;
        GridView1.GridLines = GridLines.Both;

        GridView1.PagerSettings.Visible = false;

       GridView1.DataBind();

    }



    ChangeControlsToValue(GridView1);

    Response.ClearContent();



    Response.AddHeader("content-disposition", "attachment; filename=GridViewToExcel.xls");



    Response.ContentType = "application/excel";



    StringWriter sWriter = new StringWriter();



    HtmlTextWriter hTextWriter = new HtmlTextWriter(sWriter);



    HtmlForm hForm = new HtmlForm();



    GridView1.Parent.Controls.Add(hForm);



    hForm.Attributes["runat"] = "server";



    hForm.Controls.Add(GridView1);



    hForm.RenderControl(hTextWriter);



    // Write below code to add cell border to empty cells in Excel file

    // If we don't add this line then empty cells will be shown as blank white space



     StringBuilder sBuilder = new StringBuilder();

    sBuilder.Append("<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"> <head><meta http-equiv="Content-Type" content="text/html;charset=windows-1252"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>ExportToExcel</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>");

    sBuilder.Append(sWriter + "</body></html>");

    Response.Write(sBuilder.ToString());

    Response.End();

}
相关问题