package test; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; public class Test { private static final Logger logger = Logger.getLogger(Test.class); public static void main(String[] args) throws IOException { String text[] = { "BLACK", "BROWN", "OLIVE_GREEN", "DARK_GREEN", "DARK_TEAL", "DARK_BLUE", "INDIGO", "GREY_80_PERCENT", "ORANGE", "DARK_YELLOW", "GREEN", "TEAL", "BLUE", "BLUE_GREY", "GREY_50_PERCENT", "RED", "LIGHT_ORANGE", "LIME", "SEA_GREEN", "AQUA", "LIGHT_BLUE", "VIOLET", "GREY_40_PERCENT", "PINK", "GOLD", "YELLOW", "BRIGHT_GREEN", "TURQUOISE", "DARK_RED", "SKY_BLUE", "PLUM", "GREY_25_PERCENT", "ROSE", "LIGHT_YELLOW", "LIGHT_GREEN", "LIGHT_TURQUOISE", "PALE_BLUE", "LAVENDER", "WHITE", "CORNFLOWER_BLUE", "LEMON_CHIFFON", "MAROON", "ORCHID", "CORAL", "ROYAL_BLUE", "LIGHT_CORNFLOWER_BLUE", "TAN" }; short color[] = { HSSFColor.BLACK.index, HSSFColor.BROWN.index, HSSFColor.OLIVE_GREEN.index, HSSFColor.DARK_GREEN.index, HSSFColor.DARK_TEAL.index, HSSFColor.DARK_BLUE.index, HSSFColor.INDIGO.index, HSSFColor.GREY_80_PERCENT.index, HSSFColor.ORANGE.index, HSSFColor.DARK_YELLOW.index, HSSFColor.GREEN.index, HSSFColor.TEAL.index, HSSFColor.BLUE.index, HSSFColor.BLUE_GREY.index, HSSFColor.GREY_50_PERCENT.index, HSSFColor.RED.index, HSSFColor.LIGHT_ORANGE.index, HSSFColor.LIME.index, HSSFColor.SEA_GREEN.index, HSSFColor.AQUA.index, HSSFColor.LIGHT_BLUE.index, HSSFColor.VIOLET.index, HSSFColor.GREY_40_PERCENT.index, HSSFColor.PINK.index, HSSFColor.GOLD.index, HSSFColor.YELLOW.index, HSSFColor.BRIGHT_GREEN.index, HSSFColor.TURQUOISE.index, HSSFColor.DARK_RED.index, HSSFColor.SKY_BLUE.index, HSSFColor.PLUM.index, HSSFColor.GREY_25_PERCENT.index, HSSFColor.ROSE.index, HSSFColor.LIGHT_YELLOW.index, HSSFColor.LIGHT_GREEN.index, HSSFColor.LIGHT_TURQUOISE.index, HSSFColor.PALE_BLUE.index, HSSFColor.LAVENDER.index, HSSFColor.WHITE.index, HSSFColor.CORNFLOWER_BLUE.index, HSSFColor.LEMON_CHIFFON.index, HSSFColor.MAROON.index, HSSFColor.ORCHID.index, HSSFColor.CORAL.index, HSSFColor.ROYAL_BLUE.index, HSSFColor.LIGHT_CORNFLOWER_BLUE.index, HSSFColor.TAN.index }; HSSFWorkbook wb = new HSSFWorkbook(); // 创建Excel HSSFSheet sheet = wb.createSheet(); // 创建Excel表单 sheet.setDefaultColumnWidth(25); // 表单的默认宽度 int row = 0, col_per_row = 5; HSSFRow fRow = sheet.createRow(row++); for (int i = 0; i < color.length; i++) { if (i % col_per_row == 0) { // 每行5列 fRow = sheet.createRow(row++); } HSSFCell fCell = fRow.createCell(i % col_per_row); HSSFCellStyle cellStyle = createNormalCellStyle(wb, color[i]); fCell.setCellStyle(cellStyle); fCell.setCellValue(text[i]); } write2file(wb); } private static HSSFCellStyle createNormalCellStyle(HSSFWorkbook wb, short color) { HSSFCellStyle fCellStyle = wb.createCellStyle(); fCellStyle.setFillForegroundColor(color); fCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); // 指定单元格居中对齐,边框为细 fCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); fCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 指定当单元格内容显示不下时自动换行 fCellStyle.setWrapText(true); fCellStyle.setShrinkToFit(true); return fCellStyle; } private static void write2file(HSSFWorkbook wb) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss_SSS"); String fileName = sdf.format(new Date()) + ".xls"; logger.info("exportExcel, fileName:" + fileName); try { FileOutputStream fileOut = new FileOutputStream(fileName); wb.write(fileOut); fileOut.close(); } catch (IOException e) { e.printStackTrace(); } } }