Jhui_0508 2016-02-02 03:12 采纳率: 0%
浏览 2168
已结题

itext生成PDF 表格内容循环不出来 要不就是循环的JSON最后一个字段 传的是个JSON

图片说明
package com.pgs.core.util.print;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import net.sf.json.JSONArray;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.pgs.core.util.JsonUtil;
import com.pgs.manage.system.bean.MainAreaCode;

/**

  • Title: PrinterBHD.java Description:补货单
  • @author songtao
  • @created 2016-1-29 下午1:45:47
    */
    public class PrinterBHD {

    /**

    • 导出pdf
    • @throws Exception */

    public static boolean printBHD(String json,int rownumber) {

    /*
     * Vector v = new Vector();//dao.selectEx(sql, 0); Map info = (HashMap)
     * v.get(0);
     */
    
    // Map info = (HashMap)map.get("aaData");
    
    Rectangle rectPageSize = new Rectangle(PageSize.A4);
    rectPageSize = rectPageSize.rotate();
    Document doc = new Document(rectPageSize, 23, 25, 10, 10);
    Document document = null;
    try {
        BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 设置中文字体
    
        // 第一步:创建一个document对象。
        document = new Document();
        // 第二步:创建一个PdfWriter实例,将文件输出流指向一个文件。
        PdfWriter.getInstance(document, new FileOutputStream(
                "D:/test/1.pdf"));
    
        // 第三步:打开文档。
        document.open();
    
        rectPageSize = rectPageSize.rotate();
        PdfPCell cell = new PdfPCell();
        Font tf = new Font(bfChinese, 10, Font.NORMAL);
    
        // 标头
        Paragraph tpar = new Paragraph("供应商补货单", new Font(bfChinese, 18,
                Font.NORMAL));
        tpar.setAlignment(Paragraph.ALIGN_CENTER);
        // 描述.
        float[] w1 = { 50f, 50f, 50f };
        PdfPTable table = new PdfPTable(w1);
        table.setWidthPercentage(100);
    
        // json =
        // "{\"name\":\"admin\",\"retries\":\"3fff\",\"testname\":\"ddd\",\"testretries\":\"asdasdadsdasdasd\"} ";
        String sss = "[{\"realInspecAmount\":\"0.0000\",\"skuBrand\":\"小米\",\"index\":\"1\",\"skuSpec\":\"50g\",\"skuName\":\"康师傅方便面\",\"hasGift\":\"0\",\"realInspecCount\":\"90\", \"barcode\":\"6973342956023\",\"skuUnit\": \"壶\",\"channelSkuId\":\"56fb08ffc53611e5a4bab82a72d0db4b\",\"skuCode\":\"7334\",\"purchDetailId\":\"4\",\"billDate\":\"\2016-01-28 00:49:28.0\"}]";
        json = "{\"realInspecAmount\":\"0.0000\",\"skuBrand\":\"小米\",\"index\":\"1\",\"skuSpec\":\"50g\",\"skuName\":\"康师傅方便面\",\"hasGift\":\"0\",\"realInspecCount\":\"90\", \"barcode\":\"6973342956023\",\"skuUnit\": \"壶\",\"channelSkuId\":\"56fb08ffc53611e5a4bab82a72d0db4b\",\"skuCode\":\"7334\",\"purchDetailId\":\"4\",\"billDate\":\"\2016-01-28 00:49:28.0\"}";
        HashMap<String, Object> map = JsonUtil.json2Map(json);
    
        // 第一行
        cell = new PdfPCell(new Paragraph("供应商编码:" + map.get("realInspecAmount"), tf));
        cell.setBorder(0);
        cell.setPaddingTop(6f);
        table.addCell(cell);
    
        cell = new PdfPCell(
                new Paragraph("供应商名称:" + map.get("skuBrand"), tf));
        cell.setBorder(0);
        cell.setPaddingTop(6f);
        table.addCell(cell);
    
        cell = new PdfPCell(new Paragraph("送货地址:" + map.get("testretries"),
                tf));
        cell.setBorder(0);
        cell.setPaddingTop(6f);
        table.addCell(cell);
    
        // 第二行
        cell = new PdfPCell(new Paragraph("补货单号:" + "11111111", tf));
        cell.setBorder(0);
        cell.setPaddingBottom(6f);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("补货制单人:" + "11111111", tf));
        cell.setBorder(0);
        cell.setPaddingBottom(6f);
        table.addCell(cell);
        cell = new PdfPCell(
                new Paragraph("打印日期:" + map.get("billDate"), tf));
        cell.setBorder(0);
        cell.setPaddingBottom(6f);
        table.addCell(cell);
    
        // 表格
        float[] width = { 30f, 150f, 150f, 150f, 150f, 150f, 150f, 150f };
        PdfPTable table2 = new PdfPTable(width);
        table2.setWidthPercentage(100);
        // 头
        String[][] titles = { { "序号", "i", "" },
                { "商品编码 ", "sku_code", "" }, { "商品名称", "sku_name", "" },
                { "条码", "barcode", "" }, { "规格", "sku_spec", "" },
                { "销售单位", "sale_min_unit", "" }, { "需求数", "bhd_sum", "" },
                { "实收数", "", "" } };
        Paragraph content = null;
        for (int i = 0; i < titles.length; i++) {
            content = new Paragraph(titles[i][0], tf);
            content.setAlignment(Paragraph.ALIGN_LEFT);
            cell = new PdfPCell(content);
            table2.addCell(cell);
        }
    
        //String sss = "[{\"b\":\"55\",\"a\":\"55\"},{\"b\":\"55\",\"a\":\"55\"},{\"b\":\"55\",\"a\":\"55\"},{\"b\":\"55\",\"a\":\"55\"},{\"b\":\"55\",\"a\":\"55\"},{\"b\":\"55\",\"a\":\"55\"}]";
    
        ArrayList lll = (ArrayList) JSONArray.toList(
                JSONArray.fromObject(sss), HashMap.class);
    
        System.out.println(((HashMap) lll.get(0)).get("skuBrand"));
    
        //创建一个N列的表格控件  
        PdfPTable pdfTable2 = new PdfPTable(titles[7].length);
      //设置表格占PDF文档100%宽度  
        pdfTable2.setWidthPercentage(100);  
        //水平方向表格控件左对齐  
        pdfTable2.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);  
      //创建一个表格的表头单元格  
        PdfPCell pdfTableHeaderCell = new PdfPCell();
        PdfPCell pdfTableContentCell = new PdfPCell();
    
        pdfTableContentCell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
        pdfTableContentCell.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        //表格内容行数的填充  
        for(int i = 0;i < rownumber;i++){  
            for(String tableContentInfo : titles[1]){  
                pdfTableContentCell.setPhrase(new Paragraph(tableContentInfo, tf));  
                pdfTable2.addCell(pdfTableContentCell);  
            }  
        }  
     // 脚
                Paragraph fpar1 = new Paragraph("验货员:____________________"
                        + "供应商送货人: ____________________"
                        + "收货日期:____________________" + "合计 :_____", tf);
                // 第四步:在文档中增加一个段落。
                document.add(tpar);
                document.add(table);
                document.add(table2);
                document.add(pdfTable2);
                document.add(fpar1);
        return true;  
    }catch(FileNotFoundException de) {  
        de.printStackTrace();  
        System.err.println("pdf file: " + de.getMessage());  
        return false;  
    }catch(DocumentException de) {  
        de.printStackTrace();  
        System.err.println("document: " + de.getMessage());  
        return false;  
    }catch(IOException de) {  
        de.printStackTrace();  
        System.err.println("pdf font: " + de.getMessage());  
        return false;  
    }finally{  
        //关闭PDF文档流,OutputStream文件输出流也将在PDF文档流关闭方法内部关闭  
        if(document!=null){  
            document.close();  
        }
    }
    

    }
    public static void main(String[] args) {
    printBHD("", 3);
    }

}

    /*  for (int i = 0; i < lll.size(); i++) {

            for (int j = 0; j < titles.length; j++) {

                if (titles[j][1].equals("i"))

                    content = new Paragraph(String.valueOf(i+1), tf);

                else {

                    // 表格内容
                    for (Map.Entry<String, Object> entry : map.entrySet()) {

                        content = new Paragraph(entry.getValue().toString(), tf);
                        System.out.println(entry.getValue().toString());
                    }
                }

                content.setAlignment(Paragraph.ALIGN_CENTER);
                cell = new PdfPCell(content);
                table2.addCell(cell);
            }

        }*/

        /*// 脚

        Paragraph fpar1 = new Paragraph("验货员:____________________"
                + "供应商送货人: ____________________"
                + "收货日期:____________________" + "合计 :_____", tf);
        // 第四步:在文档中增加一个段落。
        document.add(tpar);
        document.add(table);
        document.add(table2);
        document.add(fpar1);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (document != null) {
            // 第五步:关闭文档。
            document.close();
        }
    }
    return true;

}*/
  • 写回答

1条回答 默认 最新

  • Jhui_0508 2016-02-02 07:24
    关注

    不要只看不说啊 很急啊!!!!!!!!!!!!!!!1

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘