打印动态GridView内容

时间:2010-03-16 08:59:23

标签: asp.net

确定。我已经将这个令人厌恶的代码导出表格转换为 XLS (但仍然不知道如何使其可编辑(不是只读)

现在的任务是从GridView中打印数据。我可以将其发送到HTML格式(如在XLS上)没有行并且下载文件用于打印是很奇怪的。

如何为我的GridView制作一些“打印视图”(带有行...)并打印出来?

网站上有可能吗?没有下载。

顺便说一句,这是我如何导出...(也许我会得到一些进展^ _)

    using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;

public class GridViewExportUtil
{
    static HSSFWorkbook hssfworkbook;

    static MemoryStream WriteToStream()
    {
        //Write the stream data of workbook to the root directory
        MemoryStream file = new MemoryStream();
        hssfworkbook.Write(file);
        return file;
    }

    static void InitializeWorkbook()
    {
        hssfworkbook = new HSSFWorkbook();

        ////create a entry of DocumentSummaryInformation
        DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
        dsi.Company = "";
        hssfworkbook.DocumentSummaryInformation = dsi;

        ////create a entry of SummaryInformation
        SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
        si.Subject = "";
        hssfworkbook.SummaryInformation = si;
    }
    /// <param name="fileName"></param>
    /// <param name="gv"></param>
    public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.Charset = System.Text.Encoding.Unicode.EncodingName;
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
        HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
        //HttpContext.Current.Response.ContentType = "application/ms-excel";
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; // NPOI
        HttpContext.Current.Response.AddHeader(
             "content-disposition", string.Format(
                "attachment; filename=Report.xls"));//, fileName)); // Need .XLS file
        HttpContext.Current.Response.Clear();

        InitializeWorkbook();

        HSSFSheet sheet1 = hssfworkbook.CreateSheet("Таблица");
        //sheet1.CreateRow(0).CreateCell(0).SetCellValue("Таблица");

        using (StringWriter sw = new StringWriter())
        {
                //  Create a form to contain the grid
                Table table = new Table();
                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }
                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }
                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                sheet1.DisplayGridlines = true;

                HSSFCellStyle style1 = hssfworkbook.CreateCellStyle();
                style1.Alignment = HSSFCellStyle.ALIGN_CENTER;

                sheet1.SetColumnWidth(0, 10000);
                sheet1.SetColumnWidth(1, 5000);
                sheet1.VerticallyCenter = true;

                for (int j = 2; j < table.Rows[0].Cells.Count; j++)
                {
                    sheet1.SetColumnWidth(j, 4000);
                    sheet1.SetDefaultColumnStyle(short.Parse(j.ToString()), style1);
                }

                double Temp=0;
                for(int i=0; i<(table.Rows.Count-1); i++)
                {

                    for (int j = 0; j < table.Rows[i].Cells.Count;j++)
                    {
                        if (i != 0 && j != 0)
                        {
                            if (double.TryParse(table.Rows[i].Cells[j].Text, out Temp))
                            {
                                sheet1.CreateRow(i).CreateCell(j).SetCellValue(Temp.ToString());
                            }
                        }
                        else
                        {
                            sheet1.CreateRow(i).CreateCell(j).SetCellValue(table.Rows[i].Cells[j].Text);
                        }
                    }
                }
                HttpContext.Current.Response.BinaryWrite(WriteToStream().GetBuffer());
                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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你可以做的是在javascript中调用浏览器的“PRINT”功能来打印你的页面。这意味着您网页上的所有内容不仅仅是gridview。

相关问题