搞懂easyExcel设置格式问题(列宽,单元格式,小数等) 清疚 2024-03-27 17:48 28阅读 0赞 1.设置列宽 Sheet接口的抽象方法: 第几列,设置宽度为多少 @param columnIndex 第几列(以0开始) @param width 宽度(0 < width <= 255\*256) void setColumnWidth(int columnIndex, int width); 2.表格的文本显示问题 显示数字格式: Row row = sheet.getRow(startRowNo); Cell cell = row.getCell(startColumnNo); if (data != null) { cell.setCellValue(data); } cell.setCellValue(data) setCellValue为重载方法,data类型为多种(String,double,Calendar,LocalDate,LocalDateTime,Date) 3.保留两位小数 private CellStyle getChangeDecimalStyle (XSSFWorkbook workbook, CellStyle source) { CellStyle cellStyle = workbook.createCellStyle(); cellStyle.cloneStyleFrom(source); cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00")); return cellStyle; } 这是我写的,你也可以从头开始定义CellStyle. public static CellStyle createReviewDataStyle(XSSFWorkbook workbook) { CellStyle cellStyle = workbook.createCellStyle(); XSSFFont font = workbook.createFont(); //字体格式 font.setFontName("Arial"); //字体大小 font.setFontHeightInPoints((short)8); //边框 cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); //水平对齐方式 cellStyle.setAlignment(HorizontalAlignment.CENTER); //数值对齐方式 cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); cellStyle.setFont(font); return cellStyle; }
还没有评论,来说两句吧...