转换/导出C#datalist control /(html page)作为pdf

时间:2021-02-14 23:20:00

I want create a button option that takes the entire datalist and converts it to a pdf file. As anyone done this in asp.net ? Please can you show an example or direct me in the right way.

我想创建一个按钮选项,它接受整个datalist并将其转换为pdf文件。任何人都在asp.net中这样做过吗?请你能举一个例子或以正确的方式指导我。

4 个解决方案

#1


You can try develop your solution using PDFSharp a very simple pdf library for .net

您可以尝试使用PDFSharp开发您的解决方案,这是一个非常简单的.net文件库

#2


You could try this and change the content type to PDF.

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

#3


Just found a snippet of code a guy posted in forum, and though i might share with others

刚刚在论坛中发现了一段代码,尽管我可能会与其他人分享

Re: EXPORT DATAGRID TO PDF in C#/Asp.Net

回复:在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();
}

#4


This worked out great accept it does not set margins for me despite what I set them on the site. Is there anyways to do this for the Word part of this? Also i find the PDF part does not work.

尽管我在网站上设置了它,但这并不能为我设置利润。反正有没有为这部分的Word部分做这个?另外,我发现PDF部分不起作用。

(The big export post)

(出口大的帖子)

#1


You can try develop your solution using PDFSharp a very simple pdf library for .net

您可以尝试使用PDFSharp开发您的解决方案,这是一个非常简单的.net文件库

#2


You could try this and change the content type to PDF.

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

#3


Just found a snippet of code a guy posted in forum, and though i might share with others

刚刚在论坛中发现了一段代码,尽管我可能会与其他人分享

Re: EXPORT DATAGRID TO PDF in C#/Asp.Net

回复:在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();
}

#4


This worked out great accept it does not set margins for me despite what I set them on the site. Is there anyways to do this for the Word part of this? Also i find the PDF part does not work.

尽管我在网站上设置了它,但这并不能为我设置利润。反正有没有为这部分的Word部分做这个?另外,我发现PDF部分不起作用。

(The big export post)

(出口大的帖子)