一、介绍:
Apache POI是Apache软件基金会的开放式源码函式库,POI提供API给JAVA程序对Microsoft Office格式档案读和写的功能
结构:
HSSF 提供读写EXCEL格式档案的功能
XSSF 提供读写EXCEL OOXML格式档案的功能
HWPF 提供读写Word格式档案的功能
HSLF 提供读写PPT格式档案的功能
HDGF 提供Visio格式档案的功能
二、开始
1、创建一个工作簿
/**
* 新建一个工作簿
* @author
*
*/
public static void main(String[] args) throws IOException {
//创建工作簿
Workbook wb = new HSSFWorkbook();
FileOutputStream fileout = new FileOutputStream("d:\\用poi创建的工作簿.xls");
wb.write(fileout);
fileout.close();
}
2、创建sheet页
/**
* 新建sheet页
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Workbook wb = new HSSFWorkbook(); //创建工作簿
wb.createSheet("第一个sheet页");
wb.createSheet("第二个sheet页");
FileOutputStream fileout = new FileOutputStream("d:\\用poi创建的工作簿Sheet页.xls");
wb.write(fileout);
fileout.close();
}
3、创建单元格并填值
/**
* 创建单元格并填值
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("第一个sheet");
Row row = sheet.createRow(0); //创建一行
Cell cell = row.createCell(0); //创建一个单元格 第一列
cell.setCellValue(111);
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue("字符串这个");
row.createCell(3).setCellValue(false);
row.createCell(4).setCellValue(1.2);
FileOutputStream fos = new FileOutputStream("d:\\poixls.xls");
wb.write(fos);
fos.close();
}
4、创建时间格式的单元格 和其他类型的值的单元格
/**
* 创建时间格式的单元格
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("第一个sheet");
Row row = sheet.createRow(0); //创建一行
Cell cell = row.createCell(0); //创建一个单元格 第一列
cell.setCellValue(new Date());
// ## 获取HSSF和XSSF的辅助类 ##//
CreationHelper createHelper = wb.getCreationHelper();
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-mm-dd hh:mm:ss"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
cell = row.createCell(2);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);
//其他格式的单元格
row.createCell(2).setCellValue(1);
row.createCell(3).setCellValue("这个一个字符串");
row.createCell(4).setCellValue(true);
row.createCell(5).setCellValue(HSSFCell.CELL_TYPE_NUMERIC); //静态常量 0
row.createCell(6).setCellValue(false);
FileOutputStream fos = new FileOutputStream("d:\\poixls.xls");
wb.write(fos);
fos.close();
}
5、遍历工作簿
/**
* 遍历工作簿
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream("d:\\poixls.xls");
//引入文件系统,可以接收一个输入流
POIFSFileSystem fs = new POIFSFileSystem(is);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet hssfSheet = wb.getSheetAt(0);
if(hssfSheet == null) {
return;
}
//遍历行row
for(int row = 0; row <= hssfSheet.getLastRowNum(); row++) {
HSSFRow hssfRow = hssfSheet.getRow(row);
if(hssfRow == null) {
continue;
}
//遍历列
for(int cell = 0; cell < hssfRow.getLastCellNum(); cell++) {
HSSFCell hssfCell = hssfRow.getCell(cell);
if(hssfCell == null) {
continue;
}
System.out.print(" " + getValue(hssfCell));
}
System.out.println();
}
}
private static String getValue(HSSFCell hssfCell) {
if(hssfCell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
}else if(hssfCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
return String.valueOf(hssfCell.getNumericCellValue());
}else {
return String.valueOf(hssfCell.getStringCellValue());
}
}
6、文本提取
/**
* 文本提取
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
InputStream is = new FileInputStream("d:\\poixls.xls");
//引入文件系统,可以接收一个输入流
POIFSFileSystem fs = new POIFSFileSystem(is);
HSSFWorkbook wb = new HSSFWorkbook(fs);
ExcelExtractor excelExtractor = new ExcelExtractor(wb);
excelExtractor.setIncludeSheetNames(false); //我们不需要sheet页的名字
System.out.println(excelExtractor.getText()); //可以看到结果,sheet页名字都打印出来了
}
7、单元格对其方式
/**
* 单元格对其方式
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("第一个sheet");
Row row = sheet.createRow(2); //创建一行
row.setHeightInPoints(30); //设置高度
createCell(wb,row,(short)0,HSSFCellStyle.ALIGN_CENTER,HSSFCellStyle.VERTICAL_CENTER);
createCell(wb,row,(short)1,HSSFCellStyle.ALIGN_FILL,HSSFCellStyle.VERTICAL_BOTTOM);
createCell(wb,row,(short)2,HSSFCellStyle.ALIGN_LEFT,HSSFCellStyle.VERTICAL_TOP);
createCell(wb,row,(short)3,HSSFCellStyle.ALIGN_RIGHT,HSSFCellStyle.VERTICAL_CENTER);
FileOutputStream fos = new FileOutputStream("d:\\poixls3.xls");
wb.write(fos);
fos.close();
}
/**
* 创建一个单元格并为其设置指定的对其方式
* @param wb 工作簿
* @param row 行
* @param column 列
* @param halign 水平方向对齐方式
* @param valign 垂直方向对其方式
*/
private static void createCell(Workbook wb,Row row,short column,short halign, short valign) {
Cell cell = row.createCell(column); //创建单元格
cell.setCellValue(new HSSFRichTextString("Align It")); //设置值 富文本
CellStyle cellStyle = wb.createCellStyle(); //创建单元格样式
cellStyle.setAlignment(halign); //设置单元格水平方向对其方式
cellStyle.setVerticalAlignment(valign); //设置单元格垂直方向的对其方式
cell.setCellStyle(cellStyle); //设置单元格样式
}
8、设置边框及边框颜色
/**
* 边框及其颜色
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("第一个sheet");
Row row = sheet.createRow(1); //创建一行
Cell cell = row.createCell(1); //创建一个单元格 第一列
cell.setCellValue(4);
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setBorderBottom(CellStyle.BORDER_THIN); //底部边框
cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //边框颜色
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setLeftBorderColor(IndexedColors.GREEN.getIndex());
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex());
cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); //粗的虚线
cellStyle.setTopBorderColor(IndexedColors.RED.getIndex());
cell.setCellStyle(cellStyle);
FileOutputStream fos = new FileOutputStream("d:\\poixls.xls");
wb.write(fos);
fos.close();
}
9、填充颜色
/**
* 填充颜色
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("第一个sheet");
Row row = sheet.createRow(1); //创建一行
Cell cell = row.createCell(1); //创建一个单元格 第一列
cell.setCellValue("xxxx");
CellStyle cellStyle = wb.createCellStyle(); //单元格样式
cellStyle.setFillBackgroundColor(IndexedColors.GREEN.getIndex()); //背景色
cellStyle.setFillPattern(CellStyle.BIG_SPOTS);
cell.setCellStyle(cellStyle);
Cell cell2 = row.createCell(1); //创建一个单元格 第一列
cell.setCellValue("xxxx");
CellStyle cellStyle2 = wb.createCellStyle(); //单元格样式
cellStyle2.setFillForegroundColor(IndexedColors.GREEN.getIndex()); //浅背景
cellStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell2.setCellStyle(cellStyle2);
FileOutputStream fos = new FileOutputStream("d:\\poixls.xls");
wb.write(fos);
fos.close();
}
10、单元格合并
/**
* 合并单元格
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("第一个sheet");
Row row = sheet.createRow(1); //创建一行
Cell cell = row.createCell(1);
cell.setCellValue("单元格合并测试");
sheet.addMergedRegion(new CellRangeAddress(1, 12, 1, 2)); //起始结束行 起始结束列
sheet.addMergedRegion(new CellRangeAddress(13, 15, 5, 12));
FileOutputStream fos = new FileOutputStream("d:\\poixls16.xls");
wb.write(fos);
fos.close();
}
11、字体处理
/**
* 字体处理
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("第一个sheet");
Row row = sheet.createRow(1); //创建一行
Font font = wb.createFont(); //创建一个字体处理类
font.setFontHeightInPoints((short)24); //高度
font.setFontName("Courier New"); //字体
font.setItalic(true); // 斜体
font.setStrikeout(true); //横穿线
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
Cell cell = row.createCell((short)1);
cell.setCellValue("This is a test of fonts!");
cell.setCellStyle(cellStyle);
FileOutputStream fos = new FileOutputStream("d:\\poixls23.xls");
wb.write(fos);
fos.close();
}
12、读取和重写工作簿
/**
* 读取和重写工作簿
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
InputStream inp = new FileInputStream("D:\\poixls23.xls");
POIFSFileSystem fs = new POIFSFileSystem(inp);
Workbook wb = new HSSFWorkbook(fs);
Sheet sheet = wb.getSheetAt(0); //获取第一个sheet页
Row row = sheet.getRow(0); //获取第一行
Cell cell = row.getCell(0); //获取单元格
if(cell == null) {
cell = row.createCell(3);
}
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellValue("测试单元格");
FileOutputStream fos = new FileOutputStream("d:\\poixls23.xls");
wb.write(fos);
fos.close();
}
13、单元格中使用换行
/**
* 单元格中使用换行
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("第一个sheet");
Row row = sheet.createRow(1); //创建一行
Cell cell = row.createCell(1); //创建一个单元格 第一列
cell.setCellValue("这是一个\n换行测试");
CellStyle cellStyle = wb.createCellStyle();
//设置换行
cellStyle.setWrapText(true);
cell.setCellStyle(cellStyle);
//调整上下行高度
row.setHeightInPoints(2*sheet.getDefaultRowHeightInPoints()); //两倍默认行高
//调整单元格宽度
sheet.autoSizeColumn(2);
FileOutputStream fos = new FileOutputStream("d:\\poixls.xls");
wb.write(fos);
fos.close();
}
14、创建用户自定义的数据格式
/**
* 用户自定义的数据格式
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
try {
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("第一个sheet");
CellStyle style;
DataFormat format = wb.createDataFormat();
Row row;
Cell cell;
int rowNum = 0;
short colNum = 0;
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue("代理人销售情况分航空公司统计");
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("0.0")); //设置数据格式
cell.setCellStyle(style);
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.23);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#,##0.000"));
cell.setCellStyle(style);
FileOutputStream fos = new FileOutputStream("d:\\poixls1.xls");
wb.write(fos);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}