在 java 中生成报告 pdf - swing,table
Generate report pdf in java - swing, table
我在从我的程序生成 pdf 报告时遇到问题。我不知道该怎么做,我尝试使用 iText,但弹出错误。我在我的程序中创建了 tables,其中有 4 列并添加了更多行,我希望程序在按下按钮后根据此 table 生成报告。
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);
}
不要忘记调用 doc.close();
关闭流/完成保存。
我在从我的程序生成 pdf 报告时遇到问题。我不知道该怎么做,我尝试使用 iText,但弹出错误。我在我的程序中创建了 tables,其中有 4 列并添加了更多行,我希望程序在按下按钮后根据此 table 生成报告。
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);
}
不要忘记调用 doc.close();
关闭流/完成保存。