Asp.net 以pdf格式下载

Asp.net 以pdf格式下载,asp.net,Asp.net,这就是我在Excel中下载gridview的方式。请提供pdf下载代码帮助: 字符串strFileName=“Salary_Statement”+DateTime.Now.ToString(“ddMMyyyy”); GridView1.AllowPaging=false GridView1.HeaderRow.Cells[2].Visible = true; GridView1.HeaderRow.Cells[3].Visible = true;

这就是我在Excel中下载gridview的方式。请提供pdf下载代码帮助: 字符串strFileName=“Salary_Statement”+DateTime.Now.ToString(“ddMMyyyy”); GridView1.AllowPaging=false

        GridView1.HeaderRow.Cells[2].Visible = true;
        GridView1.HeaderRow.Cells[3].Visible = true;
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];

        }


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

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            row.Attributes.Add("class", "textmode");
        }
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition",
        "attachment;filename=" + strFileName + ".xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        GridView1.RenderControl(hw);

        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.Flush();

        Response.End();
        GridView1.AllowPaging = true;//AB
GridView1.HeaderRow.Cells[2].Visible=true;
GridView1.HeaderRow.Cells[3].Visible=true;
对于(int i=0;i
试试这个。下面的代码显示了如何下载

使用系统;
使用系统数据;
使用系统配置;
使用System.Web;
使用System.Web.Security;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用System.Web.UI.HTMLControl;
使用iTextSharp.text;
使用iTextSharp.text.html;
使用iTextSharp.text.pdf;
受保护的void ExportToPDF(GridView gvReport,布尔景观)
{
int noOfColumns=0,noOfRows=0;
数据表tbl=null;
if(gvReport.AutoGenerateColumns)
{
tbl=gvReport.DataSource as DataTable;//获取GridView控件的数据源。
noOfColumns=tbl.Columns.Count;
noOfRows=tbl.Rows.Count;
}
其他的
{
noOfColumns=gvReport.Columns.Count;
noOfRows=gvReport.Rows.Count;
}
浮头TEXTSIZE=8;
float ReportNameSize=10;
float ReportTextSize=8;
float ApplicationNameSize=7;
//创建PDF文档
单据=空;
如果(横向==真)
{
//将文档设置为A4大小并旋转文档,使页面的方向为横向。
document=新文档(PageSize.A4.Rotate(),0,0,15,5);
}
其他的
{
文档=新文档(PageSize.A4,0,0,15,5);
}
//创建一个PdfPTable,该表的列数等于gridview或gridview数据源的列数。
iTextSharp.text.pdf.PdfPTable mainTable=新的iTextSharp.text.pdf.PdfPTable(noOfColumns);
//将表的前4行设置为标题行,标题行将在所有页面中重复。
mainTable.HeaderRows=4;
//创建包含两列的PdfPTable,以在导出的PDF中保存标题。
iTextSharp.text.pdf.PdfPTable headerTable=新的iTextSharp.text.pdf.PdfPTable(2);
//创建一个短语,将应用程序名称保留在标题的左侧。
PHAPapplicationName=新短语(“示例应用程序”,FontFactory.GetFont(“Arial”,ApplicationNameSize,iTextSharp.text.Font.NORMAL));
//创建接受短语作为参数的PdfPCell。
PdfPCell clApplicationName=新的PdfPCell(phApplicationName);
//将单元格的边框设置为零。
claplicationname.Border=PdfPCell.NO_Border;
//将PdfPCell的水平对齐设置为左侧。
clApplicationName.HorizontalAlignment=Element.ALIGN_LEFT;
//创建一个短语,在标题右侧显示当前日期。
phDate=新短语(DateTime.Now.Date.ToString(“dd/MM/yyyy”)、FontFactory.GetFont(“Arial”、ApplicationNameSize、iTextSharp.text.Font.NORMAL));
//创建接受日期短语作为参数的PdfPCell。
PdfPCell clDate=新的PdfPCell(phDate);
//将PdfPCell的水平对齐设置为右侧。
clDate.HorizontalAlignment=Element.ALIGN\u RIGHT;
//将单元格的边框设置为零。
clDate.Border=PdfPCell.NO_Border;
//将保存应用程序名称的单元格添加到headerTable。
headerTable.AddCell(claplicationname);
//将保存日期的单元格添加到headerTable。
headerTable.AddCell(clDate);
//将headerTable的边框设置为零。
headerTable.DefaultCell.Border=PdfPCell.NO_Border;
//创建接受headerTable作为参数的PdfPCell,然后将该单元格添加到主PdfPTable中。
PdfPCell cellHeader=新的PdfPCell(headerTable);
cellHeader.Border=PdfPCell.NO_Border;
//将标题单元格的列范围设置为noOfColumns。
cellHeader.Colspan=noOfColumns;
//将上述标题单元格添加到表中。
mainTable.AddCell(cellHeader);
//创建包含文件名的短语。
phHeader=新短语(“示例导出”,FontFactory.GetFont(“Arial”,ReportNameSize,iTextSharp.text.Font.BOLD));
PdfPCell clHeader=新的PdfPCell(phHeader);
clHeader.Colspan=noOfColumns;
clHeader.Border=PdfPCell.NO_Border;
clHeader.HorizontalAlignment=Element.ALIGN\u CENTER;
mainTable.AddCell(clHeader);
//为新行创建短语。
短语phSpace=新短语(“\n”);
PdfPCell clSpace=新的PdfPCell(phSpace);
clSpace.Border=PdfPCell.NO_Border;
clSpace.Colspan=noOfColumns;
主表。