java POI创建Excel示例(xslx和xsl区别 )

时间:2023-03-08 18:42:07
Java用来处理office类库有很多,其中POI就是比较出名的一个,它是apache的类库,现在版本到了3.10,也就是2014年2月8号这个版本。
在处理PPT,Excel和Word前,需要导入以前几个jar包:
dom4j-1.7-20060614.jar
log4j-1.2.13.jar
poi-3.7-20101029.jar
poi-ooxml-3.7-20101029.jar
poi-ooxml-schemas-3.7-20101029.jar

本文主要简介下java poi处理Excel,

2003版本的.xls一张sheet表允许存2^16 = 次方行数据,2^8 = 256列数据,
2007版本以上的.xlsx一张sheet表允许存的数据就更大了,是百万级别的。行: 2^20 = 1048576; 列:2^14 = 16384 行。
以下代码简单操作了excel文件。 
 package com.caoyuanzhanlang.excelhandle;

 import java.io.File;
import java.io.FileOutputStream; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
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.XSSFWorkbook; public class NewExcel { private String excelPath = "data.xlsx"; public static void main(String[] args) throws Exception{
NewExcel excel = new NewExcel();
if(excel.createExcelFile()) {
System.out.println("data.xlsx is created successfully.");
}
}
public boolean createExcelFile() {
boolean isCreateSuccess = false;
Workbook workbook = null;
try {
// XSSFWork used for .xslx (>= 2007), HSSWorkbook for 03 .xsl
workbook = new XSSFWorkbook();//HSSFWorkbook();//WorkbookFactory.create(inputStream);
}catch(Exception e) {
System.out.println("It cause Error on CREATING excel workbook: ");
e.printStackTrace();
}
if(workbook != null) {
Sheet sheet = workbook.createSheet("testdata");
Row row0 = sheet.createRow(0);
for(int i = 0; i < 11; i++) {
Cell cell_1 = row0.createCell(i, Cell.CELL_TYPE_STRING);
CellStyle style = getStyle(workbook);
cell_1.setCellStyle(style);
cell_1.setCellValue("HELLO" + i + "Column");
sheet.autoSizeColumn(i);
}
for (int rowNum = 1; rowNum < 200; rowNum++) {
Row row = sheet.createRow(rowNum);
for(int i = 0; i < 11; i++) {
Cell cell = row.createCell(i, Cell.CELL_TYPE_STRING);
cell.setCellValue("cell" + String.valueOf(rowNum+1) + String.valueOf(i+1));
}
}
try {
FileOutputStream outputStream = new FileOutputStream(excelPath);
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
isCreateSuccess = true;
} catch (Exception e) {
System.out.println("It cause Error on WRITTING excel workbook: ");
e.printStackTrace();
}
}
File sss = new File(excelPath);
System.out.println(sss.getAbsolutePath());
return isCreateSuccess;
}
private CellStyle getStyle(Workbook workbook){
CellStyle style = workbook.createCellStyle();
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
// 设置单元格字体
Font headerFont = workbook.createFont(); // 字体
headerFont.setFontHeightInPoints((short)14);
headerFont.setColor(HSSFColor.RED.index);
headerFont.setFontName("宋体");
style.setFont(headerFont);
style.setWrapText(true); // 设置单元格边框及颜色
style.setBorderBottom((short)1);
style.setBorderLeft((short)1);
style.setBorderRight((short)1);
style.setBorderTop((short)1);
style.setWrapText(true);
return style;
}
public String getExcelPath() {
return this.excelPath;
} public void setExcelPath(String excelPath) {
this.excelPath = excelPath;
}
}