POI操作excel

POI依赖

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

创建excel

public static void createExcel() throws Exception {
    Workbook workbook = null;// new HSSFWorkbook(); //xls
    workbook = new XSSFWorkbook();// xlsx
    Sheet sheet1 = workbook.createSheet();
    workbook.createSheet("sheet2");
    // 第二行
    Row row = sheet1.createRow(1);
    // 第二行第三个单元格
    Cell cell = row.createCell(2);
    cell.setCellValue("中华人民共和国");

     样式///
    // 1.2创建单元格样式
    CellStyle style = workbook.createCellStyle();
    style.setAlignment(CellStyle.ALIGN_CENTER);// 水平居中
    style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    // 1.3设置字体
    Font font = workbook.createFont();
    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    font.setFontHeightInPoints((short) 16);
    // 加载字体
    style.setFont(font);
    // 单元格背景
    // style.setFillPattern(CellStyle.DIAMONDS);//填充模式
    // style.setFillBackgroundColor(Color.YELLOW.index);//背景色
    cell.setCellStyle(style);


     合并///合并之后坐标不便,内容样式一样不便,坐标 4行3列
    // 第4行5行,从第3到第5单元格合并 , ============写入值
    Cell cellxx= sheet1.createRow(3).createCell(2);
    cellxx.setCellStyle(style);
    cellxx.setCellValue("美利坚合众国");
    // 第4行5行,从第3到第5单元格合并
    CellRangeAddress cellRangeAddress = new CellRangeAddress(3, 4, 2, 4);// <span style="font-family: Arial, Helvetica, sans-serif;">int firstRow, </span><span style="font-family: Arial, Helvetica, sans-serif;"></span><span style="font-family: Arial, Helvetica, sans-serif;">int lastRow, </span><span style="font-family: Arial, Helvetica, sans-serif;"></span><span style="font-family: Arial, Helvetica, sans-serif;">int firstCol, </span><span style="font-family: Arial, Helvetica, sans-serif;"></span><span style="font-family: Arial, Helvetica, sans-serif;">int lastCol</span>

    sheet1.addMergedRegion(cellRangeAddress);


    // 输出到流中
    // ByteArrayOutputStream stream=new ByteArrayOutputStream();
    // workbook.write(stream);

    FileOutputStream fileOutputStream = new FileOutputStream("D:\\XX.xls");
    
    workbook.write(fileOutputStream);
    // 关闭
    workbook.close();
    fileOutputStream.close();
    System.out.println("excel输出成功");
  }

读取excel

public static void readExcel() throws Exception {
    FileInputStream fileInputStream = new FileInputStream("D:\\XX.xls");
    Workbook workbook = null;// new HSSFWorkbook(fileInputStream);//xls
    workbook = new XSSFWorkbook(fileInputStream);// xlsx
    Sheet sheet1 = workbook.getSheetAt(0);

    // 第二行
    Row row = sheet1.getRow(1);
    // 第二行第三个单元格
    Cell cell = row.getCell(2);

    System.out.printf("第二行三列的值:%s", cell.getStringCellValue());
    System.out.println();
    System.out.printf("第四行三列的值:%s", sheet1.getRow(3).getCell(2).getStringCellValue());//合并单元格 4行3列 的值
    // 关闭
    workbook.close();
    fileInputStream.close();
  }









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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值