通过.pdf下载Gridview值 [英] Download Gridview values by .pdf
本文介绍了通过.pdf下载Gridview值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨朋友们,
我有一个包含值的GirdView。现在我要将这些值下载为.pdf文件。还有Excel格式
Hi Friends,
I have a GirdView that contains values. Now i''m going to download those values as .pdf file. And also Excel formats
推荐答案
您好b $ b
使用iTextSharp将Gridview转换为PDF格式:
使用itextsharp将HTML转换为PDF [ ^ ]
对于Excel:
Hi
Use iTextSharp to conver Gridview to PDF:
HTML convert to PDF using itextsharp[^]
For Excel:
protected void btnExpExcel_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Report.xls"));
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
DataTable dt = new DataTable();
dt = (DataTable)Session["dtReport"];
gvMISReport.AllowPaging = false;
gvMISReport.DataSource = dt;
gvMISReport.DataBind();
//Change the Header Row back to white color
gvMISReport.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Applying stlye to gridview header cells
for (int i = 0; i < gvMISReport.HeaderRow.Cells.Count; i++)
{
gvMISReport.HeaderRow.Cells[i].Style.Add("background-color", "#157CB0");
}
int j = 1;
//This loop is used to apply stlye to cells based on particular row
foreach (GridViewRow gvrow in gvMISReport.Rows)
{
gvrow.BackColor = Color.White;
if (j <= gvMISReport.Rows.Count)
{
if (j % 2 != 0)
{
for (int k = 0; k < gvrow.Cells.Count; k++)
{
gvrow.Cells[k].Style.Add("background-color", "#EFF3FB");
}
}
}
j++;
}
gvMISReport.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
亲爱的朋友,
传递数据集,文件名和工作表名称......... ..
查看以下代码
Dear friend,
Pass dataset, filename and Worksheet name ...........
Look into the following code
private void ExportToExcel(DataTable dt, string fileName, string worksheetName)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + fileName + "");
Response.ContentType = "application/vnd.ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
DataGrid dataExportExcel = new DataGrid();
dataExportExcel.ItemDataBound += new DataGridItemEventHandler(dataExportExcel_ItemDataBound);
dataExportExcel.DataSource = dt;
dataExportExcel.DataBind();
dataExportExcel.RenderControl(htmlWrite);
StringBuilder sbResponseString = new StringBuilder();
sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html;charset=windows-1252\"><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>" + worksheetName + "</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>");
sbResponseString.Append(stringWriter + "</body></html>");
Response.Write(sbResponseString.ToString());
Response.End();
}
void dataExportExcel_ItemDataBound(object sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
//Header Text Format can be done as follows
e.Item.Font.Bold = true;
//Adding Filter/Sorting functionality for the Excel
int cellIndex = 0;
while (cellIndex < e.Item.Cells.Count)
{
e.Item.Cells[cellIndex].Attributes.Add("x:autofilter", "all");
e.Item.Cells[cellIndex].Width = 200;
e.Item.Cells[cellIndex].HorizontalAlign = HorizontalAlign.Center;
cellIndex++;
}
}
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
int cellIndex = 0;
while (cellIndex < e.Item.Cells.Count)
{
//Any Cell specific formatting should be done here
e.Item.Cells[cellIndex].HorizontalAlign = HorizontalAlign.Left;
cellIndex++;
}
}
}
这篇关于通过.pdf下载Gridview值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文