POI中可能会用到一些需要设置EXCEL单元格格式的操作小结:先获取工作薄对象:
HSSFWorkbook wb = new HSSFWorkbook()
HSSFSheet sheet = wb.createSheet()
HSSFCellStyle setBorder = wb.createCellStyle()
一、设置背景色:setBorder.setFillForegroundColor((short) 13)
setBorder.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND)
二、设置边框:setBorder.setBorderBottom(HSSFCellStyle.BORDER_THIN)
setBorder.setBorderLeft(HSSFCellStyle.BORDER_THIN)
setBorder.setBorderTop(HSSFCellStyle.BORDER_THIN)
setBorder.setBorderRight(HSSFCellStyle.BORDER_THIN)
三、设置居中:setBorder.setAlignment(HSSFCellStyle.ALIGN_CENTER)
四、设置字体:
HSSFFont font = wb.createFont()
font.setFontName("黑体")
font.setFontHeightInPoints((short) 16)
HSSFFont font2 = wb.createFont()
font2.setFontName("仿宋_GB2312")
font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD)
font2.setFontHeightInPoints((short) 12)
setBorder.setFont(font)
五、设置列宽:sheet.setColumnWidth(0, 3766)
六、设置自动换行:setBorder.setWrapText(true)
七、合并单元格:Region region1 = new Region(0, (short) 0, 0, (short) 6)
sheet.addMergedRegion(region1)
八、加边框 HSSFCellStyle cellStyle= wookBook.createCellStyle()
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER)
cellStyle.setBorderBottom(HSSFCellStyle.BorderBORDER_MEDIUM)
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index)
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM)
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index)
cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM)
cellStyle.setRightBorderColor(HSSFColor.BLACK.index)
cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM)
cellStyle.setTopBorderColor(HSSFColor.BLACK.index)
例子:
@ResponseBody@RequestMapping(value = "/reportForms/joinStocktaking/exportStorage.api")
public AjaxResponse exportStorage(@RequestBody StorageModel model) throws Exception
{
if (logger.isDebugEnabled())
logger.debug("tmpdir is, {}", System.getProperty(JAVA_IO_TMPDIR))
int row = 1
HSSFWorkbook workbook = new HSSFWorkbook()
HSSFSheet hssfSheet = workbook.createSheet()
HSSFCellStyle style = workbook.createCellStyle()
style.setFillBackgroundColor(HSSFCellStyle.LEAST_DOTS)
style.setFillPattern(HSSFCellStyle.LEAST_DOTS)
style.setAlignment(HSSFCellStyle.ALIGN_CENTER)
style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM)
style.setBottomBorderColor(HSSFColor.BLACK.index)
style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM)
style.setLeftBorderColor(HSSFColor.BLACK.index)
style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM)
style.setRightBorderColor(HSSFColor.BLACK.index)
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM)
style.setTopBorderColor(HSSFColor.BLACK.index)
HSSFFont font = workbook.createFont()
font.setFontHeightInPoints((short) 14)
font.setFontName(" 黑体 ")
style.setFont(font)
HSSFCell firstCell = firstRow.createCell(0)
firstRow.setHeight((short) 400)
style.setFillBackgroundColor(HSSFColor.GREEN.index)
firstCell.setCellValue(new HSSFRichTextString("库房"))
firstCell.setCellStyle(style)
HSSFCell secondCell = firstRow.createCell(1)
style.setFillForegroundColor(HSSFColor.GREEN.index)
style.setFillBackgroundColor(HSSFColor.GREEN.index)
secondCell.setCellValue(new HSSFRichTextString("库区"))
secondCell.setCellStyle(style)
firstRow.setHeight((short) 400)
style.setFillForegroundColor(HSSFColor.GREEN.index)
style.setFillBackgroundColor(HSSFColor.GREEN.index)
threeCell.setCellValue(new HSSFRichTextString("物料编号"))
threeCell.setCellStyle(style)
HSSFCell fourCell = firstRow.createCell(3)
firstRow.setHeight((short) 400)
style.setFillForegroundColor(HSSFColor.GREEN.index)
style.setFillBackgroundColor(HSSFColor.GREEN.index)
fourCell.setCellValue(new HSSFRichTextString("物料名称"))
fourCell.setCellStyle(style)
style.setFillForegroundColor(HSSFColor.GREEN.index)
style.setFillBackgroundColor(HSSFColor.GREEN.index)
fiveCell.setCellValue(new HSSFRichTextString("在库数量"))
fiveCell.setCellStyle(style)
style.setFillForegroundColor(HSSFColor.GREEN.index)
style.setFillBackgroundColor(HSSFColor.GREEN.index)
sixCell.setCellValue(new HSSFRichTextString("锁定数量"))
sixCell.setCellStyle(style)
hssfSheet.setColumnWidth(0, 7000)
hssfSheet.setColumnWidth(1, 8000)
hssfSheet.setColumnWidth(2, 4000)
hssfSheet.setColumnWidth(3, 6000)
hssfSheet.setColumnWidth(4, 4000)
hssfSheet.setColumnWidth(5, 4000)
List list = joinStocktackingService.findjoinStorageByTerm(model.getWareHouse(), model.getStockArea(), model.getMaterialCode(), model.getMaterialName())
for (Object object : list) {
Object[] objects = (Object[]) object
Storage storage = (Storage) objects[0]
Warehouse warehouse = (Warehouse) objects[1]
StockArea stockArea = (StockArea) objects[2]
Material material = (Material) objects[3]
HSSFCellStyle cellStyle = workbook.createCellStyle()
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER)
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM)
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index)
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index)
cellStyle.setBorderRight(HSSFCellStyle.BORDER_MEDIUM)
cellStyle.setRightBorderColor(HSSFColor.BLACK.index)
cellStyle.setBorderTop(HSSFCellStyle.BORDER_MEDIUM)
cellStyle.setTopBorderColor(HSSFColor.BLACK.index)
HSSFRow hssfRow = hssfSheet.createRow((short) row)
HSSFCell firstHssfCell = hssfRow.createCell(0)
firstHssfCell.setCellValue(new HSSFRichTextString(warehouse.getName()))
firstHssfCell.setCellStyle(cellStyle)
HSSFCell secondHssfCell = hssfRow.createCell(1)
secondHssfCell.setCellValue(new HSSFRichTextString(stockArea.getName()))
secondHssfCell.setCellStyle(cellStyle)
HSSFCell threeHssfCell = hssfRow.createCell(2)
threeHssfCell.setCellValue(new HSSFRichTextString(material.getCode()))
HSSFCell fourHssfCell = hssfRow.createCell(3)
fourHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING)
fourHssfCell.setCellValue(new HSSFRichTextString(material.getName()))
fourHssfCell.setCellStyle(cellStyle)
fiveHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING)
fiveHssfCell.setCellValue(new HSSFRichTextString(String.valueOf(storage.getQty())))
fiveHssfCell.setCellStyle(cellStyle)
sixHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING)
sixHssfCell.setCellValue(new HSSFRichTextString(String.valueOf(storage.getQtyLocked())))
sixHssfCell.setCellStyle(cellStyle)
}
String newFileName = String.format("%s.%s", "joinStocktaking-" + (new Date()).getTime(), "xls")
String uploadPath = FileUtils.contractPath(System.getProperty(JAVA_IO_TMPDIR), newFileName)
FileOutputStream fOut = new FileOutputStream(uploadPath)
workbook.write(fOut)
fOut.close()
return AjaxResponse.createSuccess(newFileName)
}