poi报表技术快速入门demo(带图片插入)

这里写图片描述

maven依赖
 <!--POI-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.9</version>
    </dependency>

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.9</version>
    </dependency>

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml-schemas</artifactId>
      <version>3.9</version>
    </dependency>

    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-scratchpad</artifactId>
      <version>3.9</version>
    </dependency>



下面是源代码
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;

/**
 * Created by admin on 2016/1/14.
 */
public class POITest {
    public static void main(String[] args) throws IOException {
        /*
        * 创建表格前  先自己建立一个excel的模板,方便以后直接拷贝对应的样式  而不用自己设置单元格的样式
        * */
        Workbook workbook = new XSSFWorkbook(new FileInputStream("G:/demo3.xlsx"));
        Sheet sheet1 = workbook.getSheet("Sheet1");
        CellStyle rowStyle = sheet1.getRow(1).getRowStyle();
        short height = sheet1.getRow(1).getHeight();
        CellStyle cellStyle = sheet1.getRow(1).getCell(0).getCellStyle();

/*set row style*/
        Row row2 = sheet1.createRow(2);
        row2.setHeight(height);

        row2.setRowStyle(rowStyle);

        Cell cell0 = row2.createCell(0);
        cell0.setCellValue("pawn");
        cell0.setCellStyle(cellStyle);

        Cell cell1 = row2.createCell(1);
        cell1.setCellValue(19);
        cell1.setCellStyle(cellStyle);


        cellStyle=sheet1.getRow(1).getCell(2).getCellStyle();


        Cell cell2 = row2.createCell(2);
        cell2.setCellValue("1993年9月11日");
        cell2.setCellStyle(cellStyle);





        Cell cell3 = row2.createCell(3);
/*
* 下面是插入图片到指定的位置
* */
        InputStream inputStream = new FileInputStream("C:\\Users\\admin\\Desktop\\20160114155540.png");
        byte[] bytes = IOUtils.toByteArray(inputStream);
        int i = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
        Drawing drawing = sheet1.createDrawingPatriarch();
        CreationHelper creationHelper = workbook.getCreationHelper();
        ClientAnchor anchor = creationHelper.createClientAnchor();
//        定位图片位置
        anchor.setRow1(2);
        anchor.setCol1(3);
        Picture picture = drawing.createPicture(anchor, i);
        picture.resize();

        OutputStream out = new FileOutputStream("G:/demo3.xlsx");
        workbook.write(out);
        out.close();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值