gridview 导出pdf???????????????
最好能用哦
最好能用哦
解决方案 »
- 不显示删除回复显示所有回复显示星级回复显示得分回复 DataGrid中如何操作DropDownList?[
- 完全开源,最近研究NHibernate的小成果,大家给点意见
- alert()消息框问题
- datagrid增加行问题 高手进
- 請教高手,如何實現遠程文件訪問?
- DATAlist的一个让我头痛很久的问题
- 在使用CrystalReportViewer时提示编译错误?请问怎么回事(在线等……)
- 在代码中给DataGrid加超级链接列,怎样才能加到右边???
- datagrid控件是不是大多用来做查询时显示数据用的?如果表格的字段很多,而且有的字段很长,用datagrid做起来不美观,请问各位都用什么办
- 怎样在页面过度期间显示Loading提示?
- 网站发布到服务器上后访问提示数据库是只读的是怎么回事?
- WPF引入UserControl问题
using System;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;//******************************************//引入的命名空间using System.Text;using System.IO;using iTextSharp;using iTextSharp.text;using iTextSharp.text.pdf;//****************************************** /// <summary>///GridViewToPdf 的摘要说明/// </summary>public class GridViewToPdf{ public GridViewToPdf() { } #region ConvertGrdiViewToPdf 换GridView为PDF文档,每一页都有表头 /// <summary> /// 转换GridView为PDF文档 /// </summary> /// <param name="pobjGrdv">要转换的GridView</param> /// <param name="PDFFileName">在服务器端保存PDF时的文件名</param> /// <param name="FontPath">PDF甩用字体所在的物理路径</param> /// <param name="FontSize">字体大小</param> /// <returns>返回调用是否成功</returns> public static void ConvertGrdiViewToPdf(GridView pobjGrdv, string PDFFileName, string FontPath, float FontSize) { //在服务器端保存PDF时的文件名 string strFileName = PDFFileName + ".pdf"; // GridView的所有数据全输出 pobjGrdv.AllowPaging = false; //************************** int countColumns = pobjGrdv.Columns.Count; int countRows = pobjGrdv.Rows.Count; if (countColumns != 0 && countRows != 0) { //初始化一个目标文档类 //Document document = new Document(); //竖排模式,大小为A4,四周边距均为25 Document document = new Document(PageSize.A4, 0, 0, 0, 0); //横排模式,大小为A4,四周边距均为50 //Document doc = new Document(PageSize.A4.rotate(),50,50,50,50); //调用PDF的写入方法流 //注意FileMode-Create表示如果目标文件不存在,则创建,如果已存在,则覆盖。 PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(HttpContext.Current.Server.MapPath(strFileName), FileMode.Create)); try { //创建PDF文档中的字体 BaseFont baseFont = BaseFont.CreateFont( FontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //根据字体路径和字体大小属性创建字体 Font font = new Font(baseFont, FontSize); // 添加页脚 //HeaderFooter footer = new HeaderFooter(new Phrase(footertxt), true); HeaderFooter footer = new HeaderFooter(new Phrase("-- ", font), new Phrase(" --", font)); footer.Border = Rectangle.NO_BORDER; // 不显示两条横线 footer.Alignment = Rectangle.ALIGN_CENTER; // 让页码居中 document.Footer = footer; //打开目标文档对象 document.Open(); //************************** //根据数据表内容创建一个PDF格式的表 PdfPTable table = new PdfPTable(countColumns); //iTextSharp.text.Table table = new iTextSharp.text.Table(pobjGrdv.Columns.Count); // 添加表头 // 设置表头背景色 //table.DefaultCell.BackgroundColor = Color.GRAY; // OK //table.DefaultCell.BackgroundColor = //(iTextSharp.text.Color)System.Drawing.Color.FromName("#3399FF"); // NG table.DefaultCell.BackgroundColor = iTextSharp.text.Color.LIGHT_GRAY; //table.DefaultCell.BackgroundColor = System.Drawing.Color.DodgerBlue; // 添加表头 for (int j = 0; j < countColumns; j++) { table.AddCell(new Phrase(pobjGrdv.HeaderRow.Cells[j].Text, font)); // OK } // 告诉程序这行是表头,这样页数大于1时程序会自动为你加上表头。 table.HeaderRows = 1; // 添加数据 // 设置表体背景色 table.DefaultCell.BackgroundColor = Color.WHITE; //遍历原gridview的数据行 for (int i = 0; i < countRows; i++) { for (int j = 0; j < countColumns; j++) { table.AddCell(new Phrase(pobjGrdv.Rows[i].Cells[j].Text, font)); } } //在目标文档中添加转化后的表数据 document.Add(table); } catch (Exception) { throw; } finally { //关闭目标文件 document.Close(); //关闭写入流 writer.Close(); } // 弹出提示框,提示用户是否下载保存到本地 try { //这里是你文件在项目中的位置,根目录下就这么写 String FullFileName = System.Web.HttpContext.Current.Server.MapPath(strFileName); FileInfo DownloadFile = new FileInfo(FullFileName); System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.ClearHeaders(); System.Web.HttpContext.Current.Response.Buffer = false; System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream"; System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8)); System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString()); System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName); } catch (Exception) { throw; } finally { System.Web.HttpContext.Current.Response.Flush(); System.Web.HttpContext.Current.Response.End(); } } else { System.Web.HttpContext.Current.Response.Write ("<script type='text/javascript'>alert('数据为空,不值得导出pdf!');</script>"); } } //然后,在要调用转换的按钮的事件代码中调用就可以了 //假设传进去的GridView的名字为GridView1 //假设要保存的文件名为GridView的ID //假设字体使用simsun (请注意根据你电脑的实际情况来选择目录) //假设字号选择14 //GridViewToPdf.ConvertGridViewToPdf(this.GridView1, //this.GridView1.ID.ToString(), "c:\\winnt\\FONTS\\simsun.ttc,1", 14); #endregion}1. 请把上述代码放入编辑器中,由于格式混乱引起问题请自行调整。2. 外部使用时调用方法:using System.IO;using iTextSharp.text; /// <summary> /// 导出为pdf /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button_ExportPdf_Click(object sender, EventArgs e) { try { //假定GridView的ID为GridView_CheckStat GridView_CheckStat.AllowPaging = false; GridView_CheckStat.AllowSorting = false; //从页面取到查询条件 string materialType = this.txtMaterialType.Text; string depotType = this.DropDownList_DepotType.SelectedValue; string depotId = this.DropDownList_Depot.SelectedValue; string goodsName = this.txtGoodsName.Text.Trim(); //填充数据源 GridView_CheckStat.DataSource = CheckStatBll.getCheckStatByCondition(materialType, depotId, depotType,goodsName); //绑定数据源 GridView_CheckStat.DataBind(); //调用上面写好的转换方法 //将绑定好的GridView传入下面方法GridViewToPdf.ConvertGridViewToPdf(this.GridView_CheckStat, this.GridView_CheckStat.ID.ToString(), @"c:\WINDOWS\Fonts\msyh.ttf", 8); } catch (DocumentException de) { Response.Write(de.ToString()); } }
/// 导出数据到PDF
/// </summary>
/// <param name="Data">DataTable</param>
/// <param name="fileName">文件名</param>
/// <param name="headertxt">头部文字</param>
/// <param name="footertxt">尾部文字</param>
public static void ExportToPdf(DataTable Data, string fileName)
{ MemoryStream m = new MemoryStream();
Document document = new Document();
try
{
// step 2: we set the ContentType and create an instance of the Writer HttpContext.Current.Response.ContentType = "application/pdf";
PdfWriter.GetInstance(document, m); // step 3
document.Open();
//定义字体
BaseFont baseFont = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//设定字体:黑体
iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 8, iTextSharp.text.Font.NORMAL);
iTextSharp.text.Font fontHeader = new iTextSharp.text.Font(baseFont, 12, iTextSharp.text.Font.NORMAL);
//添加内容
PdfPTable table = new PdfPTable(Data.Columns.Count);
for (int i = 0; i < Data.Rows.Count; i++)
{
for (int j = 0; j < Data.Columns.Count; j++)
{
table.AddCell(new Phrase(Data.Rows[i][j].ToString(), font)); }
} document.Add(table);
document.Close();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString() + "-" + DateTime.Now.ToShortDateString() + ".pdf");
HttpContext.Current.Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
HttpContext.Current.Response.OutputStream.Flush();
HttpContext.Current.Response.OutputStream.Close();
}
catch (DocumentException ex)
{
HttpContext.Current.Response.Write(ex.Message);
}
}
希望谁能给我个DEMO 那我就乐翻了
我先试试一楼的 再次谢过各位