1. 程式人生 > >從前臺獲取資料,並匯出PDF檔案

從前臺獲取資料,並匯出PDF檔案

/**
 * 房帖調整 ,生成pdf檔案
 */
public static void fttzPdfFile(Object[][] data,String path){
 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
 Date date = new Date();
 String nowtime = sdf.format(date);
 String year = nowtime.substring(0,4);
 String month = nowtime.substring(4,6);
 try {
  //建立檔案
   Document document=new Document(new RectangleReadOnly(842F,595F));
   //建立一個書寫器
   PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
   BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false); 
   Font fontChinese = new Font(bfChinese,10, Font.NORMAL);  
   //開啟檔案
   document.open();
   //新增內容
   Paragraph par = new Paragraph("住房補貼調整通知單--"+year+"年"+month+"月",fontChinese);
   par.setAlignment(Element.ALIGN_CENTER);
   document.add(par);
   // 8列的表.
   PdfPTable table = new PdfPTable(10);
   float[] columnWidths = { 1f, 1f, 1f , 1f, 1f , 1f , 1f , 1f ,1f ,1f};
   table.setWidths(columnWidths);
   //table.setTotalWidth(500f);//設定表格的總寬度
         
   table.setWidthPercentage(105); // 寬度100%填充
   table.setSpacingBefore(1f); // 前間距
   table.setSpacingAfter(1f); // 後間距
   List<PdfPRow> listRow = table.getRows();
         
   //行1
   PdfPCell cells1[]= new PdfPCell[10];
   PdfPRow row1 = new PdfPRow(cells1);
   //單元格
   cells1[0] = new PdfPCell(new Paragraph("人員編號",fontChinese));//單元格內容
   /*cells1[0].setPaddingLeft(20);//左填充20
    cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
    cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
*/         
    cells1[1] = new PdfPCell(new Paragraph("姓名",fontChinese));
    cells1[2] = new PdfPCell(new Paragraph("單位",fontChinese));
    cells1[3] = new PdfPCell(new Paragraph("校內房帖時間",fontChinese));
    cells1[4] = new PdfPCell(new Paragraph("原房帖",fontChinese));
    cells1[5] = new PdfPCell(new Paragraph("現房貼",fontChinese));
    cells1[6] = new PdfPCell(new Paragraph("補發",fontChinese));
    cells1[7] = new PdfPCell(new Paragraph("備註",fontChinese));
    cells1[8] = new PdfPCell(new Paragraph("",fontChinese));
    cells1[9] = new PdfPCell(new Paragraph("",fontChinese));
    cells1[7].setColspan(3); //合併列
    for (int j = 0; j < 10; j++) {
        cells1[j].setMinimumHeight(20);//設定表格行高
        cells1[j].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
        cells1[j].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
   }
        //把第一行新增到集合
        listRow.add(row1);
         
    for(int i =0;i<data.length;i++){
    PdfPCell cells[]= new PdfPCell[10];
    PdfPRow row = new PdfPRow(cells);
    for(int j = 0;j<8;j++){
        cells[j] = new PdfPCell(new Paragraph(String.valueOf(data[i][j]),fontChinese));//單元格內容
        cells[j].setMinimumHeight(20);//設定表格行高
        cells[j].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中
        cells[j].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中
        if (j==7) {
           cells[7].setColspan(3); //合併列
    }
      }
        listRow.add(row);
      }
   //把表格新增到檔案中
  document.add(table);
   //關閉文件
  document.close();
   //關閉書寫器
  writer.close();
 } catch (Exception e) {
  e.printStackTrace();
 }
}