itextSharp를 사용하여 그리드보기 데이터를 내보내고 싶습니다. 여기 내 코드입니다 어떻게 수출 표보기를 Excel 로이 기술을 사용하고 itextSharp를 사용하여 테이블을 만드는 방법입니다.itextSharp를 사용하여 그리드보기 데이터를 PDF로 내보내는 방법
이 솔루션은 나를 위해 작동합니다 : 난 대답 아래이 스크린 샷이 포함
protected void ibtnxls1_Click(object sender, ImageClickEventArgs e)
{
DataTable dt = (DataTable)Session["PostTable"];
string fileName = DateTime.Now.ToString("ddMMyyyy_HHmmss");
string attachment = "attachment; filename=" + fileName + ".xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "applicssssation/vnd.ms-excel";
string sTab = "";
foreach (DataColumn dc in dt.Columns)
{
HttpContext.Current.Response.Write(sTab + dc.ColumnName);
sTab = "\t";
}
HttpContext.Current.Response.Write("\n");
int i;
foreach (DataRow dr1 in dt.Rows)
{
sTab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
HttpContext.Current.Response.Write(sTab + dr1[i].ToString());
sTab = "\t";
}
HttpContext.Current.Response.Write("\n");
}
HttpContext.Current.Response.End();
}
:
이
내 코드는이 방법을 시도DataTable dt = (DataTable)Session["PostTable"];
iTextSharp.text.Table table = new iTextSharp.text.Table(dt.Columns.Count);
table.Cellpadding = 2;
table.Width = 100;
//Transfer rows from GridView to table
for (int i = 0; i < dt.Columns.Count; i++)
{
string cellText = Server.HtmlDecode(dt.Columns[i].ToString());
iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
cell.BackgroundColor = new Color(System.Drawing.ColorTranslator.FromHtml("#FFFFFF"));
table.AddCell(cell);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
string cellText = Server.HtmlDecode(dt.Rows[i][j].ToString());
iTextSharp.text.Cell cell = new iTextSharp.text.Cell(cellText);
//Set Color of Alternating row
if (i % 2 != 0)
{
cell.BackgroundColor = new Color(System.Drawing.ColorTranslator.FromHtml("#FFFFFF"));
}
table.AddCell(cell);
}
}
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
pdfDoc.Add(table);
pdfDoc.Close();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;" +
"filename=" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
데이터를 Excel 또는 PDF로 내보내시겠습니까? –
이것은 표보기 데이터를 내보내는 방법에 대한 코드 샘플입니다. 이제는이 논리를 사용하여 pdf로 내보내려고합니다. – Shal