本文介绍了我们如何使用ASP.NET/C#将数据表导出为PDF并保存在服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧这个问题似乎重复但我在谷歌的帮助下尝试了一些代码,它对我不起作用。实际上它生成了空白的pdf文件。

我想将数据表导出为PDF文件,并将文件保存在我的Web应用程序的serverside文件夹中。为此我只想使用ajax调用和expoet datatable调用web服务方法到pdf文件并将其保存到服务器端并返回文件名,并在ajax sucess上打印客户端的pdf文件。任何人都可以建议我另一个更好的方式。



我尝试过:



//我的网络服务



[ScriptMethod(UseHttpGet = false,ResponseFormat = ResponseFormat.Json)]

公共字符串ExportPdfReport (DataTable dt)

{

PdfWriter writer = PdfWriter.GetInstance(document,new FileStream(strUploadPath +/+ strFilename,FileMode.Create));

document.Open();

Font font5 = FontFactory.GetFont(FontFactory.HELVETICA,5);



PdfPTable table = new PdfPTable(dt.Columns.Count);

PdfPRow row = null;

float [] widths = new float [] {4f,4f,4f,4f, 4f,4f,4f,4f,4f,4f,4f};



table.SetWidths(宽度);



table.WidthPercentage = 100;

int iCol = 0;

string colname =;

PdfPCell cell = null;



cell.Colspan = dt.Columns.Count;



foreach(DataColumn) c in dt.Columns)

{



table.AddCell(new Phrase(c.ColumnName,font5));

}



foreach(DataRow r in dt.Rows)

{

if(dt) .Rows.Count> 0)

{

table.AddCell(new Phrase(r [0] .ToString(),font5));

table.AddCell (新词组(r [1] .ToString(),font5));

table.AddCell(新词组(r [2] .ToString(),font5));

table.AddCell(new Phrase(r [3] .ToString(),font5));

}

} document.Add(table);

document.Close();



返回strFilename;

}

Well this question seems to be repeated but i tried some code with the help of google and it dont work for me. actually it generate blank pdf file.
i want to export datatable to PDF file and save the file in serverside folder in my web application. for that i just want to call the the web service method using ajax call and expoet datatable to pdf file and save it to server side and return the file name and on ajax sucess i print the pdf file on client side. can any one suggest me the other better way.

What I have tried:

//my web service

[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string ExportPdfReport(DataTable dt)
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(strUploadPath+"/"+strFilename, FileMode.Create));
document.Open();
Font font5 = FontFactory.GetFont(FontFactory.HELVETICA, 5);

PdfPTable table = new PdfPTable(dt.Columns.Count);
PdfPRow row = null;
float[] widths = new float[] { 4f, 4f, 4f, 4f,4f, 4f, 4f, 4f,4f, 4f, 4f };

table.SetWidths(widths);

table.WidthPercentage = 100;
int iCol = 0;
string colname = "";
PdfPCell cell = null;

cell.Colspan = dt.Columns.Count;

foreach (DataColumn c in dt.Columns)
{

table.AddCell(new Phrase(c.ColumnName, font5));
}

foreach (DataRow r in dt.Rows)
{
if (dt.Rows.Count > 0)
{
table.AddCell(new Phrase(r[0].ToString(), font5));
table.AddCell(new Phrase(r[1].ToString(), font5));
table.AddCell(new Phrase(r[2].ToString(), font5));
table.AddCell(new Phrase(r[3].ToString(), font5));
}
} document.Add(table);
document.Close();

return strFilename;
}

推荐答案


这篇关于我们如何使用ASP.NET/C#将数据表导出为PDF并保存在服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 08:19