POI打印(excle)+读取模板

开发步骤:共8步

1、创建一个工作簿workbook

2、创建一个工作表sheet

3、创建一个行对象row  (下标起始值为0)

4、创建一个单元格对象cell   (下标起始值为0)

5、给单元格设置内容

6、设置单元格的样式,设置字体和字体的大小

7、保存,关闭流对象

8、下载

public static void main(String[] args) throws Exception {

         //1 创建工作薄:Workbook是一个接口,它有一个实现类HSSFWorkbook对象,这个对象专门操作excel97-03excel的后缀名是xls

         Workbook wb = new HSSFWorkbook();

         //2 创建工作表sheet:工作表

         Sheet sheet = wb.createSheet();

         //3 创建行对象,java中从0开始计数

         Row row = sheet.createRow(3);

         //4 创建列对象

         Cell cell = row.createCell(3);

         //5 设置内容

         cell.setCellValue("啦啦啦啦");

         //6 设置内容格式

         Font font = wb.createFont();

         font.setFontHeightInPoints((short)24);//以像素点的方式设置字体大小

         font.setFontName("华文彩云");//设置字体  

         //System.out.println(Short.MIN_VALUE+"-"+Short.MAX_VALUE);

         //创建格式

         CellStyle cellStyle = wb.createCellStyle();

         cellStyle.setFont(font);

         //cellStylecell

         cell.setCellStyle(cellStyle);

         //7 保存(javase项目采用保存)

         FileOutputStream stream = new FileOutputStream(new File("d://a.xls"));

         wb.write(stream);//将对象写进流

         stream.flush();

         stream.close();

         //8 下载

         System.out.println("运行结束");

    }

-----------------------------------------------------样式

    //大标题的样式
    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;
    }

 

------------------------------------模板打印

public String print() throws Exception {

        //读取模板,路径

        String path = ServletActionContext.getRequest().getRealPath("/");

        System.out.println("path:"+path);

        path += "d://tOUTPRODUCT.xls";//获取模板在服务器的路径

        FileInputStream is = new FileInputStream(new File(path));

        //1 借助模板创建工作簿

        Workbook wb = new HSSFWorkbook(is);

        //2 获取工作表

        Sheet sheet = wb.getSheetAt(0);

        // 义公共变量

        Row nRow = null;//行对象

        Cell nCell = null;//单元格对象

        int rowNo = 0;//第几行

        int cellNo = 1;//列对象

        /*******************设置大标题********************/

        //3 获取行对象

        nRow = sheet.getRow(rowNo);

        //4 获取单元格

        nCell = nRow.getCell(cellNo);  

        // 5 设置数据

        nCell.setCellValue(inputDate.replace("-0", "-").replace("-", "")+"月份出货表");  

        // 6 设置样式,获取原来的样式赋值,这步可以省略

//      nCell.setCellStyle(nCell.getCellStyle());

        /*******************设置小标题********************/

        rowNo++;//跳过小标题行

        /*******************设置出货数据********************/

        rowNo++;//进入数据第一行行

        //获取样式

        nRow = sheet.getRow(rowNo);

        //客户 

        CellStyle customerCellStyle = nRow.getCell(cellNo++).getCellStyle();

        //订单号

        CellStyle contractNoCellStyle = nRow.getCell(cellNo++).getCellStyle();

        //货号 

        CellStyle productNoCellStyle = nRow.getCell(cellNo++).getCellStyle();

        // 准备数据

        String hql = "from ContractProduct where to_char(contract.shipTime,'yyyy-mm')='"+inputDate+"'";

        //货物的集合

        List<ContractProduct> list = contractProductService.find(hql, ContractProduct.class, null);

        for(ContractProduct cp:list){

 

            //单元格no1

            cellNo = 1;

           

            //一条数据创建一行

            nRow = sheet.createRow(rowNo);

            //在创建每列的数据

            //客户 

            nCell = nRow.createCell(cellNo++);

            nCell.setCellValue(cp.getContract().getCustomName());

            nCell.setCellStyle(customerCellStyle);

           

            //订单号

            nCell = nRow.createCell(cellNo++);

            nCell.setCellValue(cp.getContract().getContractNo());

            nCell.setCellStyle(contractNoCellStyle);

            //货号 

            nCell = nRow.createCell(cellNo++);

            nCell.setCellValue(cp.getProductNo());

            nCell.setCellStyle(productNoCellStyle);

//          开始操作数据,rowNo++

            rowNo++;

        }

        // 7 写入流

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();//字节数组缓冲流

        wb.write(byteArrayOutputStream);

        // 8 

        DownloadUtil downloadUtil = new DownloadUtil();

        HttpServletResponse response = ServletActionContext.getResponse();

        /**

         * 第一个参数:流

         * 第二个参数:response

         * 第三个参数:下载的文件名

         */

        downloadUtil.download(byteArrayOutputStream, response, "出货表.xls");

        return NONE;

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值