/** * 本测试使用的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, XSSFCel