1、Sheet sheet1 = wb.createSheet(HSSF_Sheet_1);Sheet sheet2 = wb.createSheet(HSSF_Sheet_2/Row表示一行Cell表示一列Row row = null;Cell cell = null;for(int i=0;i60;i=i+2) /获得这个sheet的第i行row = sheet1.createRow(i);/设置行长度自动/row.setHeight(short)500);row.setHeightInPoints(20);/row.setZeroHeight(true);for(int j=0;j25;j+
2、) /设置每个sheet每一行的宽度,自动,根据需求自行确定sheet1.autoSizeColumn(j+1, true);/创建一个基本的样式CellStyle cellStyle = SummaryHSSF.createStyleCell(wb);/获得这一行的每j列cell = row.createCell(j);if(j=0) /设置文字在单元格里面的位置cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);/先创建字体样式
3、,并把这个样式加到单元格的字体里面cellStyle.setFont(createFonts(wb);/把这个样式加到单元格里面cell.setCellStyle(cellStyle);/给单元格设值cell.setCellValue(true);else if(j=1) /设置这个样式的格式(Format) cellStyle = SummaryHSSF.setCellFormat(helper,cellStyle, #,#0.0000cell.setCellValue(new Double(2008.2008);else if(j=2) cell.setCellValue(helper.c
4、reateRichTextString(RichString+i+j);else if(j=3) MM-yyyy-ddcell.setCellValue(new Date();else if(j=24) /设置公式cell.setCellFormula(SUM(E+(i+1)+:X)else cellStyle = SummaryHSSF.setFillBackgroundColors(cellStyle,IndexedColors.ORANGE.getIndex(),IndexedColors.ORANGE.getIndex(),CellStyle.SOLID_FOREGROUND);cel
5、l.setCellValue(1); /输出OutputStream os = new FileOutputStream(new File(c:/SummaryHSSF.xls);wb.write(os);os.close();/* * 边框* param wb * return */ public static CellStyle createStyleCell(Workbook wb) CellStyle cellStyle = wb.createCellStyle();/设置一个单元格边框颜色cellStyle.setBorderBottom(CellStyle.BORDER_THIN)
6、;cellStyle.setBorderTop(CellStyle.BORDER_THIN);cellStyle.setBorderLeft(CellStyle.BORDER_THIN);cellStyle.setBorderRight(CellStyle.BORDER_THIN);cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex();cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex();cellStyle.setBottomBorderColor(Indexe
7、dColors.BLACK.getIndex();cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex();return cellStyle;* 设置文字在单元格里面的位置* CellStyle.ALIGN_CENTER * CellStyle.VERTICAL_CENTER * param cellStyle * param halign * param valign public static CellStyle setCellStyleAlignment(CellStyle cellStyle,short halign,shor
8、t valign) /设置上下cellStyle.setAlignment(CellStyle.ALIGN_CENTER);/设置左右cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);* 格式化单元格* 如#,#0.00,m/d/yy去HSSFDataFormat或XSSFDataFormat里面找* param fmt public static CellStyle setCellFormat(CreationHelper helper,CellStyle cellStyle,String fmt) /还可以用其它方法创建fo
9、rmat cellStyle.setDataFormat(helper.createDataFormat().getFormat(fmt);* 前景和背景填充的着色* param bg IndexedColors.ORANGE.getIndex();* param fg IndexedColors.ORANGE.getIndex();* param fp CellStyle.SOLID_FOREGROUND public static CellStyle setFillBackgroundColors(CellStyle cellStyle,short bg,short fg,short fp
10、) /cellStyle.setFillBackgroundColor(bg);cellStyle.setFillForegroundColor(fg);cellStyle.setFillPattern(fp);* 设置字体public static Font createFonts(Workbook wb) /创建Font对象Font font = wb.createFont();/设置字体font.setFontName(黑体/着色font.setColor(HSSFColor.BLUE.index);/斜体font.setItalic(true);/字体大小font.setFontHei
11、ght(short)300);return font;public class SummaryHSSF public static void main(String args) throws IOException /创建Sheet并给名字(表示Excel的一个Sheet)i=i+2)if(j=0)else if(j=1)/设置这个样式的格式(Format)else if(j=2)else if(j=3)else if(j=24)/* param wb* return*/public static CellStyle createStyleCell(Workbook wb)* CellStyl
12、e.ALIGN_CENTER* CellStyle.VERTICAL_CENTER* param cellStyle* param halign* param valignpublic static CellStyle setCellStyleAlignment(CellStyle cellStyle,short halign,short valign)* param fmtpublic static CellStyle setCellFormat(CreationHelper helper,CellStyle cellStyle,String fmt)/还可以用其它方法创建format* param fp CellStyle.SOLID_FOREGROUNDpublic static CellStyle setFillBackgroundColors(CellStyle cellStyle,short bg,short fg,short fp)public static Font createFonts(Workbook wb)读取Excel文件public class ReadExcel public static void main(String args) throws Exce
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2