转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/22800915
java操作office文件现在主流的jar包有jxl和POI,由于目前jxl.jar没有人更新,并不支持Excel2007及其以上版本,Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
POI结构如下:
HSSF - 提供读写Microsoft Excel格式档案的功能。
XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。
HWPF - 提供读写Microsoft Word格式档案的功能。
HSLF - 提供读写Microsoft PowerPoint格式档案的功能。
HDGF - 提供读写Microsoft Visio格式档案的功能。
下面就简单介绍一下POI对Excel文件操作的一些基础实现。在下面的实现中,均将Excel文件中单元格数据认为是字符串。
表格中单元格java bean
/** *@Description: Excel单元格格式 */ package cn.lulei.office.model; public class ExcelCell { private int row;//行 private int column;//列 private String content;//内容 public int getRow() { return row; } public void setRow(int row) { this.row = row; } public int getColumn() { return column; } public void setColumn(int column) { this.column = column; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
表格中工作表 java bean
/** *@Description: Excel表格sheet页数据 */ package cn.lulei.office.model; import java.util.List; public class ExcelSheet { private String sheetName; private List<ExcelCell> cells; public String getSheetName() { return sheetName; } public void setSheetName(String sheetName) { this.sheetName = sheetName; } public List<ExcelCell> getCells() { return cells; } public void setCells(List<ExcelCell> cells) { this.cells = cells; } }
/** *@Description: Excel2003之前版本操作 */ package cn.lulei.office.v2003; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import cn.lulei.office.model.ExcelCell; import cn.lulei.office.model.ExcelSheet; public class Excel { private static final String excelFileSuffix = ".xls";//Excel文件后缀名 private static final String defaultSheetName = "Sheet";//默认sheet名 /** * @param sheets * @param fileName * @return * @Date:2014-4-1 * @Author:lulei * @Description: 生成Excel文件 */ public boolean create(List<ExcelSheet> sheets, String fileName) { if (sheets == null) { throw new NumberFormatException("sheets is null"); } if (fileName == null || "".equals(fileName)) { throw new NumberFormatException("fileName is null"); } try { //判断后缀名是否是 xls fileName = fileName.toLowerCase().endsWith(excelFileSuffix) ? fileName : fileName + excelFileSuffix; File file = new File(fileName); //创建Excel HSSFWorkbook wbook = new HSSFWorkbook(); int i = 1; for (ExcelSheet sheet : sheets) { //如果sheetName为空,则使用默认的命名方式 if (sheet.getSheetName() == null || "".equals(sheet.getSheetName())) { sheet.setSheetName(defaultSheetName + i); } //创建工作表 HSSFSheet hSheet = wbook.createSheet(sheet.getSheetName()); //如果工作表的内容为空,则继续下一个工作表内容 if (sheet.getCells() == null) { continue; } for (ExcelCell cell : sheet.getCells()) { //获取表中的一行,如果为空,则创建行 HSSFRow hRow = hSheet.getRow(cell.getRow()); if (hRow == null) { hRow = hSheet.createRow(cell.getRow()); } //创建改行中某列单元格 HSSFCell hCell = hRow.createCell(cell.getColumn()); //设置单元格属性为String hCell.setCellType(HSSFCell.CELL_TYPE_STRING); //设置单元格内容 hCell.setCellValue(cell.getContent()); } } //将wbook写入到文件中 FileOutputStream fos = new FileOutputStream(file); wbook.write(fos); fos.flush(); fos.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * @param rowStart * @param columnStart * @param rowEnd * @param columnEnd * @param sheetNo * @param fileName * @return * @Date:2014-4-1 * @Author:lulei * @Description: 读取excel文件sheet页数据 */ public List<ExcelCell> getCells(int rowStart, int columnStart, int rowEnd, int columnEnd, int sheetNo, String fileName) { List<ExcelCell> cells = null; try { File file = new File(fileName); //判断文件是否存在 if (!file.exists()) { throw new NullPointerException(fileName + " is not exist"); } //读取Excel文件 HSSFWorkbook wbook = new HSSFWorkbook(new FileInputStream(file)); HSSFSheet sheet = wbook.getSheetAt(sheetNo); //如果工作表格为空,则直接返回空值 if (sheet == null) { return null; } rowStart = rowStart < 0 ? 0 : rowStart; columnStart = columnStart < 0 ? 0 : columnStart; cells = new ArrayList<ExcelCell>(); //循环行 for (int i = rowStart; i <= rowEnd; i++) { HSSFRow row = sheet.getRow(i); //如果改行为空,继续下一行 if (row == null) { continue; } //循环列 for (int j = columnStart; j <= columnEnd; j++) { HSSFCell cell = row.getCell(j); //如果该单元格不为空,添加到返回数组中 if (cell != null) { ExcelCell eCell = new ExcelCell(); eCell.setRow(i); eCell.setColumn(j); eCell.setContent(cell.toString()); cells.add(eCell); } } } } catch (Exception e) { e.printStackTrace(); return null; } //返回 数据 return cells; } /** * @param sheets * @param fileName * @return * @Date:2014-4-1 * @Author:lulei * @Description: Excel更新操作 */ public boolean update(List<ExcelSheet> sheets, String fileName) { if (sheets == null) { throw new NumberFormatException("sheets is null"); } try { File file = new File(fileName); //判断文件是否存在 if (!file.exists()) { throw new NullPointerException(fileName + " is not exist"); } //读取Excel文件 HSSFWorkbook wbook = new HSSFWorkbook(new FileInputStream(file)); for (ExcelSheet sheet : sheets) { //获取Excel中的工作表,如果为空,则创建表 HSSFSheet hSheet = wbook.getSheet(sheet.getSheetName()); if (hSheet == null) { hSheet = wbook.createSheet(sheet.getSheetName()); } for (ExcelCell cell : sheet.getCells()) { //获取表中的一行,如果为空,则创建行 HSSFRow hRow = hSheet.getRow(cell.getRow()); if (hRow == null) { hRow = hSheet.createRow(cell.getRow()); } //获取某列单元格,如果为空,则创建单元格 HSSFCell hCell = hRow.getCell(cell.getColumn()); if (hCell == null) { hCell = hRow.createCell(cell.getColumn()); } //设置单元格属性为String hCell.setCellType(HSSFCell.CELL_TYPE_STRING); //设置单元格内容 hCell.setCellValue(cell.getContent()); } } //将wbook写入到文件中 FileOutputStream fos = new FileOutputStream(file); wbook.write(fos); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } }
/** *@Description: Excel2003之前版本操作 */ package cn.lulei.office.v2007; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import cn.lulei.office.model.ExcelCell; import cn.lulei.office.model.ExcelSheet; public class Excel { private static final String excelFileSuffix = ".xlsx";//Excel文件后缀名 private static final String defaultSheetName = "Sheet";//默认sheet名 /** * @param sheets * @param fileName * @return * @Date:2014-4-1 * @Author:lulei * @Description: 生成Excel文件 */ public boolean create(List<ExcelSheet> sheets, String fileName) { if (sheets == null) { throw new NumberFormatException("sheets is null"); } if (fileName == null || "".equals(fileName)) { throw new NumberFormatException("fileName is null"); } try { //判断后缀名是否是 xls fileName = fileName.toLowerCase().endsWith(excelFileSuffix) ? fileName : fileName + excelFileSuffix; File file = new File(fileName); //创建Excel XSSFWorkbook wbook = new XSSFWorkbook(); int i = 1; for (ExcelSheet sheet : sheets) { //如果sheetName为空,则使用默认的命名方式 if (sheet.getSheetName() == null || "".equals(sheet.getSheetName())) { sheet.setSheetName(defaultSheetName + i); } //创建工作表 XSSFSheet hSheet = wbook.createSheet(sheet.getSheetName()); //如果工作表的内容为空,则继续下一个工作表内容 if (sheet.getCells() == null) { continue; } for (ExcelCell cell : sheet.getCells()) { //获取表中的一行,如果为空,则创建行 XSSFRow hRow = hSheet.getRow(cell.getRow()); if (hRow == null) { hRow = hSheet.createRow(cell.getRow()); } //创建改行中某列单元格 XSSFCell hCell = hRow.createCell(cell.getColumn()); //设置单元格属性为String hCell.setCellType(XSSFCell.CELL_TYPE_STRING); //设置单元格内容 hCell.setCellValue(cell.getContent()); } } //将wbook写入到文件中 FileOutputStream fos = new FileOutputStream(file); wbook.write(fos); fos.flush(); fos.close(); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * @param rowStart * @param columnStart * @param rowEnd * @param columnEnd * @param sheetNo * @param fileName * @return * @Date:2014-4-1 * @Author:lulei * @Description: 读取excel文件sheet页数据 */ public List<ExcelCell> getCells(int rowStart, int columnStart, int rowEnd, int columnEnd, int sheetNo, String fileName) { List<ExcelCell> cells = null; try { File file = new File(fileName); //判断文件是否存在 if (!file.exists()) { throw new NullPointerException(fileName + " is not exist"); } //读取Excel文件 XSSFWorkbook wbook = new XSSFWorkbook(new FileInputStream(file)); XSSFSheet sheet = wbook.getSheetAt(sheetNo); //如果工作表格为空,则直接返回空值 if (sheet == null) { return null; } rowStart = rowStart < 0 ? 0 : rowStart; columnStart = columnStart < 0 ? 0 : columnStart; cells = new ArrayList<ExcelCell>(); //循环行 for (int i = rowStart; i <= rowEnd; i++) { XSSFRow row = sheet.getRow(i); //如果改行为空,继续下一行 if (row == null) { continue; } //循环列 for (int j = columnStart; j <= columnEnd; j++) { XSSFCell cell = row.getCell(j); //如果该单元格不为空,添加到返回数组中 if (cell != null) { ExcelCell eCell = new ExcelCell(); eCell.setRow(i); eCell.setColumn(j); eCell.setContent(cell.toString()); cells.add(eCell); } } } } catch (Exception e) { e.printStackTrace(); return null; } //返回数据 return cells; } /** * @param sheets * @param fileName * @return * @Date:2014-4-1 * @Author:lulei * @Description: Excel更新操作 */ public boolean update(List<ExcelSheet> sheets, String fileName) { if (sheets == null) { throw new NumberFormatException("sheets is null"); } try { File file = new File(fileName); //判断文件是否存在 if (!file.exists()) { throw new NullPointerException(fileName + " is not exist"); } //读取Excel文件 XSSFWorkbook wbook = new XSSFWorkbook(new FileInputStream(file)); for (ExcelSheet sheet : sheets) { //获取Excel中的工作表,如果为空,则创建表 XSSFSheet hSheet = wbook.getSheet(sheet.getSheetName()); if (hSheet == null) { hSheet = wbook.createSheet(sheet.getSheetName()); } for (ExcelCell cell : sheet.getCells()) { //获取表中的一行,如果为空,则创建行 XSSFRow hRow = hSheet.getRow(cell.getRow()); if (hRow == null) { hRow = hSheet.createRow(cell.getRow()); } //获取某列单元格,如果为空,则创建单元格 XSSFCell hCell = hRow.getCell(cell.getColumn()); if (hCell == null) { hCell = hRow.createCell(cell.getColumn()); } //设置单元格属性为String hCell.setCellType(XSSFCell.CELL_TYPE_STRING); //设置单元格内容 hCell.setCellValue(cell.getContent()); } } //将wbook写入到文件中 FileOutputStream fos = new FileOutputStream(file); wbook.write(fos); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } }Excel文件统一向外服务类
/** *@Description: Excel操作提供服务接口 */ package cn.lulei.office.service; import java.util.List; import cn.lulei.office.model.ExcelCell; import cn.lulei.office.model.ExcelSheet; public class ExcelService { public static final int Excel2003 = 0; public static final int Excel2007 = 1; private cn.lulei.office.v2003.Excel excel2003 = new cn.lulei.office.v2003.Excel(); private cn.lulei.office.v2007.Excel excel2007 = new cn.lulei.office.v2007.Excel(); /** * @param sheets * @param fileName 文件绝对 * @param excelVersion excel版本 * @return * @Date:2014-4-2 * @Author:lulei * @Description: 创建Excel */ public boolean create(List<ExcelSheet> sheets, String fileName, int excelVersion) { if (excelVersion >= Excel2007) { return excel2007.create(sheets, fileName); } else { return excel2003.create(sheets, fileName); } } /** * @param sheets * @param fileName 文件绝对路径 * @return * @Date:2014-4-2 * @Author:lulei * @Description: 创建Excel */ public boolean create(List<ExcelSheet> sheets, String fileName) { return create(sheets, fileName, Excel2007); } /** * @param rowStart 起始行 * @param columnStart 起始列 * @param rowEnd 终止行 * @param columnEnd 终止列 * @param sheetNo sheet编号~0 1 * @param fileName 文件名 * @param excelVersion 使用excel版本 * @return * @Date:2014-4-2 * @Author:lulei * @Description: 读取Excel */ public List<ExcelCell> getCells(int rowStart, int columnStart, int rowEnd, int columnEnd, int sheetNo, String fileName, int excelVersion) { if (excelVersion >= Excel2007) { return excel2007.getCells(rowStart, columnStart, rowEnd, columnEnd, sheetNo, fileName); } else { return excel2003.getCells(rowStart, columnStart, rowEnd, columnEnd, sheetNo, fileName); } } /** * @param rowStart 起始行 * @param columnStart 起始列 * @param rowEnd 终止行 * @param columnEnd 终止列 * @param sheetNo sheet编号~0 1 * @param fileName 文件名 * @return * @Date:2014-4-2 * @Author:lulei * @Description: 读取Excel */ public List<ExcelCell> getCells(int rowStart, int columnStart, int rowEnd, int columnEnd, int sheetNo, String fileName) { return getCells(rowStart, columnStart, rowEnd, columnEnd, sheetNo, fileName, Excel2007); } /** * @param sheets * @param fileName * @param excelVersion * @return * @Date:2014-4-2 * @Author:lulei * @Description: 更新Excel */ public boolean update(List<ExcelSheet> sheets, String fileName, int excelVersion) { if (excelVersion >= Excel2007) { return excel2007.update(sheets, fileName); } else { return excel2003.update(sheets, fileName); } } /** * @param sheets * @param fileName * @return * @Date:2014-4-2 * @Author:lulei * @Description: 更新Excel */ public boolean update(List<ExcelSheet> sheets, String fileName) { return update(sheets, fileName, Excel2007); } }这里就只写出了一些具体的实现方式,至于测试和用法,这里并没有涉及,在使用中,只需要去构建ExcelSheet即可~
在以后的bolg中,将继续更新Excel的其他操作以及其他office文件的操作