2017-09-23 7 views
0

にデータを挿入することができません私は、iTextののPDFライブラリを使用してテーブルを作成する必要があります。テーブルには3つの列があり、複数の行を持つことができます。いずれの行でも、任意のフィールドは空でも、値を持つこともできます。 私は、特定の列のセルを作成するためのiTextの中の道を見つけることができません。何が起こっているのかは、エントリがnullになった場合、次のカラムのデータは最初のものになります。 コードスニペット - 私は明示的にすべての可能なケースをチェックせずにこれを処理することができますどのようにiTextの:PDFPTable - 特定の列

//Printing First column data 
    if (!attributes.getJSONObject(i).isNull("First")) { 
     PdfPCell cell = new PdfPCell(new Phrase(attributes.getJSONObject(i).getString("First"), h4)); 
     table.addCell(cell); 
    } 
//Printing Second column data 
    if (!attributes.getJSONObject(i).isNull("Second ")) { 
     PdfPCell cell = new PdfPCell(new Phrase(attributes.getJSONObject(i).getString("Second "), h4)); 
     table.addCell(cell); 
    } 
//Printing Third column data 
    if (!attributes.getJSONObject(i).isNull("Third")) { 
     PdfPCell cell = new PdfPCell(new Phrase(attributes.getJSONObject(i).getString("Third"), h4)); 
     table.addCell(cell); 
    } 

ApacheのPOIを使用してExcelテーブルを生成するために、私は行の特定の列のデータを挿入することができるように、私は非常に簡単にこれを行うことができます。私が行ったExcelのコードスニペット -

//Printing First column data 
    if (!attributes.getJSONObject(i).isNull("First")) { 
    row.createCell(1); //This will insert in first column 
    row.getCell(1).setCellValue(attributes.getJSONObject(i).getString("First")); 
    } 
    //Printing Second column data 
    if(!attributes.getJSONObject(i).isNull("Second")) { 
    row.createCell(2); //This will insert in second column 
    row.getCell(2).setCellValue(attributes.getJSONObject(i).getString("Second")); 
    } 
    //Printing Third column data 
    if(!attributes.getJSONObject(i).isNull("Third")) { 
    row.createCell(3); //This will insert in third column 
    row.getCell(3).setCellValue(attributes.getJSONObject(i).getString("Third")); 
    } 

これを実現する方法はありますか?彼らが来るよう

答えて

1

iTextのは、細胞と、表のセルを充填する(特定の列にデータを挿入します)。したがって、空のセルをスキップすることはできません。しかし、なぜ単にそのような状況では、空のコンテンツでCellインスタンスを追加しませんか?例えば。単に

if(!attributes.getJSONObject(i).isNull("Second")) { 
    row.createCell(2); //This will insert in second column 
    row.getCell(2).setCellValue(attributes.getJSONObject(i).getString("Second")); 
} 

の代わりに、あなたは

row.createCell(2); //This will insert in second column 
if(!attributes.getJSONObject(i).isNull("Second")) { 
    row.getCell(2).setCellValue(attributes.getJSONObject(i).getString("Second")); 
} 

またはおそらく

row.createCell(2); //This will insert in second column 
if(!attributes.getJSONObject(i).isNull("Second")) { 
    row.getCell(2).setCellValue(attributes.getJSONObject(i).getString("Second")); 
} else { 
    row.getCell(2).setCellValue(""); 
} 
を行うことができます
関連する問題