POI 处理Excel的简单介绍

来源:互联网 发布:矩阵论 pdf 编辑:程序博客网 时间:2024/06/03 21:51

一.POI简介

Jakarta POI 是apache的子项目,它提供了一组操纵Windows文档的Java API
目前比较成熟的是HSSF接口,处理Excel对象,可以控制一些属性如sheet,cell等等。

二.HSSF介绍

HSSF 是Horrible SpreadSheet Format的缩写(讨厌的电子表格格式)。也许HSSF的名字有点滑稽,就本质而言它是一个非常严肃、正规的API。通过HSSF,你可以用纯Java代码来读取、写入、修改Excel文件。
HSSF 为读取操作提供了两类API:usermodel和eventusermodel,即“用户模型”和“事件-用户模型”。前者很好理解,后者比较抽象,但操作效率要高得多。

三、POI结构

HSSFWorkbook Excel文档对象介绍
HSSFSheet Excel的表单
HSSFRow Excel的行
HSSFCell Excel的格子单元
HSSFFont Excel字体
HSSFName 名称
HSSFDataFormat 日期格式
在poi1.7中才有以下2项:
HSSFHeader sheet头
HSSFFooter sheet尾
和这个样式
HSSFCellStyle cell样式
辅助操作包括
HSSFDateUtil 日期
HSSFPrintSetup 打印
HSSFErrorConstants 错误信息表

四、一个创建Excel的例子

package com.gordon.test;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;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.HSSFPalette;import org.apache.poi.hssf.usermodel.HSSFRichTextString;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.HSSFColor;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.util.CellRangeAddress;public class Test {public static void main(String[] args) {ExportExcel excel = new ExportExcel();String[] sheetTitles = new String[]{"test1","test2"};OutputStream out = null;try {out = new FileOutputStream("test.xls");excel.exportExcel(sheetTitles, out);} catch (FileNotFoundException e) {e.printStackTrace();} finally {try {out.close();} catch (IOException e) {e.printStackTrace();}}}public static class ExportExcel {public void exportExcel(String[] titles, OutputStream outputStream) {HSSFWorkbook workbook = new HSSFWorkbook();//创建一个excel文件//创建样式HSSFCellStyle headerStyle = getHeaderStyle(workbook);        HSSFCellStyle baseStyle = getBaseStyle(workbook);for (String title : titles) {HSSFSheet sheet = workbook.createSheet(title);  //创建一个sheet//每一个sheet创建10行数据HSSFRow row = sheet.createRow(0);//创建一行数据for (int j = 0; j < 20; j++) {HSSFCell cell = row.createCell(j);//创建表头cell.setCellType(HSSFCell.CELL_TYPE_STRING);//设置单元格类型            cell.setCellValue(new HSSFRichTextString("表头" + j));            cell.setCellStyle(headerStyle);//设置样式}for (int i = 1; i <= 10; i++) {HSSFRow dataRow = sheet.createRow(i);//创建一行数据//每一行创建20个单元格for (int j = 0; j < 20; j++) {HSSFCell cell = dataRow.createCell(j);            cell.setCellType(HSSFCell.CELL_TYPE_STRING);//设置单元格类型            if(j == 0 && i % 2 != 0) { //第一列只奇数行set数据,为了之后的单元格合并            cell.setCellValue(new HSSFRichTextString("数据" + j));            } else if (j != 0) {            cell.setCellValue(new HSSFRichTextString("数据" + j));}                        cell.setCellStyle(baseStyle);//设置样式}}int startRowNum = 1;        int endRowNum = 2;        int startAndEndColumnNum = 0;//合并单元格        //单元格合并时,addMergedRegion只是记录了需要合并的单元格左边,        //连续添加时的坐标都是针对初始表格的,并不会add一个,坐标就动态变了,而是最后生成表格时一起mergefor(int k = 0;k < 10;k++) {            CellRangeAddress roleCellRangeAddress =             new CellRangeAddress(            startRowNum,//开始合并的行号(都是从0开始计算的)            endRowNum, //合并到的行号            startAndEndColumnNum, //开始合并的列号            startAndEndColumnNum);//合并到的列号                sheet.addMergedRegion(roleCellRangeAddress);//向sheet中添加合并区域                startRowNum += 2;                endRowNum += 2;            }for (int i = 0; i < 20; i++) {            sheet.autoSizeColumn(i);        }}//导出报表try {workbook.write(outputStream);} catch (IOException e) {e.printStackTrace();}}}public static HSSFCellStyle getHeaderStyle(HSSFWorkbook workbook) {HSSFCellStyle headerCellStyle = workbook.createCellStyle();//创建一个样式//对于不在POI中内置的颜色,可以自定义一个颜色        HSSFPalette customPalette = workbook.getCustomPalette();//获得自定义调色板        //更新内置颜色的index值为自定义颜色,注意,更新后在程序中所有用到这个颜色的地方都会被更新为自定义的颜色        //如果在这段代码之前使用了这个颜色,也会被更新为自定义颜色        customPalette.setColorAtIndex(HSSFColor.BLUE.index, (byte) 79, (byte) 129, (byte) 189);        //把自定义颜色填充到单元格背景色中        headerCellStyle.setFillForegroundColor(new HSSFColor.BLUE().getIndex());        headerCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);                //给单元格加边框        headerCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);        headerCellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);        headerCellStyle.setBottomBorderColor(HSSFColor.WHITE.index);        headerCellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);        headerCellStyle.setLeftBorderColor(HSSFColor.WHITE.index);        headerCellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);        headerCellStyle.setRightBorderColor(HSSFColor.WHITE.index);        headerCellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);        headerCellStyle.setTopBorderColor(HSSFColor.WHITE.index);                //垂直、水平都居中        headerCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);        headerCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);                //设置字体        HSSFFont whiteFont = workbook.createFont();        whiteFont.setColor(new HSSFColor.WHITE().getIndex());        whiteFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);        headerCellStyle.setFont(whiteFont);                return headerCellStyle;}public static HSSFCellStyle getBaseStyle(HSSFWorkbook workbook) {        HSSFCellStyle baseCellStyle = workbook.createCellStyle();        HSSFPalette customPalette = workbook.getCustomPalette();        customPalette.setColorAtIndex(HSSFColor.BLUE_GREY.index, (byte) 233, (byte) 237, (byte) 244);        baseCellStyle.setFillForegroundColor(new HSSFColor.BLUE_GREY().getIndex());        baseCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);                baseCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);        baseCellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);        baseCellStyle.setBottomBorderColor(HSSFColor.WHITE.index);        baseCellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);        baseCellStyle.setLeftBorderColor(HSSFColor.WHITE.index);        baseCellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);        baseCellStyle.setRightBorderColor(HSSFColor.WHITE.index);        baseCellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);        baseCellStyle.setTopBorderColor(HSSFColor.WHITE.index);                baseCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);        baseCellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);                return baseCellStyle;    }}

产生后的Excel效果如下:





0 0
原创粉丝点击