日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java poi excel 单元格样式_java poi批量导出excel 设置单元格样式

發(fā)布時間:2023/12/31 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java poi excel 单元格样式_java poi批量导出excel 设置单元格样式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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); //第一個參數代表列id(從0開始),第2個參數代表寬度值

//或者這個方式也可

sheet.setColumnWidth(0, 30 * 256);

六、設置自動換行:

setBorder.setWrapText(true);//設置自動換行

七、合并單元格:

Region region1 = new Region(0, (short) 0, 0, (short) 6);

//參數1:行號 參數2:起始列號 參數3:行號 參數4:終止列號

sheet.addMergedRegion(region1);

//或者這種方式(親測好使)

//合并addMergedRegion方法四個屬性分別是(開始行,結束行,開始列,結束列)

sheet.addMergedRegion(new CellRangeAddress(firstrow, lastrow, firstcol, lastcol));

八、加邊框

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);

//如果設置行高

highRow.setHeight((short) (19*20));

例子:

@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);

//設置Excel中的邊框(表頭的邊框)

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);

HSSFRow firstRow = hssfSheet.createRow((short) 0);

HSSFCell firstCell = firstRow.createCell(0);

firstRow.setHeight((short) 400);

//設置Excel中的背景

style.setFillForegroundColor(HSSFColor.GREEN.index);

style.setFillBackgroundColor(HSSFColor.GREEN.index);

firstCell.setCellValue(new HSSFRichTextString("庫房"));

firstCell.setCellStyle(style);

HSSFCell secondCell = firstRow.createCell(1);

firstRow.setHeight((short) 400);

style.setFillForegroundColor(HSSFColor.GREEN.index);

style.setFillBackgroundColor(HSSFColor.GREEN.index);

secondCell.setCellValue(new HSSFRichTextString("庫區(qū)"));

secondCell.setCellStyle(style);

HSSFCell threeCell = firstRow.createCell(2);

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);

HSSFCell fiveCell = firstRow.createCell(4);

firstRow.setHeight((short) 400);

style.setFillForegroundColor(HSSFColor.GREEN.index);

style.setFillBackgroundColor(HSSFColor.GREEN.index);

fiveCell.setCellValue(new HSSFRichTextString("在庫數量"));

fiveCell.setCellStyle(style);

HSSFCell sixCell = firstRow.createCell(5);

firstRow.setHeight((short) 400);

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];

//設置Excel中的邊框

HSSFCellStyle cellStyle = workbook.createCellStyle();

cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

cellStyle.setBorderBottom(HSSFCellStyle.BORDER_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);

HSSFRow hssfRow = hssfSheet.createRow((short) row);

HSSFCell firstHssfCell = hssfRow.createCell(0);//庫房

firstHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING);

firstHssfCell.setCellValue(new HSSFRichTextString(warehouse.getName()));

firstHssfCell.setCellStyle(cellStyle);//設置單元格的樣式

HSSFCell secondHssfCell = hssfRow.createCell(1);

secondHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING);

secondHssfCell.setCellValue(new HSSFRichTextString(stockArea.getName()));

secondHssfCell.setCellStyle(cellStyle);//設置單元格的樣式

HSSFCell threeHssfCell = hssfRow.createCell(2);

threeHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING);

threeHssfCell.setCellValue(new HSSFRichTextString(material.getCode()));

threeHssfCell.setCellStyle(cellStyle);//設置單元格的樣式

HSSFCell fourHssfCell = hssfRow.createCell(3);

fourHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING);

fourHssfCell.setCellValue(new HSSFRichTextString(material.getName()));

fourHssfCell.setCellStyle(cellStyle);//設置單元格的樣式

HSSFCell fiveHssfCell = hssfRow.createCell(4);

fiveHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING);

fiveHssfCell.setCellValue(new HSSFRichTextString(String.valueOf(storage.getQty())));

fiveHssfCell.setCellStyle(cellStyle);//設置單元格的樣式

HSSFCell sixHssfCell = hssfRow.createCell(5);

sixHssfCell.setCellType(HSSFCell.CELL_TYPE_STRING);

sixHssfCell.setCellValue(new HSSFRichTextString(String.valueOf(storage.getQtyLocked())));

sixHssfCell.setCellStyle(cellStyle);//設置單元格的樣式

row++;

}

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.flush();

fOut.close();

return AjaxResponse.createSuccess(newFileName);

}

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的java poi excel 单元格样式_java poi批量导出excel 设置单元格样式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。