/** * 本测试使用的POI版本为3.6 * 其Excel测试案例全部来自POI文档,部分根据情况做了不同程度的修改. * 该测试仅供交流学习使用,代码根据个人理解基本上已做注释 * 测试环境: * OS: Windows 7 Ultimate US_en * IDE: MyEclipse 8.0 GA * JDK: JDK_1.6_20 * MS Excel: Excel 2007 Zh_cn * 作者:WESTDREAM */ package junit.westdream.test; import java.awt.Color; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Calendar; import java.util.Date; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFFooter; import org.apache.poi.hssf.usermodel.HSSFPatriarch; import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFShape; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFSimpleShape; import org.apache.poi.hssf.usermodel.HSSFTextbox; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Drawing; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.Picture; import org.apache.poi.ss.usermodel.PrintSetup; import org.apache.poi.ss.usermodel.RichTextString; 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.ss.usermodel.WorkbookFactory; import org.apache.poi.ss.usermodel.contrib.CellUtil; import org.apache.poi.ss.usermodel.contrib.RegionUtil; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; import org.apache.poi.util.IOUtils; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.junit.BeforeClass; import org.junit.Test; /** * @author WESTDREAM * @since 2010-8-7 下午10:34:03 */ public class POIExcelTest { /** * @throws java.lang.Exception */ public static final String XLS_WORKBOOK_LOCATION = "D:/workbook.xls"; public static final String XLS_OR_XLSX_DIR = "D:/"; public static final String XLSX_WORKBOOK_LOCATION = "D:/workbook.xlsx"; public static final String IMAGE_LOCATION = "F:/Pictures/Picture/love2.jpg"; @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void testWriteExcel() { //## 重复利用 的对象 ##// Workbook wb = null; FileOutputStream fileOut = null; CellStyle cellStyle = null; Cell cell = null; Font font = null; /** * EXCEL早期版本 */ try { //## 创建早期EXCEL的Workbook ##// wb = new HSSFWorkbook(); //## 获取HSSF和XSSF的辅助类 ##// CreationHelper createHelper = wb.getCreationHelper(); //## 创建一个名为“New Sheet”的Sheet ##// Sheet sheet = wb.createSheet("New Sheet"); /** 第一行 --- CELL创建,数据填充及日期格式 **/ Row row1 = sheet.createRow(0); //Cell cell = row.createCell(0); //cell.setCellValue(1); //## 在相应的位置填充数据 ##// row1.createCell(0).setCellValue(1); row1.createCell(1).setCellValue(1.2); row1.createCell(2).setCellValue(createHelper.createRichTextString("CreationHelper---字符串")); row1.createCell(3).setCellValue(true); //## 填充日期类型的数据---未设置Cell Style ##// row1.createCell(4).setCellValue(new Date()); //## 填充日期类型的数据---已设置Cell Style ##// cellStyle = wb.createCellStyle(); cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy年MM月dd日 hh:mm:ss")); //cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("mm/dd/yyyy h:mm")); cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd hh:mm:ss")); cell = row1.createCell(5); cell.setCellValue(new Date()); cell.setCellStyle(cellStyle); //## 另一种创建日期的方法 ##// /*cell = row1.createCell(6); cell.setCellValue(Calendar.getInstance()); cell.setCellStyle(cellStyle);*/ /** 第二行 --- 数据类型 **/ Row row2 = sheet.createRow(1); row2.createCell(0).setCellValue(1.1); row2.createCell(1).setCellValue(new Date()); row2.createCell(2).setCellValue(Calendar.getInstance()); row2.createCell(3).setCellValue("字符串"); row2.createCell(4).setCellValue(true); //## 错误的CELL数据格式 ##// row2.createCell(5).setCellType(HSSFCell.CELL_TYPE_ERROR); /** 第三行 --- CELL的各种对齐方式 **/ Row row3 = sheet.createRow(2); row3.setHeightInPoints(30); //## 水平居中,底端对齐 ##// createCell(wb, row3, (short)0, XSSFCellStyle.ALIGN_CENTER, XSSFCellStyle.VERTICAL_BOTTOM); //## 水平居中,垂直居中 ##// createCell(wb, row3, (short)1, XSSFCellStyle.ALIGN_CENTER_SELECTION, XSSFCellStyle.VERTICAL_BOTTOM); //## 填充 ,垂直居中 ##// createCell(wb, row3, (short)2, XSSFCellStyle.ALIGN_FILL, XSSFCellStyle.VERTICAL_CENTER); //## 左对齐,垂直居中 ##// createCell(wb, row3, (short)3, XSSFCellStyle.ALIGN_GENERAL, XSSFCellStyle.VERTICAL_CENTER); //## 左对齐,顶端对齐 ##// createCell(wb, row3, (short)4, XSSFCellStyle.ALIGN_JUSTIFY, XSSFCellStyle.VERTICAL_JUSTIFY); //## 左对齐,顶端对齐 ##// createCell(wb, row3, (short)5, XSSFCellStyle.ALIGN_LEFT, XSSFCellStyle.VERTICAL_TOP); //## 右对齐,顶端对齐 ##// createCell(wb, row3, (short)6, XSSFCellStyle.ALIGN_RIGHT, XSSFCellStyle.VERTICAL_TOP); /** 第四行 --- CELL边框 **/ Row row4 = sheet.createRow(3); cell = row4.createCell(1); cell.setCellValue(4); cellStyle = wb.createCellStyle(); //## 设置底部边框为THIN ##// cellStyle.setBorderBottom(CellStyle.BORDER_THIN); //## 设置底部边框颜色为黑色 ##// cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex()); //## 设置左边边框为THIN ##// cellStyle.setBorderLeft(CellStyle.BORDER_THIN); //## 设置左边边框颜色为红色 ##// cellStyle.setLeftBorderColor(IndexedColors.RED.getIndex()); //## 设置右边边框为THIN ##// cellStyle.setBorderRight(CellStyle.BORDER_THIN); //## 设置右边边框颜色为蓝色 ##// cellStyle.setRightBorderColor(IndexedColors.BLUE.getIndex()); //## 设置顶部边框为MEDIUM DASHED ##// cellStyle.setBorderTop(CellStyle.BORDER_MEDIUM_DASHED); //## 设置顶部边框颜色为黑色 ##// cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex()); cell.setCellStyle(cellStyle); /** 第五行 --- 填充与颜色 **/ Row row5 = sheet.createRow((short) 4); //## Aqua背景 ##// cellStyle = wb.createCellStyle(); cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex()); //## 设置填充模式为BIG SPOTS ##// cellStyle.setFillPattern(CellStyle.BIG_SPOTS); cell = row5.createCell((short) 1); cell.setCellValue("Aqua背景"); cell.setCellStyle(cellStyle); //## 橙色前景色(相对 于CELL背景) ##// cellStyle = wb.createCellStyle(); cellStyle.setFillForegroundColor(IndexedColors.ORANGE.getIndex()); //## 设置填充模式为SOLID FOREGROUND ##// cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); cell = row5.createCell((short) 2); cell.setCellValue("橙色前景色"); cell.setCellStyle(cellStyle); /** 第六行 --- 合并单元格 **/ Row row6 = sheet.createRow((short) 5); cell = row6.createCell((short) 4); cell.setCellValue("合并单元格测试"); //## Wrong:EXCEL 2007中打开workbook.xls文件看不到"合并单元格测试",但单元格已经合并了 ##// /*sheet.addMergedRegion(new CellRangeAddress( 3, //first row (0-based) 5, //last row (0-based) 4, //first column (0-based) 6 //last column (0-based) ));*/ //## 正确合并单元格 注意:与上不同的是first row=last row ##// sheet.addMergedRegion(new CellRangeAddress( 5, //first row (0-based) 5, //last row (0-based) 4, //first column (0-based) 6 //last column (0-based) )); /** 第七行 --- 字体 **/ Row row7 = sheet.createRow(6); //## 创建字体 ##// //注意:POI限制一个Workbook创建的Font对象最多为32767,所以不要为每个CELL创建一个字体,建议重用字体 font = wb.createFont(); //## 设置字体大小为24 ##// font.setFontHeightInPoints((short)24); //## 设置字体样式为华文隶书 ##// font.setFontName("华文隶书"); //## 斜体 ##// font.setItalic(true); //## 添加删除线 ##// font.setStrikeout(true); //## 将字体添加到样式中 ##// cellStyle = wb.createCellStyle(); cellStyle.setFont(font); cell = row7.createCell(1); cell.setCellValue("字体测试"); cell.setCellStyle(cellStyle); /** 第八行 --- 自定义颜色 **/ Row row8 = sheet.createRow(7); cell = row8.createCell(0); cell.setCellValue("自定义颜色测试"); cellStyle = wb.createCellStyle(); //## 设置填充前景色为LIME ##// cellStyle.setFillForegroundColor(HSSFColor.LIME.index); //## 设置填充模式为SOLID FOREGROUND ##// cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); font = wb.createFont(); //## 设置字体颜色为红色 ##// font.setColor(HSSFColor.RED.index); cellStyle.setFont(font); cell.setCellStyle(cellStyle); /* cell.setCellValue("自定义颜色测试Palette"); //creating a custom palette for the workbook HSSFPalette palette = ((HSSFWorkbook)wb).getCustomPalette(); //replacing the standard red with freebsd.org red palette.setColorAtIndex(HSSFColor.RED.index, (byte) 153, //RGB red (0-255) (byte) 0, //RGB green (byte) 0 //RGB blue ); //replacing lime with freebsd.org gold palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);*/ /** 第九行 --- 换行 **/ Row row9 = sheet.createRow(8); cell = row9.createCell(2); cell.setCellValue("使用 /n及Word-wrap创建一个新行"); cellStyle = wb.createCellStyle(); //## 设置WrapText为true ##// cellStyle.setWrapText(true); cell.setCellStyle(cellStyle); //## 设置行的高度以适应新行 ---两行##// row9.setHeightInPoints((2*sheet.getDefaultRowHeightInPoints())); //## 调整列宽 ##// sheet.autoSizeColumn(2); /** 第十行 --- 数据格式 **/ DataFormat format = wb.createDataFormat(); Row row10 = sheet.createRow(9); cell = row10.createCell(0); cell.setCellValue(11111.25); cellStyle = wb.createCellStyle(); //## 一位小数 ##// cellStyle.setDataFormat(format.getFormat("0.0")); cell.setCellStyle(cellStyle); cell = row10.createCell(1); cell.setCellValue(11111.25); cellStyle = wb.createCellStyle(); //## 四位小数,千位逗号隔开 ##// // #,###.0000效果一样 cellStyle.setDataFormat(format.getFormat("#,##0.0000")); cell.setCellStyle(cellStyle); //## 将文件写到硬盘上 ##// fileOut = new FileOutputStream(XLS_WORKBOOK_LOCATION); wb.write(fileOut); fileOut.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } /** * EXCEL 2007及以后 */ /* try { wb = new XSSFWorkbook(); wb.createSheet("sheet1"); Cell cell = row.createCell( 0); cell.setCellValue("custom XSSF colors"); CellStyle style1 = wb.createCellStyle(); style1.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128))); style1.setFillPattern(CellStyle.SOLID_FOREGROUND); fileOut = new FileOutputStream("d:/workbook.xlsx"); wb.write(fileOut); fileOut.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }*/ } /** * 创建相应格式的CELL */ public void createCell(Workbook wb, Row row, short column, short halign, short valign) { Cell cell = row.createCell(column); //## 给CELL赋值 ##// cell.setCellValue("对齐排列"); CellStyle cellStyle = wb.createCellStyle(); //## 设置水平对齐方式 ##// cellStyle.setAlignment(halign); //## 设置垂直对齐方式 ##// cellStyle.setVerticalAlignment(valign); //## 添加CELL样式 ##// cell.setCellStyle(cellStyle); } /** * 测试POI EXCEL迭代和或CELL中的值 */ @Test public void testExcelIteratorAndCellContents() { try { //## 创建HSSFWorkbook实例 ##// Workbook wb = new HSSFWorkbook(new FileInputStream(XLS_WORKBOOK_LOCATION)); //## 获得第一个SHEET ##// Sheet sheet = wb.getSheetAt(0); // or we could cast into HSSFSheet,that doesn't matter /** 第一种迭代方法 **/ /* //## 迭代ROW ##// for (Iterator