如何将网页中的datagridviiew数据转换为PDF文件 [英] how to convert datagridviiew data in web page to PDF file

查看:100
本文介绍了如何将网页中的datagridviiew数据转换为PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生,

这是萨蒂亚.
我正在使用Microsoft技术,但是在将网页中的数据DataGridView转换为PDF文件时遇到问题.

请帮帮我吗?

Hi Sir,

This is Satya.
I am working with Microsoft technologies and I have a problem on converting data DataGridView in web page to PDF file.

Please help me?

推荐答案

受保护的void Page_Load(对象发送者,EventArgs e)
{
//获取与报告页面上的datagrid [报告视图]相同的数据
//然后将"ContentType"设置为"application/vnd.ms-excel",这将生成.XSL文件

//-从SQL检索报表,放入DataSet,然后将其Bind()到DataGrid
SqlConnection conn =新的SqlConnection(strConn);
conn.Open();
SqlDataAdapter cmd1 =新的SqlDataAdapter("EXEC [dbo].[spStatReport] @ CompanyID =" +会话["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;


//-创建文件---------
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();

//-对于PDF取消注释以下几行----------
//Response.ContentType ="application/pdf";
//Response.AddHeader("content-disposition," attachment; filename = FileName.pdf);

//-对于MS Excel,请取消注释以下几行----------
//Response.ContentType ="application/vnd.ms-excel";
//Response.AddHeader("content-disposition," attachment; filename = FileName.xls);

//-对于MS Word,请取消注释以下几行------------
//Response.ContentType ="application/vnd.word";
//Response.AddHeader("content-disposition," attachment; filename = FileName.doc);

//-对于CSV取消注释以下行----------
//Response.ContentType ="text/csv";
//Response.AddHeader("content-disposition," attachment; filename = FileName.csv);

//-对于TXT,请取消注释以下几行----------
//Response.ContentType ="text/plain";
//Response.AddHeader("content-disposition," attachment; filename = FileName.txt);


EnableViewState = false;

StringWriter sw = new StringWriter();
HtmlTextWriter hw =新的HtmlTextWriter(sw);

//-呈现DataGrid,然后将其转储到HtmlTextWriter控件中
dtaFinal.RenderControl(hw);

//-使用响应对象将StringWriter写入页面
Response.Write(sw.ToString());
Response.Flush();
Response.Close();
Response.End();
}
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();
}


这篇关于如何将网页中的datagridviiew数据转换为PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆