public class PoiTest extends TestCase{ List<User> userList; SimpleDateFormat dateFormat; DecimalFormat numberFormat; public void testPoi() throws IOException{ System.out.println("testPoi 1"); HSSFWorkbook workbook = create(); HSSFSheet sheet = creatresheet(workbook, "客户报表"); int size = userList.size(); initColumnWidth(size, 4000, sheet); HSSFFont font = createFont(workbook); HSSFCellStyle hssfCellStyle = createCellStyle(workbook, font); HSSFCellStyle headCellStyle = createHeadCellStyle(workbook, null); HSSFRow headHssfRow = createRow(sheet, 0, 1000); setCellValue(headHssfRow, font, 0, 0, "姓名", headCellStyle); setCellValue(headHssfRow, font, 0, 1, "年龄", headCellStyle); setCellValue(headHssfRow, font, 0, 2, "注册时间", headCellStyle); int rowOffset = 1; for (int i = 0; i < size; i++) { HSSFRow hssfRow = createRow(sheet, i + rowOffset, 500); User user = userList.get(i); setCellValue(hssfRow, font, i + rowOffset, 0, user.getUserName(), hssfCellStyle); setCellValue(hssfRow, font, i + rowOffset, 1, String.valueOf(user.getAge()), hssfCellStyle); setCellValue(hssfRow, font, i + rowOffset, 2, dateFormat.format(user.getAddTime()), hssfCellStyle); } // 指定合并区域 // sheet.addMergedRegion(new Region(1, (short) 0, 1, (short) colSum)); OutputStream outputStream = new FileOutputStream(new File("d:/报表.xls")); workbook.write(outputStream); } private void setCellValue(HSSFRow hssfRow, HSSFFont font, int rownum, int col, String value, HSSFCellStyle style){ HSSFCell cell = hssfRow.createCell(col); cell.setCellType(HSSFCell.ENCODING_UTF_16); cell.setCellValue(new HSSFRichTextString(value)); if (style != null) { cell.setCellStyle(style); } } private HSSFRow createRow(HSSFSheet sheet, int rownum, int height){ HSSFRow hssfRow = sheet.createRow(rownum); hssfRow.setHeight((short) height); return hssfRow; } private HSSFFont createFont(HSSFWorkbook workbook){ HSSFFont font = workbook.createFont(); font.setFontName("宋体"); font.setFontHeight((short) 200); return font; } private HSSFCellStyle createHeadCellStyle(HSSFWorkbook workbook, HSSFFont font){ HSSFCellStyle headStyle = workbook.createCellStyle(); headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); headStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index); headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); setBorderStyle(headStyle); if (font != null) { headStyle.setFont(font); } return headStyle; } private HSSFCellStyle createCellStyle(HSSFWorkbook workbook, HSSFFont font){ HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); cellStyle.setWrapText(true); setBorderStyle(cellStyle); if (font != null) { cellStyle.setFont(font); } return cellStyle; } public void setBorderStyle(HSSFCellStyle cellStyle){ cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setLeftBorderColor(HSSFColor.BLACK.index); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setTopBorderColor(HSSFColor.BLACK.index); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setRightBorderColor(HSSFColor.BLACK.index); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBottomBorderColor(HSSFColor.BLACK.index); } private void initColumnWidth(int size, int width, HSSFSheet sheet){ for (int i = 0; i < size; i++) { sheet.setColumnWidth(i, width); } } private HSSFSheet creatresheet(HSSFWorkbook workbook, String sheetName){ return workbook.createSheet(sheetName); } private HSSFWorkbook create(){ return new HSSFWorkbook(); } /** * @see junit.framework.TestCase#setUp() */ @Override protected void setUp() throws Exception { // TODO Auto-generated method stub super.setUp(); userList = new ArrayList<User>(); int count = 10; for (int i = 0; i < count; i++) { userList.add(new User(("x" + i), (i+1), new Date())); } dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); numberFormat = new DecimalFormat("0.00"); System.out.println("setUp"); } /** * @see junit.framework.TestCase#tearDown() */ @Override protected void tearDown() throws Exception { // TODO Auto-generated method stub super.tearDown(); System.out.println("tearDown"); userList = null; } }