江西广告网
标题:
poi操作excel-中文与单元格样式的方法
[打印本页]
作者:
仲夏
时间:
2009-2-24 10:37
标题:
poi操作excel-中文与单元格样式的方法
package *.*; import java.util.ArrayList; import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class FontCellStyle { private static HSSFFont fontStyle = null; private static HSSFCellStyle cellStyle = null; /*设置字体格式*/ public static HSSFFont getHdrFont(HSSFWorkbook wb) { fontStyle = wb.createFont(); fontStyle.setFontName("宋体"); fontStyle.setFontHeightInPoints((short)20); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); return fontStyle; } public static HSSFFont getFtrFont(HSSFWorkbook wb) { fontStyle = wb.createFont(); fontStyle.setFontName("宋体"); fontStyle.setFontHeightInPoints((short)12); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); return fontStyle; } public static HSSFFont getContentFont(HSSFWorkbook wb) { fontStyle = wb.createFont(); fontStyle.setFontName("宋体"); fontStyle.setFontHeightInPoints((short)12); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); return fontStyle; } public static HSSFFont getMergeConflictFont(HSSFWorkbook wb) { fontStyle = wb.createFont(); fontStyle.setFontName("Arial"); fontStyle.setFontHeightInPoints((short)12); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); fontStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); return fontStyle; } /*设置Excel单元格格式,引用到字体格式*/ public static HSSFCellStyle getAnyCellStyle(HSSFWorkbook wb,HSSFFont font,short align,short valign,short indent,boolean wrapText) { cellStyle =wb.createCellStyle(); if(font != null) cellStyle.setFont(font); if(align > 0) cellStyle.setAlignment(align); if(valign > 0) cellStyle.setVerticalAlignment(valign); if(indent > 0) cellStyle.setIndention(indent); cellStyle.setWrapText(wrapText); return cellStyle; } /*设置Excel单元格行高、列宽*/ public static void setDefaultHighWidth(HSSFSheet sheet) { sheet.setDefaultRowHeightInPoints(10); sheet.setDefaultColumnWidth((short) 20); } public static void setDefaultCellHighWidthInRange(HSSFSheet sheet,short[] eachCellWidth,int high) { //假定第一行和第一行所需的单元个已经建立好了,也就是说,在这之前已经调用了DesignXlsHeaderFooter.setXlsHeader sheet.setDefaultRowHeightInPoints(high);//设置默认高 /*设置各列单元格宽度*/ for(int i = 0;i < eachCellWidth.length;i ) { //System.out.print("" i "\t"); sheet.setColumnWidth((short) i,(short) ((eachCellWidth[i])*256)); } //System.out.println(); /* Iterator arrayItr = eachCellWidth.iterator(); short width; short pos = 0; while(arrayItr.hasNext()) { width = Short.parseShort((String)arrayItr.next()); sheet.setColumnWidth(pos,width); pos ; } */ }//end_setDefaultCellHighWidthInRange } /*调用方式*/ /*设置整体excel单元格格式*/ FileOutputStream fos = null; try { fos = new FileOutputStream(rptRealPathAndName); } catch (FileNotFoundException e) { // TODO Auto-generated catch block //System.out.println("创建文件失败。。。"); log.info("In WriteRptByType.writeRptTypeFive(),create file failed!!!"); log.error(e.getMessage()); //e.printStackTrace(); return -1; } HSSFWorkbook workBook = new HSSFWorkbook(); HSSFSheet sheet = workBook.createSheet();; workBook.setSheetName(0,"移动",HSSFWorkbook.ENCODING_UTF_16); HSSFCellStyle cellStyleHdr = FontCellStyle.getAnyCellStyle(workBook,FontCellStyle.getHdrFont(workBook),HSSFCellStyle.ALIGN_CENTER, HSSFCellStyle.VERTICAL_CENTER, (short)-1, true); HSSFRow curRow = sheet.createRow(0); HSSFCell curCell= curRow.createCell((short)0); curCell.setEncoding(HSSFCell.ENCODING_UTF_16); curCell.setCellStyle(cellStyleHdr ); curCell.setCellValue("可以写入汉字,无乱码"); /*.............写入文件.............*/ try { workBook.write(fos); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block //System.out.println("写错误。。。"); succFlag = -1; log.error("报表写错误:" e.getMessage()); //e.printStackTrace();
欢迎光临 江西广告网 (http://bbs.jxadw.com/)
Powered by Discuz! X3.2