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

POI操作EXCEL03和EXCEL07以上版本

2016-05-06 09:12 549 查看
JAVA中操作Excel的有两种比较主流的工具包: JXL 和 POI 。jxl 只能操作Excel 95, 97, 2000也即以.xls为后缀的excel,个人认为已经是被淘汰的框架,而poi可以操作Excel 95及以后的版本,即可操作后缀为 .xls 和 .xlsx两种格式的excel。

POI全称 Poor Obfuscation Implementation,直译为“可怜的模糊实现”,当然并不可怜而且很实用,利用POI接口可以通过JAVA操作Microsoft office 套件工具的读写功能。官网:http://poi.apache.org ,POI支持office的所有版本,并且在接下来的演示中需要从前端页面导入用户上传的版本不确定的excel文件,所以选择POI来讲解。在官网,下载POI :对于只操作2003 及以前版本的excel,只需要 ,如果需要同时对2007及以后版本进行操作则需要复制

引入以下jar包:

poi-3.10.1-20140818.jar(若需求为03版本的,以下两个jar包不需要引入)

poi-ooxml-3.10.1-20140818.jar,

poi-ooxml-schemas-3.10.1-20140818.jar,以及复制在ooxml-lib目录下的xmlbeans-2.6.0.jar,dom4j-1.6.1.jar。

在POI包中有如下几个主要对象和excel的几个对象对应:



 (07以上版本与之不同是使用的XSSFWorkbook,XSSFSheet,XSSFRow,XSSFCell)

 附代码:

 

package cn.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

public class TestPOI2Excel {
/**
* 写入03版本excel
* @throws Exception
*/
@Test
public void testWrite03Excel() throws Exception {
/**
* 工作簿-工作表-行-单元格
*
*/
//工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
//工作表
HSSFSheet sheet = workbook.createSheet("hello world");
//行--index从0开始
HSSFRow row = sheet.createRow(2);
//创建单元格--index从0开始
HSSFCell cell = row.createCell(2);
cell.setCellValue("hello world");
//输出到硬盘
FileOutputStream fileOutputStream = new FileOutputStream("D:\\Workspaces\\MyEclipse 2015\\itcastTax\\WebRoot\\upload\\测试.xls");
//吧excel输入到具体的地址
workbook.write(fileOutputStream);

//关闭
workbook.close();
fileOutputStream.close();

}

/**
* 读取03版本excel
* @throws Exception
*/
@Test
public void testRead03Excel() throws Exception {
/**
* 工作簿-工作表-行-单元格
*
*/
FileInputStream fileInputStream = new FileInputStream("D:\\Workspaces\\MyEclipse 2015\\itcastTax\\WebRoot\\upload\\测试.xls");
//读取工作簿
HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
//读取第一个工作表
HSSFSheet sheet = workbook.getSheetAt(0);
//读取第三行--index从0开始
HSSFRow row = sheet.getRow(2);
//读取单元格--index从0开始
HSSFCell cell = row.getCell(2);
System.out.println(cell.getStringCellValue());

//关闭
workbook.close();
fileInputStream.close();

}

/**
* 写入07版本excel
* @throws Exception
*/
@Test
public void testWrite07Excel() throws Exception {
/**
* 工作簿-工作表-行-单元格
*
*/
//工作簿
XSSFWorkbook workbook = new XSSFWorkbook();
//工作表
XSSFSheet sheet = workbook.createSheet("hello world");
//行--index从0开始
XSSFRow row = sheet.createRow(2);
//创建单元格--index从0开始
XSSFCell cell = row.createCell(2);
cell.setCellValue("hello world");
//输出到硬盘
FileOutputStream fileOutputStream = new FileOutputStream("D:\\Workspaces\\MyEclipse 2015\\itcastTax\\WebRoot\\upload\\test.xlsx");
//吧excel输入到具体的地址
workbook.write(fileOutputStream);

//关闭
workbook.close();
fileOutputStream.close();

}

/**
* 读取07版本excel
* @throws Exception
*/
@Test
public void testRead07Excel() throws Exception {
/**
* 工作簿-工作表-行-单元格
*
*/
FileInputStream fileInputStream = new FileInputStream("D:\\Workspaces\\MyEclipse 2015\\itcastTax\\WebRoot\\upload\\test.xlsx");
//读取工作簿
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
//读取第一个工作表
XSSFSheet sheet = workbook.getSheetAt(0);
//读取第三行--index从0开始
XSSFRow row = sheet.getRow(2);
//读取单元格--index从0开始
XSSFCell cell = row.getCell(2);
System.out.println(cell.getStringCellValue());

//关闭
workbook.close();
fileInputStream.close();

}

/**
* 读取07版本excel
* @throws Exception
*/
@Test
public void testRead03And07Excel() throws Exception {
/**
* 工作簿-工作表-行-单元格
*
*/
String fileName = "D:\\Workspaces\\MyEclipse 2015\\itcastTax\\WebRoot\\upload\\test.xls";
if(fileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){//判断是否是excel
boolean is03Excel = fileName.matches("^.+\\.(?i)(xls)$");
FileInputStream fileInputStream = new FileInputStream(fileName);

//读取工作簿
Workbook workbook = is03Excel?new HSSFWorkbook(fileInputStream):new XSSFWorkbook(fileInputStream);
//读取第一个工作表
Sheet sheet = workbook.getSheetAt(0);
//读取第三行--index从0开始
Row row = sheet.getRow(2);
//读取单元格--index从0开始
Cell cell = row.getCell(2);
System.out.println(cell.getStringCellValue());

//关闭
workbook.close();
fileInputStream.close();
}
}

/**
* 创建合并单元格
* @throws Exception
*/
@Test
public void testExcelStyle() throws Exception {
/**
* 工作簿-工作表-行-单元格
*
*/
//工作簿
HSSFWorkbook workbook = new HSSFWorkbook();

//1.1创建合并单元格对象合并第三行第三列到第五列
CellRangeAddress cellRangeAddress = new CellRangeAddress(2,2,2,4);//firstRow, lastRow, firstCol, lastCol
//1.2创建单元格样式
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
//1.3设置字体
HSSFFont font = workbook.createFont();
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short)16);
//加载字体
style.setFont(font);
//单元格背景
//style.setFillPattern(HSSFCellStyle.DIAMONDS);//填充模式
//style.setFillBackgroundColor(HSSFColor.YELLOW.index);//背景色

//2、工作表
HSSFSheet sheet = workbook.createSheet("hello world");

//2.1加载合并单单元格对象
sheet.addMergedRegion(cellRangeAddress);

//3.行--index从0开始
HSSFRow row = sheet.createRow(2);
//4.创建单元格--index从0开始
HSSFCell cell = row.createCell(2);
cell.setCellStyle(style);
cell.setCellValue("hello world");
//输出到硬盘
FileOutputStream fileOutputStream = new FileOutputStream("D:\\Workspaces\\MyEclipse 2015\\itcastTax\\WebRoot\\upload\\test2.xls");
//吧excel输入到具体的地址
workbook.write(fileOutputStream);

//关闭
workbook.close();
fileOutputStream.close();

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