1.Jakarta POI 是apache的子项目,目标是处理ol2对象。它提供了一组Windows文档操作的Java API。
2.EXCEL 结构
HSSFWorkbook excell 文档对象介绍
HSSFSheet excell的表单
HSSFRow excell的行
HSSFCell excell的格子单元
HSSFFont excell字体
HSSFName 名称
HSSFDataFormat 日期格式
在poi1.7中才有以下2项:
HSSFHeader sheet头
HSSFFooter sheet尾
和这个样式
HSSFCellStyle cell样式
辅助操作包括
HSSFDateUtil 日期
HSSFPrintSetup 打印
HSSFErrorConstants 错误信息表
3.简单的用法
创建Excel
- import java.io.FileOutputStream;
-
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-
- public class NewWorkbook {
-
- public static String outputFile = "C:/test1.xls";
- public static void main(String[] args) {
-
- try {
- HSSFWorkbook wb = new HSSFWorkbook();
- FileOutputStream fileOut = new FileOutputStream(outputFile);
- wb.write(fileOut);
- fileOut.flush();
- fileOut.close();
- System.out.println("The file has been created.");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- }
简单的Excel写操作
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Date;
-
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFCellStyle;
- import org.apache.poi.hssf.usermodel.HSSFDataFormat;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-
- public class CreateCells {
-
- public static String fileTobewrite = "C:/test1.xls";
- public static void main(String[] args) throws IOException {
- try {
- HSSFWorkbook wb = new HSSFWorkbook();
- HSSFSheet sheet = wb.createSheet("new sheet");
-
- HSSFRow row = sheet.createRow(0);
-
- HSSFCell cell = row.createCell(0);
- cell.setCellValue(1);
-
-
- row.createCell(1).setCellValue(1.2);
- row.createCell(2).setCellValue("test");
- row.createCell(3).setCellValue(true);
- HSSFCellStyle cellStyle = wb.createCellStyle();
- cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
- HSSFCell dcell = row.createCell(4);
- dcell.setCellValue(new Date());
- dcell.setCellStyle(cellStyle);
- HSSFCell csCell = row.createCell(5);
- csCell.setCellType(HSSFCell.ENCODING_UTF_16);
- csCell.setCellValue("中文测试_Chinese Words Test");
-
- row.createCell(6).setCellType(HSSFCell.CELL_TYPE_ERROR);
-
-
- FileOutputStream fileOut = new FileOutputStream(fileTobewrite);
- wb.write(fileOut);
- fileOut.flush();
- fileOut.close();
- System.out.println("The cells have been added.");
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
-
- }
简单的Excel读操作
- import java.io.FileInputStream;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
-
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFDateUtil;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-
- public class ReadExcel {
- public static String fileTobeRead = "C:/test1.xls";
- public static String getCellValue(HSSFCell cell){
- String value = null;
- if (cell != null)
- {
-
- int cellType = cell.getCellType();
- switch (cellType)
- {
-
- case HSSFCell.CELL_TYPE_BLANK :
- value = "";
- break;
-
- case HSSFCell.CELL_TYPE_BOOLEAN :
- value = cell.getBooleanCellValue() ? "TRUE" : "FALSE";
- break;
-
- case HSSFCell.CELL_TYPE_ERROR :
- value = "ERR-" + cell.getErrorCellValue();
- break;
-
- case HSSFCell.CELL_TYPE_FORMULA :
- value = cell.getCellFormula();
- break;
-
- case HSSFCell.CELL_TYPE_NUMERIC :
-
- if (HSSFDateUtil.isCellDateFormatted(cell))
- {
-
- DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- value = sdf.format(cell.getDateCellValue());
- }
-
- else
- {
- value = cell.getNumericCellValue() + "";
- }
- break;
-
- case HSSFCell.CELL_TYPE_STRING :
- value = cell.getStringCellValue();
- break;
-
- default :
- value = "Unknown Cell Type:" + cell.getCellType();
- }
- }
- return value;
-
- }
-
- public static void main(String[] args) {
- try {
- HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(fileTobeRead));
-
- HSSFSheet sheet = wb.getSheet("new sheet");
-
- int rowNum = sheet.getPhysicalNumberOfRows();
- int cellNum;
- System.out.println("Row number is " + rowNum);
- HSSFRow row;
- HSSFCell cell;
- for(int i=0;i<sheet.getPhysicalNumberOfRows();i++){
-
- row = sheet.getRow(i);
- cellNum = row.getPhysicalNumberOfCells();
- System.out.println("cell number is " + cellNum);
- for(int j = 0; j < cellNum; j++){
- cell=row.getCell(j);
-
- System.out.println("row " + i + "cell "+ j + ":" + getCellValue(cell));
- }
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- }
4.设置单元格格式
- HSSFFont font = wb.createFont();
- font.setColor(HSSFFont.COLOR_RED);
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
-
- HSSFCellStyle cellStyle1 = wb.createCellStyle();
- cellStyle1.setFont(font);
-
- HSSFCell cell1 = row.createCell(1);
- cell.setCellStyle(cellStyle1);
- cell.setCellType(HSSFCell.CELL_TYPE_STRING);
- cell.setCellValue("Title");
java用org.apache.poi包操作excel
原文:http://www.cnblogs.com/gtaxmjld/p/4185711.html