poi 打印格式代码设置

偷个懒,直接上代码吧,大家每一行我都写注释了的,更具需求可以自己设计自己的表格 

@Action(value="outProductAction_print")
    public String print() throws Exception{
        //HSSF  2003版 ,   XSSF 2007 版,07可以加载03,但03加载不来07      SXSSF      
        HSSFWorkbook workbook = new HSSFWorkbook(); //创建笔记本
        HSSFSheet sheet = workbook.createSheet();//创建表空间
        sheet.setColumnWidth(1,25*256);//设置宽度 第一行   宽度25
        sheet.setColumnWidth(2,10*256);
        sheet.setColumnWidth(3,25*256);
        sheet.setColumnWidth(4,10*256);
        sheet.setColumnWidth(5,25*256);
        sheet.setColumnWidth(6,10*256);
        sheet.setColumnWidth(7,25*256);
        sheet.setColumnWidth(8,10*256);
        
        HSSFCell createCell=null;  //提升单元格的作用域
        
        //大标题
        int rownum=0;               //设置行数为0,创建一次加一次,循环增加,就可以循环创建行数了
        HSSFRow row = sheet.createRow(rownum++);//创建一行
        row.setHeightInPoints(36);  //设置高度
        createCell = row.createCell(1);  //创建一个单元格,重第2个算
        sheet.addMergedRegion(new CellRangeAddress(0,0,1,8));//合并单元格
        String replace = inputDate.replace("-0", "-").replace("-", "年")+"月出货单";//把页面传过来的时间装换成XXXX年X月格式
        createCell.setCellValue(replace); //赋值到大标题内容中
        
        createCell.setCellStyle(bigTitle(workbook));//调用下面大标题的样式
        //小标题
        String[] smartStrs = {"客户","订单号","货号","数量","工厂","工厂交期","船期","贸易条款"};
        
        HSSFRow createRow = sheet.createRow(rownum++); //创建一行
        createRow.setHeightInPoints(26); //设立高度
        for (int i = 0; i < smartStrs.length; i++) {//循环数据
            createCell = createRow.createCell(i+1);//创建单元格,循环一次,创建一个单元格
            createCell.setCellValue(smartStrs[i]);//赋值
            createCell.setCellStyle(title(workbook));//调用标题样式
        
        }
        //内容
        
        
        //通过选择的时间 查询数据库  由于默认语法中没有我们要的语法,这里我自定义了一个更具时间查询的语法 ContractProduct:货物表,其对应了合同表Contract 详情表结构看一看做到物流云项目
        List<ContractProduct> findDateAll = contractProductServiceimpl.findDateAll(inputDate);
         System.out.println(findDateAll.size());
        for (ContractProduct contractProduct : findDateAll) {
            
            row = sheet.createRow(rownum++); //创建一行
            createRow.setHeightInPoints(26);//设置高度26
            int i1=1;
            createCell = row.createCell(i1++);//客户名   创建一个单元格
            createCell.setCellValue(contractProduct.getContract().getCustomName());//赋值
            createCell.setCellStyle(text(workbook));//引入样式
            
            
            createCell = row.createCell(i1++);//订单号
            createCell.setCellValue(contractProduct.getContract().getContractNo());
            createCell.setCellStyle(text(workbook));
            
            createCell = row.createCell(i1++);//货号
            createCell.setCellValue(contractProduct.getProductNo());
            createCell.setCellStyle(text(workbook));
            
            createCell = row.createCell(i1++);//数量
            createCell.setCellValue(contractProduct.getCnumber());
            createCell.setCellStyle(text(workbook));
            
            createCell = row.createCell(i1++);//工厂
            createCell.setCellValue(contractProduct.getFactoryName());
            createCell.setCellStyle(text(workbook));
            
            createCell = row.createCell(i1++);//工厂交期
            //根据工具类UtilFuns的dateTimeFormat方法,把日期格式转换成      String
            createCell.setCellValue(UtilFuns.dateTimeFormat(contractProduct.getContract().getDeliveryPeriod()));
            createCell.setCellStyle(text(workbook));
            
            createCell = row.createCell(i1++);//船期
            createCell.setCellValue(UtilFuns.dateTimeFormat(contractProduct.getContract().getShipTime()));
            createCell.setCellStyle(text(workbook));
            
            createCell = row.createCell(i1++);//贸易条款
            createCell.setCellValue(contractProduct.getContract().getTradeTerms());
            createCell.setCellStyle(text(workbook));
            
        } 
                    //输出
        ByteArrayOutputStream os = new ByteArrayOutputStream();//new 一个io流
        HttpServletResponse response = ServletActionContext.getResponse(); //创建一个Response
        
        DownloadUtil util = new DownloadUtil();//调用下载工具类 DownloadUtil
        workbook.write(os);//把工作薄里的内容写入io流中
        util.download(os, response, replace + ".xls");//调用DownloadUtil工具类的download方法,设立下载文件名
        os.close();//关闭流
        return NONE;
    }

==============================下面的都是样式,不必死记================================
    //大标题的样式
    public CellStyle bigTitle(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)16);
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);                    //字体加粗
        
        style.setFont(font);
        
        style.setAlignment(CellStyle.ALIGN_CENTER);                    //横向居中
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);      //纵向居中
        
        return style;
    }
    //小标题的样式
    public CellStyle title(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short)12);
        
        style.setFont(font);
        
        style.setAlignment(CellStyle.ALIGN_CENTER);                    //横向居中
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);      //纵向居中
        
        style.setBorderTop(CellStyle.BORDER_THIN);                    //上细线
        style.setBorderBottom(CellStyle.BORDER_THIN);                //下细线
        style.setBorderLeft(CellStyle.BORDER_THIN);                    //左细线
        style.setBorderRight(CellStyle.BORDER_THIN);                //右细线
        
        return style;
    }
    
    //文字样式
    public CellStyle text(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)10);
        
        style.setFont(font);
        
        style.setAlignment(CellStyle.ALIGN_LEFT);                    //横向居左
        style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);      //纵向居中
        
        style.setBorderTop(CellStyle.BORDER_THIN);                    //上细线
        style.setBorderBottom(CellStyle.BORDER_THIN);                //下细线
        style.setBorderLeft(CellStyle.BORDER_THIN);                    //左细线
        style.setBorderRight(CellStyle.BORDER_THIN);                //右细线
        
        return style;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿邱先森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值