POI介绍:
POI提供API给Java程序对Microsoft Office格式档案进行各种操作,用它可以方便地操作Excel文件。
Apache POI是Apache软件基金会的开放源码函式库,是用Java编写的免费开源的跨平台的 Java API。
POI主要用到的类有HSSFWorkbook、HSSFSheet、HSSHRow、HSSFCell,HSSFWorkbook是Excel文件对象、HSSFSheet是Excel文件内的分页sheet对象、HSSHRow是行对象、HSSFCell是单元格对象,它们都在org.apache.poi.hssf.usermodel这个package里面。
主流操作execl API:
JXL:只能操作excel 2003
POI:可以操作整个office(excel、doc、ppt、visio);包括所有的excel版本。
应用场景
支持Excel 库的所有基本功能; 数据库写入excel,用户可以共享数据,作为备份数据(不包括大字段),还原数据等;呈现和文本提取是它的主要特点。
POI的使用
1.导入POI相关的jar包
Maven项目:
pom.xml文件中xml配置
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.11</version>
</dependency>
2.需求:需要8步
3.实现代码
实现如下图的excel效果
实现代码:
@Test
public void testPoi() throws IOException {
// 1.创建一个工作簿
Workbook wb = new HSSFWorkbook();// 作用于excel
// 2.创建工作表
Sheet sheet = wb.createSheet();
// 3.创建行对象,从0开始
Row row = sheet.createRow(4);
// 4.创建单元格
Cell cell = row.createCell(2);
// 5.设置单元格的内容
cell.setCellValue("Hello World!");
// 6.设置单元格样式,基于工作簿wb创建
CellStyle cellStyle = wb.createCellStyle();
Font font = wb.createFont();
font.setFontName("微软雅黑");
font.setFontHeightInPoints((short) 20);
cellStyle.setFont(font);
cell.setCellStyle(cellStyle);
//7.保存,关闭流
OutputStream os = new FileOutputStream("D:/pio/helloworld.xls");
wb.write(os);
os.close();
}
打开d盘pio文件夹下的helloworld.xls文件,对比内容一致:
模版打印
实现代码:
public String print() throws Exception {
//通用变量:cellNo列号,rowNo行号,
int rowNo = 0, cellNo = 1;
//列
Row nRow = null;
//行
Cell nCell = null;
//1.读取工作簿
//获取模版路径
String path = ServletActionContext.getServletContext().getRealPath("/")+"/make/xlsprint/tOUTPRODUCT.xls";
System.out.println(path);
//把模版放入输入流中,读取
InputStream is = new FileInputStream(path);
Workbook wb = new HSSFWorkbook(is);
//2.读取工作表,根据索引位置 0 读取
Sheet sheet = wb.getSheetAt(0);
cellNo = 1; //重置
//3.创建行对象
//=================大标题================
nRow = sheet.getRow(rowNo++); //读取行对象
nCell = nRow.getCell(cellNo); //读取单元格对象
//设置单元格的内容--格式:(2012年8月份出货表)
nCell.setCellValue(inputDate.replace("-0", "-").replace("-", "年")+"月份出货表");
//=================小标题================
rowNo++;
//=================数据输出================
//首先读取模版第三行,获取模版中第三行相应列的格式
nRow = sheet.getRow(rowNo); //读取第三行
CellStyle customerCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle orderNoCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle productNoCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle cNumberCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle factoryCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle deliveryPeriodCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle shipTimeCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle tradeTermsCellStyle = nRow.getCell(cellNo++).getCellStyle();
String hql = "from ContractProduct where to_char(contract.shipTime,'yyyy-MM') = '"+inputDate+"'";
List<ContractProduct> list = contractProductService.find(hql, ContractProduct.class, null);
//设置出货表中的数据,文本样式采用模版中获取的样式
for(ContractProduct cp : list){
nRow = sheet.createRow(rowNo++); //创建行对象
nRow.setHeightInPoints(24); // 设置行高
cellNo = 1; //重置开始列为1
nCell = nRow.createCell(cellNo++); //创建单元格对象
nCell.setCellValue(cp.getContract().getCustomName()); //客户名称
nCell.setCellStyle(customerCellStyle); //设置文本样式
nCell = nRow.createCell(cellNo++); //创建单元格对象
nCell.setCellValue(cp.getContract().getContractNo()); //订单号--合同号
nCell.setCellStyle(orderNoCellStyle); //设置文本样式
nCell = nRow.createCell(cellNo++); //创建单元格对象
nCell.setCellValue(cp.getProductNo()); //货号
nCell.setCellStyle(productNoCellStyle); //设置文本样式
nCell = nRow.createCell(cellNo++); //创建单元格对象
nCell.setCellValue(cp.getCnumber()); //数量
nCell.setCellStyle(cNumberCellStyle); //设置文本样式
nCell = nRow.createCell(cellNo++); //创建单元格对象
nCell.setCellValue(cp.getFactoryName()); //工厂名
nCell.setCellStyle(factoryCellStyle); //设置文本样式
nCell = nRow.createCell(cellNo++); //创建单元格对象
nCell.setCellValue(UtilFuns.dateTimeFormat(cp.getContract().getDeliveryPeriod())); //工厂交期,同时进行日期格式化
nCell.setCellStyle(deliveryPeriodCellStyle); //设置文本样式
nCell = nRow.createCell(cellNo++); //创建单元格对象
nCell.setCellValue(UtilFuns.dateTimeFormat(cp.getContract().getShipTime())); //船期,同时进行日期格式化
nCell.setCellStyle(shipTimeCellStyle); //设置文本样式
nCell = nRow.createCell(cellNo++); //创建单元格对象
nCell.setCellValue(cp.getContract().getTradeTerms()); //贸易条款
nCell.setCellStyle(tradeTermsCellStyle); //设置文本样式
}
//==========================输出到客户端(下载)====================
DownloadUtil downUtil = new DownloadUtil();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
wb.write(baos); //将excel表格中的内容输出到缓存
baos.close(); //刷新缓存
HttpServletResponse response = ServletActionContext.getResponse();
downUtil.download(baos, response, "出货表.xls"); //如果是中文,下载时可能会产生乱码,如何解决?
//直接使用response下载出货表即可,无需返回字符串
return NONE;
}