POI 怎么设置Excel整列的CellStyle啊,而不是循环每个Cell。因为现在是生成Excel模板,不知道客户会输入多少行。
上面的回答者,这些代码只是把单元格格式放在一个MAP里面,方便调用,我现在做的项目就是这样的。
可是问题者问的问题是:[color=red]如何设置整列的格式[/color],这列每个单元格的格式应该是一样的,所以只要一种单元格格式就行了,当然调用你MAP中的一个就可以了,你可以试试我前面说的方法
[color=red]sheet.setDefaultColumnStyle(short column, CellStyle style)[/color],
你就知道问的人的意思了~~~不过你真的很热心,呼呼
好像没有这个方法,CellStyle是Cell的字段,没有Cell怎么设置啊。动态生成Cell时再根据需要设置它的CellStyle不行吗?我也不是太懂的。
我也正在写一个小项目,之前做了导入的工作,现在也该写导出的代码了。查了查相关的资料,一般要是应对大数据的话,好像一般的方案都是多sheet完成。我觉得style还是在new的时候设定吧。我也是刚刚接触poi,只是了解一点点,我的想法不一定对,仅供参考吧。
你所说的问题是有办法的解决的,最近也在搞个项目,暂时没有用到模板,但用到别的很多方法,顺便帮你找了找,你说的设置一列的属性,不需要用循环,一个方法就能搞定,首先你是设置列的属性,肯定不是在cell上来完成,应该在sheet上进行设置。
[color=red]sheet.setDefaultColumnStyle(short column, CellStyle style),[/color]
前面一个参数是列号,后面一个参数就是Style了,这样应该就可以了,多看看POI的API吧~~~
刚才看源码是找到这一兜,应该是能设定整列的,希望对你能有帮助。
[code="java"]
/**
* create a library of cell styles
*/
private static Map createStyles(Workbook wb){
Map styles = new HashMap();
DataFormat df = wb.createDataFormat();
CellStyle style;
Font headerFont = wb.createFont();
headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(headerFont);
styles.put("header", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setFont(headerFont);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("header_date", style);
Font font1 = wb.createFont();
font1.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setFont(font1);
styles.put("cell_b", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setFont(font1);
styles.put("cell_b_centered", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font1);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_b_date", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font1);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_g", style);
Font font2 = wb.createFont();
font2.setColor(IndexedColors.BLUE.getIndex());
font2.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setFont(font2);
styles.put("cell_bb", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setFont(font1);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_bg", style);
Font font3 = wb.createFont();
font3.setFontHeightInPoints((short)14);
font3.setColor(IndexedColors.DARK_BLUE.getIndex());
font3.setBoldweight(Font.BOLDWEIGHT_BOLD);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setFont(font3);
style.setWrapText(true);
styles.put("cell_h", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setWrapText(true);
styles.put("cell_normal", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setWrapText(true);
styles.put("cell_normal_centered", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_RIGHT);
style.setWrapText(true);
style.setDataFormat(df.getFormat("d-mmm"));
styles.put("cell_normal_date", style);
style = createBorderedStyle(wb);
style.setAlignment(CellStyle.ALIGN_LEFT);
style.setIndention((short)1);
style.setWrapText(true);
styles.put("cell_indented", style);
style = createBorderedStyle(wb);
style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
styles.put("cell_blue", style);
return styles;
}
private static CellStyle createBorderedStyle(Workbook wb){
CellStyle style = wb.createCellStyle();
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
return style;
}
[/code]