在不使用隐藏网格的情况下导出Dataview [英] Exporting Dataview without using a hidden grid

查看:78
本文介绍了在不使用隐藏网格的情况下导出Dataview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在使用隐藏的网格导出表格.有什么方法可以将数据视图直接导出到excel,而无需使用网格或在页面中绘制表格?直接将其写入文件,而无需先将其渲染到网格中.无论如何,在我看来,这似乎是一个更好的主意.


是的,您可以将表数据导出为PDF,Excel,Word和HTML的多个文档.我正在为您提供用于单独文档的方法.

受保护的void ExportToExcel(DataTable datatable)//对于EXcel
{
字符串附件=附件;文件名= PaymentFile.xls";
Response.ClearContent();
Response.AddHeader("content-disposition",附件);
Response.ContentType ="application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw =新的HtmlTextWriter(sw);
GridView gridview1 =新的GridView();
gridview1.DataSource =数据表;
gridview1.DataBind();
gridview1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}


受保护的void ExportToPDF(DataTable datatable)//对于PDF
{
如果(datatable.Rows.Count> 0)
{
PdfDocument BranchDoc =新的PdfDocument(PdfDocumentFormat.Letter_8_5x11_Horizo​​ntal);
PdfTable表= BranchDoc.NewTable(新Font("Vardana",7),datatable.Rows.Count,datatable.Columns.Count,4.0);
table.ImportDataTable(datatable);
table.HeadersRow.SetColors(Color.White,Color.Navy);
table.SetColors(Color.Black,Color.White,Color.Gainsboro);
table.SetBorders(Color.Black,1,BorderType.CompleteGrid);
table.SetColumnsWidth(new int [] {6,4,4,4,5,6,6,4,7,9,4,4,5,6,6,5,5,5,5,6}) ;
table.SetContentAlignment(ContentAlignment.MiddleCenter);
table.Columns [1] .SetContentAlignment(ContentAlignment.MiddleLeft);
while(!table.AllTablePagesCreated)
{
PdfPage newPage = BranchDoc.NewPage();
PdfTablePage newPdfTablePage = table.CreateTablePage(new PdfArea(BranchDoc,8,80,780,700));
PdfTextArea pta =新的PdfTextArea(新的Font("Verdana",26,FontStyle.Bold),Color.Red,新的PdfArea(BranchDoc,20,5,5,595,120),ContentAlignment.MiddleCenter,收入对帐付款文件") ;
newPage.Add(newPdfTablePage);
newPage.Add(pta);
newPage.SaveToDocument();
}
Response.ClearHeaders();
Response.AppendHeader("Content-disposition",string.Format("attachment; filename = {0}","PaymentFile.pdf")));
Response.ContentType =应用程序/pdf";
BranchDoc.SaveToStream(Response.OutputStream);
Response.End();
}
其他
{
lblMsg.Text =没有数据可导出";
}

}



受保护的void ExportToWord(DataTable datatable)//对于Word
{
字符串附件=附件;文件名= Payment.doc";
Response.ClearContent();
Response.AddHeader("content-disposition",附件);
Response.ContentType ="application/ms-word";
StringWriter sw = new StringWriter();
HtmlTextWriter htw =新的HtmlTextWriter(sw);
GridView gridview1 =新的GridView();
gridview1.DataSource =数据表;
gridview1.DataBind();
gridview1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}



受保护的void ExportToHTML(DataTable datatable)//对于HTML
{
字符串附件=附件;内联;文件名= Payment.html";
Response.ClearContent();
Response.AddHeader("content-disposition",附件);
Response.ContentType ="application/html";
StringWriter sw = new StringWriter();
HtmlTextWriter htw =新的HtmlTextWriter(sw);
GridView gridview1 =新的GridView();
gridview1.DataSource =数据表;
gridview1.DataBind();
gridview1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

我已经使用了这些功能,并且可以正常工作.所以我认为这对您会有所帮助.


谢谢与问候
Lalit Kumar
新德里
印度


我找到了库PdfDocument:confused:


Hi, right now i am exporting a table using a hidden grid. Is there any way we can export a dataview directly to excel without using grid or drawing a table in page???

解决方案

Yes, you can iterate over your data source directly, and write your file from that without rendering it into a grid first. In many ways, that seems like a better idea to me anyhow.


Yes you can export table data into multiple document as PDF, Excel, Word and HTML. I am giving you methods for separate document..

protected void ExportToExcel(DataTable datatable) // For EXcel
{
string attachment = "attachment; filename=PaymentFile.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView gridview1 = new GridView();
gridview1.DataSource = datatable;
gridview1.DataBind();
gridview1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}


protected void ExportToPDF(DataTable datatable) // For PDF
{
if (datatable.Rows.Count > 0)
{
PdfDocument BranchDoc = new PdfDocument(PdfDocumentFormat.Letter_8_5x11_Horizontal);
PdfTable table = BranchDoc.NewTable(new Font("Vardana", 7), datatable.Rows.Count, datatable.Columns.Count, 4.0);
table.ImportDataTable(datatable);
table.HeadersRow.SetColors(Color.White, Color.Navy);
table.SetColors(Color.Black, Color.White, Color.Gainsboro);
table.SetBorders(Color.Black, 1, BorderType.CompleteGrid);
table.SetColumnsWidth(new int[] { 6,4,4,4,5,6,6,4,7,9,4,4,5,6,6,5,5,5,5,6});
table.SetContentAlignment(ContentAlignment.MiddleCenter);
table.Columns[1].SetContentAlignment(ContentAlignment.MiddleLeft);
while (!table.AllTablePagesCreated)
{
PdfPage newPage = BranchDoc.NewPage();
PdfTablePage newPdfTablePage = table.CreateTablePage(new PdfArea(BranchDoc, 8, 80, 780, 700));
PdfTextArea pta = new PdfTextArea(new Font("Verdana", 26, FontStyle.Bold), Color.Red, new PdfArea(BranchDoc, 20, 5, 595, 120), ContentAlignment.MiddleCenter, "Revenue Reconcilation-Payment File");
newPage.Add(newPdfTablePage);
newPage.Add(pta);
newPage.SaveToDocument();
}
Response.ClearHeaders();
Response.AppendHeader("Content-disposition", string.Format("attachment;filename={0}", "PaymentFile.pdf"));
Response.ContentType = "application/pdf";
BranchDoc.SaveToStream(Response.OutputStream);
Response.End();
}
else
{
lblMsg.Text = "No Data for Export";
}

}



protected void ExportToWord(DataTable datatable) // For Word
{
string attachment = "attachment; filename=Payment.doc";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-word";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView gridview1 = new GridView();
gridview1.DataSource = datatable;
gridview1.DataBind();
gridview1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}



protected void ExportToHTML(DataTable datatable) // For Html
{
string attachment = "attachment; inline; filename=Payment.html";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/html";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView gridview1 = new GridView();
gridview1.DataSource = datatable;
gridview1.DataBind();
gridview1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}

I have used these functions and it is working properly. so i think it will be helpfull for you.


Thanks & Regards
Lalit Kumar
New Delhi
India


I find the library PdfDocument :confused:


这篇关于在不使用隐藏网格的情况下导出Dataview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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