Java代码用POI读写Excel并修改excel格式

8 篇文章 0 订阅
4 篇文章 0 订阅


JAVA中操作Excel的有两种比较主流的工具包: JXL POI jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel。而poi可以操作Excel95及以后的版本,即可操作后缀为.xls.xlsx两种格式的excel。本文用POI工具对excel进行读写操作。

首先到官网下载POI的jar包以及源码包。网址为http://poi.apache.org/。到网上下载binary包和源码包source。我用的是poi-bin-3.16-beta1版。

解压下载的poi-bin-3.16-beta1-20161120.zip。将poi-3.16-beta1.jar,poi-ooxml-3.16-beta1.jar,poi-ooxml-schemas-3.16-beta1.jar以及ooxml-lib下的xmlbeans-2.6.0.jar,还有lib下的commons-collections4-4.1.jar,log4j-1.2.17.jar这些jar文件引入到项目中。

在写代码前我们要知道excel基础元素由工作簿(workbook),工作表(sheet属于工作簿),行(row,属于工作表),单元格(cell属于行;由行和列确定)组成。

下面的代码创建了一个excel文档,并向其中的第一行第一列写入hello world。 HSSFWorkbook只能读取后缀为xls的excel文件。若想读写*.xlsx的文件只需将 HSSFWorkbook换成 XSSFWorkbook即可。

@Test
public void testWrite() throws Exception{
 HSSFWorkbook wookbook=new HSSFWorkbook();//1、创建工作簿
 HSSFSheet sheet = wookbook.createSheet("shermin"); //2、创建工作表,名字是shermin
 HSSFRow row = sheet.createRow(0);3、创建行;创建第一行,索引从0开始
 HSSFCell cell = row.createCell(0);//4、创建单元格;创建第1行第1列

 cell.setCellValue("hello world");

//输出到硬盘,把excel输出到具体的地址
 FileOutputStream fileOutputStream=new FileOutputStream("D:\\xcm\\excel\\测试.xls");
 wookbook.write(fileOutputStream);
 wookbook.close();
 fileOutputStream.close();
}



这个程序从excel中读数据显示到控制台。JUint单元测试结果如下图。

@Test
public void testRead() throws Exception{
 FileInputStream finFileInputStream=new FileInputStream("D:\\xcm\\excel\\测试.xls");
 HSSFWorkbook wookbook=new HSSFWorkbook(finFileInputStream);//1、读取工作簿
 HSSFSheet sheet = wookbook.getSheet("shermin"); //2、读取第"shermin"工作表
 HSSFRow row = sheet.getRow(0);
 HSSFCell cell = row.getCell(0); //读取单元格;读取第0行第0列
 System.out.println(cell.getStringCellValue()); 
 wookbook.close();
 finFileInputStream.close();
}


下面是一个通用的程序,可以读写*.xls和*.xlsx文件。先用正则表达式判断文件是否为excel文件,再判断是xls还是xlsx。用统一的接口Workbook接受XSSFWorkbook或HSSFWorkbook。然后在处理。

@Test
public void testToRead() throws Exception{
 String filePath="D:\\xcm\\excel\\测试.xls";
 if(filePath.matches("^.+\\.(?i)(xls|xlsx)$")){//判断是否excel文档
  FileInputStream fileInputStream=new FileInputStream(filePath);
  boolean is03Excel=filePath.matches("^.+\\.(?i)(xlsx)$") ? true :false;
  Workbook workbook=is03Excel ? new XSSFWorkbook(fileInputStream) :new HSSFWorkbook(fileInputStream);
  Sheet sheet = workbook.getSheetAt(0);
  Row row = sheet.getRow(0);
  Cell cell = row.getCell(0);
  System.out.println(cell.getStringCellValue());
  workbook.close();
  fileInputStream.close();
 }
}

下面通过java代码修改excel数据的格式。

@Test
public void testStyle() throws Exception{
 HSSFWorkbook workbook=new HSSFWorkbook();
 //创建合并单元格对象,合并0行0列到0行
 CellRangeAddress cellRangeAddress=new CellRangeAddress(0,0,0,10);
 //创建字体
  HSSFFont font = workbook.createFont();
  font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗字体

font.setFontHeightInPoints((short) 16);//字体大小
 //创建单元格样式
 HSSFCellStyle style = workbook.createCellStyle();
 style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//c垂直居中
 style.setFont(font);//将字体加入到样式中。
 HSSFSheet sheet = workbook.createSheet("shermin");
 //加载合并单元格对象
 sheet.addMergedRegion(cellRangeAddress);
 HSSFRow row = sheet.createRow(0);
 HSSFCell cell = row.createCell(0);
 cell.setCellValue("java写的excel文件");
 cell.setCellStyle(style);//样式加到单元格中
 FileOutputStream fileOutputStream=new FileOutputStream("D:\\xcm\\excel\\测试.xls");
 workbook.write(fileOutputStream);
 workbook.close();
 fileOutputStream.close();
}

结果


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值