【问题标题】:Convert/Export C# datalist control/(html page) as a pdf将 C# datalist 控件/(html 页面)转换/导出为 pdf
【发布时间】:2009-06-24 22:09:33
【问题描述】:

我想创建一个按钮选项来获取整个数据列表并将其转换为 pdf 文件。正如任何人在 asp.net 中所做的那样?请您举个例子或以正确的方式指导我。

【问题讨论】:

    标签: c# asp.net export pdf-generation datalist


    【解决方案1】:

    您可以尝试使用 PDFSharp 一个非常简单的 .net pdf 库来开发您的解决方案

    【讨论】:

    • ITextSharp 是另一种选择。
    【解决方案2】:

    您可以尝试this 并将内容类型更改为 PDF。

    【讨论】:

      【解决方案3】:

      刚刚发现一个人在论坛上发布的代码,虽然我可能会与其他人分享

      Re: 在 C#/Asp.Net 中将数据网格导出为 PDF

      //*************************************************
      // 
      // Author:
      //  Ryan Van Aken (vanakenr@msn.com)
      // (C) 2009 Ryan Van Aken
      // 
      //
      // Permission is hereby granted, free of charge, to any person obtaining
      // a copy of this software and associated documentation files (the
      // "Software"), to deal in the Software without restriction, including
      // without limitation the rights to use, copy, modify, merge, publish,
      // distribute, sublicense, and/or sell copies of the Software, and to
      // permit persons to whom the Software is furnished to do so, subject to
      // the following conditions:
      // 
      // The above copyright notice and this permission notice shall be
      // included in all copies or substantial portions of the Software.
      // 
      // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
      // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
      // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
      // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
      // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      //
      //*************************************************
      
      
      
      //SQL Connection Settings -----------
      public string strConn = ConfigurationManager.ConnectionStrings["BLAH-Here"].ConnectionString;
      //-----------------------------------
      
      protected void Page_Load(object sender, EventArgs e)
      {
      //Grab the same data as the datagrid [report view] on the reporting page
      //Then set the "ContentType" to "application/vnd.ms-excel" which will generate the .XSL file
      
      //---Retrieve the Report from SQL, drop into DataSet, then Bind() it to a DataGrid
      SqlConnection conn = new SqlConnection(strConn);
      conn.Open();
      SqlDataAdapter cmd1 = new SqlDataAdapter("EXEC [dbo].[spStatReport] @CompanyID=" + Session["CompanyID"] + ", @StatReportID=" + Request.QueryString["ReportID"].ToString() + ", @StartDate='" + Request.QueryString["StartDate"].Replace("-", "/").ToString() + "', @EndDate='" + Request.QueryString["EndDate"].Replace("-", "/").ToString() + "';", conn);
      cmd1.SelectCommand.CommandType = CommandType.Text;
      DataSet dsReports = new DataSet("tblReporting");
      cmd1.Fill(dsReports);
      conn.Close();
      
      DataGrid dtaFinal = new DataGrid();
      dtaFinal.DataSource = dsReports.Tables[0];
      dtaFinal.DataBind();
      
      dtaFinal.HeaderStyle.ForeColor = System.Drawing.Color.White;
      dtaFinal.HeaderStyle.BackColor = System.Drawing.Color.DarkGray;
      dtaFinal.ItemStyle.BackColor = System.Drawing.Color.White;
      dtaFinal.AlternatingItemStyle.BackColor = System.Drawing.Color.AliceBlue;
      
      
      //---Create the File---------
      Response.Buffer = true;
      Response.ClearContent();
      Response.ClearHeaders();
      
      //---For PDF uncomment the following lines----------
      //Response.ContentType = "application/pdf";
      //Response.AddHeader("content-disposition", "attachment;filename=FileName.pdf");
      
      //---For MS Excel uncomment the following lines----------
      //Response.ContentType = "application/vnd.ms-excel";
      //Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
      
      //---For MS Word uncomment the following lines----------
      //Response.ContentType = "application/vnd.word";
      //Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
      
      //---For CSV uncomment the following lines----------
      //Response.ContentType = "text/csv";
      //Response.AddHeader("content-disposition", "attachment;filename=FileName.csv");
      
      //---For TXT uncomment the following lines----------
      //Response.ContentType = "text/plain";
      //Response.AddHeader("content-disposition", "attachment;filename=FileName.txt");
      
      
      EnableViewState = false;
      
      StringWriter sw = new StringWriter();
      HtmlTextWriter hw = new HtmlTextWriter(sw);
      
      //---Renders the DataGrid and then dumps it into the HtmlTextWriter Control
      dtaFinal.RenderControl(hw);
      
      //---Utilize the Response Object to write the StringWriter to the page
      Response.Write(sw.ToString());
      Response.Flush();
      Response.Close();
      Response.End();
      }
      

      【讨论】:

      • 它工作得很好,我下载了 PDF,但是当我试图打开它时,Adobe Reader 告诉我文件已损坏或其他原因。无法读取文件。不过,这比我迄今为止看到的任何其他事情都取得了更大的进步
      【解决方案4】:

      这很好接受,尽管我在网站上设置了边距,但它并没有为我设置边距。无论如何要为这部分的 Word 做这件事吗?我还发现 PDF 部分不起作用。

      (大出口帖)

      【讨论】:

        猜你喜欢
        • 2011-01-13
        • 1970-01-01
        • 2019-08-04
        • 2014-08-21
        • 1970-01-01
        • 1970-01-01
        • 2018-03-02
        • 2011-07-01
        相关资源
        最近更新 更多