在JavaSwing中生成报表pdf,表
我在从程序生成pdf报告时遇到问题。我不知道怎么做,我用iText试过了,但是错误突然出现了。我在我的程序中创建了表,其中我有4列并添加了更多行,我希望程序在按下按钮后从这个表生成一个报告
DefaultTableModel model = (DefaultTableModel) tabela.getModel();
String path="";
JFileChooser j= new JFileChooser();
j.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
j.showSaveDialog(this);
int x=j.showOpenDialog(this);
if(x==JFileChooser.APPROVE_OPTION){
path=j.getSelectedFile().getPath();
}
Document doc= new Document();
try {
PdfWriter.getInstance(doc, new FileOutputStream(path+"raport.pdf"));
doc.open();
PdfPTable tbl=new PdfPTable(4);
tbl.addCell("column1");
tbl.addCell("column2");
tbl.addCell("column3");
tbl.addCell("column4");
for(int i=0; i<table.getRowCount(); i++){
String column1=table.getValueAt(i, 0).toString();
String column2=table.getValueAt(i, 1).toString();
String column3=table.getValueAt(i, 2).toString();
String column4=table.getValueAt(i, 3).toString();
tbl.addCell(column1);
tbl.addCell(column2);
tbl.addCell(column3);
tbl.addCell(column4);
}
doc.add(tbl);
}
catch (FileNotFoundException ex){
Logger.getLogger(Energy.class.getName()).log(Level.SEVERE, null, ex);
} catch (DocumentException ex) {
Logger.getLogger(Energy.class.getName()).log(Level.SEVERE, null, ex);
}
# 1 楼答案
不要忘记调用
doc.close();
关闭流/完成保存