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 테이블을 생성하는 경우 행의 특정 열에 데이터를 삽입 할 수 있으므로이 작업을 매우 쉽게 수행 할 수 있습니다. 내가 한 코드 스 니펫 -

//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")); 
    } 

iText에서이를 수행 할 수있는 방법이 있습니까? (특정 열에 데이터를 삽입하려면)

답변

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(""); 
} 
을 수행 할 수 있습니다