6 附录
HSSFColor类:
生成代码:
public class TestHSSFColor { @SuppressWarnings({ "rawtypes", "unused" }) public static List<String> SysoColor() throws ClassNotFoundException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException{ List<String> list = new ArrayList<String>(); Class clazz = Class.forName(HSSFColor.class.getName()); Class[] Classes = clazz.getDeclaredClasses(); Method[] methods = clazz.getDeclaredMethods(); for(int i = 0; i < Classes.length; i++){ Class colorClazz = Classes[i]; Field field = colorClazz.getDeclaredField("index"); Object property = field.get(colorClazz); list.add(colorClazz.getSimpleName()+":"+property); } return list; }
public static HSSFWorkbook write(){ HSSFWorkbook workbook = null; try { int count = 0; List<String> list = SysoColor(); workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("HSSFColor"); double size = list.size(); double rowlength = size/5.0; for(int rownum = 0; rownum < Math.ceil(rowlength); rownum++){ HSSFRow row = sheet.createRow(rownum); for(int cellnum = 0; cellnum < 5; cellnum++){ HSSFCell cell = row.createCell(cellnum); if(count >= list.size()){ break; } String str = list.get(count); count++; String indexStr = str.substring(str.indexOf(":")+1, str.length()); cell.setCellValue(new HSSFRichTextString(str)); HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setFillForegroundColor(Short.valueOf(indexStr)); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); cell.setCellStyle(cellStyle); } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return workbook; }
public static void main(String[] args) { OutputStream outputStream = null; try { HSSFWorkbook workbook = TestHSSFColor.write(); outputStream = new FileOutputStream(new File("E:\\helloPOI1.xls")); workbook.write(outputStream); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ if(outputStream != null){ try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
} |