您的位置:首页 > 编程语言 > Java开发

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

2016-12-14 13:52 567 查看


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);//将字体加入到
4000
样式中。

 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();

}
结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: