Kaskus

Tech

rds081295Avatar border
TS
rds081295
convert xlsx to pdf using POI itext java
gan mau tanya, ane ada tugas buat convert xlsx ke pdf. tp hasilnya belum sesuai yg diharapkan .. supaaya kalau hasilnya mirip hasil convert lewat microsoft excel gmna ya?
terus cell pada tabel nya buat autosize gmna ya gan. mohon bantuannya.

package backup;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.Properties;
import java.util.Scanner;

import javax.swing.JOptionPane;

import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class test {
int colNum;
int solrow;
int b;
Rectangle rect ;
public void test() throws IOException, EncryptedDocumentException, InvalidFormatException, DocumentException{
File excelFile = null, destFolder = null, config = new File(System.getProperty("user.dir") + "\\config.properties");
Properties prop = new Properties();
Scanner sc = null;
try {
String str = "";
if (config.exists()) {
sc = new Scanner(config);
while (sc.hasNextLine()) {
str += sc.nextLine() + "\n";
}
} else {
JOptionPane.showMessageDialog(null,config+ "\nincorrect or doesn't exist\nPlease make sure your config.properties is exist");
System.exit(0);
}
prop.load(new StringReader(str.replace("\\", "\\\\")));
destFolder = new File(prop.getProperty("Dest")emoticon-Wink;
excelFile = new File(prop.getProperty("File")emoticon-Wink;

FileInputStream inputstream = new FileInputStream(excelFile);
Workbook workbook = WorkbookFactory.create(inputstream);
Sheet firstSheet= null;
Row row = null ;
Cell cell;

for(int a=0;a < workbook.getNumberOfSheets();a++){
firstSheet = workbook.getSheetAt(a);
Iterator<Row> rowIterator = firstSheet.iterator();
colNum = firstSheet.getRow(a).getLastCellNum();
System.out.println(colNum);

solrow = firstSheet.getLastRowNum()+1;
System.out.println(solrow);

if(colNum>b){
b = colNum;
}

while(rowIterator.hasNext()) {
row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
cell = cellIterator.next();
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC:
cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_BLANK:
cell.getStringCellValue();
break;
}
}
}
}
System.out.println(b);
if(b>8){
rect = new Rectangle(PageSize.A4.rotate());
} else {
rect = new Rectangle(PageSize.A4.rotate());
}

Document iText_xls_2_pdf = new Document(rect, 100f, 100f, 100f, 0f);
PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream(destFolder));
iText_xls_2_pdf.open();

for(int a=0;a < workbook.getNumberOfSheets();a++){

firstSheet = workbook.getSheetAt(a);
//firstSheet.autoSizeColumn(9);

iText_xls_2_pdf.add(new Paragraph(" "+firstSheet.getSheetName()+"\n\n"));
Iterator<Row> rowIterator = firstSheet.iterator();
colNum = firstSheet.getRow(0).getLastCellNum();
System.out.println(colNum+"apa ini ya");
solrow = firstSheet.getLastRowNum()+1;
System.out.println(solrow);
PdfPTable my_table = new PdfPTable(colNum);
//iText_xls_2_pdf.newPage();
//my_table.setWidthPercentage(100);
PdfPCell table_cell = null;
int ctr ;
int clm ;
//firstSheet.autoSizeColumn(arg0);
while(rowIterator.hasNext()) {
row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
cell = cellIterator.next();
int columnIndex = cell.getColumnIndex();
firstSheet.autoSizeColumn(columnIndex);
switch(cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
table_cell=new PdfPCell(new Phrase(cell.getStringCellValue()));
my_table.addCell(table_cell);
break;
case Cell.CELL_TYPE_NUMERIC:
table_cell= new PdfPCell(new Phrase(""+cell.getNumericCellValue()));
my_table.addCell(table_cell);
break;
case Cell.CELL_TYPE_BLANK:
table_cell= new PdfPCell(new Phrase(cell.getStringCellValue()));
my_table.addCell(table_cell);
break;
}
}
}
iText_xls_2_pdf.add(my_table);
iText_xls_2_pdf.newPage();
}
iText_xls_2_pdf.close();
workbook.close();
inputstream.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Source excel " + excelFile + "\nincorrect or doesn't exist \n" + ex.getMessage());
} finally {
if (sc != null) {
sc.close();
}
}

}
public static void main(String[] args) throws Exception{
test te = new test();
te.test();
}
}
convert xlsx to pdf using POI itext java
nona212Avatar border
nona212 memberi reputasi
1
1.2K
0
GuestAvatar border
Komentar yang asik ya
GuestAvatar border
Komentar yang asik ya
Komunitas Pilihan