2017-09-18 43 views
-3

首先,我將數據放在粗糙的gridview中。
數據是在數據庫上運行存儲過程的結果而獲得的。
然後,我嘗試在表格中添加此數據並嘗試將數據導出爲pdf。使用iText將數據放入表格中

這是我的代碼:

Dim GridView1 As New GridView 
    Dim pdfDoc As New iTextSharp.text.Document(iTextSharp.text.PageSize.A1, 10.0F, 10.0F, 10.0F, 0.0F) 
    pdfDoc.Open() 
    Dim pdfTable As New PdfPTable(3) 
    For Each row As GridViewRow In GridView1.Rows 
     Dim Val1 As String = row.Cells(0).Text 
     Dim Val2 As String = row.Cells(1).Text 
     Dim Val3 As String = row.Cells(2).Text 
    Next 
    pdfDoc.Add(pdfTable) 
    pdfDoc.Close() 

現在我該怎樣將這些值(VAL1,VAL2,VAL3)添加到表,然後導出爲PDF?

請幫忙嗎?

回答

1

這是一些iText代碼來處理表格。

Document document = new Document(); 
PdfWriter.getInstance(document, new FileOutputStream(dest)); 
document.open(); 
PdfPTable table = new PdfPTable(8); 
for(int aw = 0; aw < 16; aw++){ 
    table.addCell("hi"); 
} 
document.add(table); 
document.close(); 
+0

PS:此代碼是通過Google搜索「iText如何創建表格」獲得的。我得到的第一個鏈接是iText7的代碼示例,第二個鏈接是iText5的示例。 請考慮將**更多的努力**在未來自己解決問題。 –

1

這是錯誤的:

Dim pdfTable As New PdfPTable(3) 
For Each row As GridViewRow In GridView1.Rows 
    Dim Val1 As String = row.Cells(0).Text 
    Dim Val2 As String = row.Cells(1).Text 
    Dim Val3 As String = row.Cells(2).Text 
Next 

這是錯誤的,因爲你只是定義三個string值,而不做這些值有任何。爲什麼會這樣工作?你的代碼沒有任何反應。

您需要將這些string值添加到您剛剛定義的table

Dim pdfTable As New PdfPTable(3) 
For Each row As GridViewRow In GridView1.Rows 
    pdfTable.Add(row.Cells(0).Text) 
    pdfTable.Add(row.Cells(1).Text) 
    pdfTable.Add(row.Cells(2).Text) 
Next 

或者更確切地說,是在回答解釋了這個問題:dataGridView to pdf with itextsharp

PdfPTable pdfTable= new PdfPTable(3); 
foreach(DataGridViewRow row in dataGridView1.Rows) { 
    foreach (DataGridViewCell celli in row.Cells) { 
     pdfTable.AddCell(celli.Value.ToString()); 
    } 
} 
doc.Add(pdfTable); 

你需要申請小的變化到端口這個C#代碼段VBA代碼,但不應該是一個問題。

也不是說如何定義PdfPTable的單元格樣式有很多變體。它們記錄在官方網站上。

+0

我使用了itext7 .... –

相關問題