poi導出excel設置樣式


由於要利用poi導出excel(XSSFWorkbook),而且要添加樣式,搜索其他的結果無非都是顏色值,經查詢的結果,做一下總結:

1、設置背景色,要用  style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

使用 style.setFillBackgroundColor(bg);方法總是出現一個黑塊,所以改為上面的寫法,結果正確

顏色的問題,可以在 IndexedColors 中查到,是個枚舉

2、有合並單元格的情況下,給一行直接設置背景顏色,並沒有起到效果,所以在 createCell 時,包裝了一個方法

 1 /**
 2      * 創建cell 此方法目的,是要給所有的cell加上邊框和背景色
 3      * @param workbook 工作薄
 4      * @param row 行
 5      * @param index 下標
 6      * @return
 7      */
 8 public XSSFCell createCell(XSSFWorkbook workbook,XSSFRow row,int index,String ... args) {
 9         CellStyle style=SheetStyle.getDefaultCellStyle(workbook);
10         XSSFCell cell = row.createCell(index);
11         
12         //大於3,因為前邊幾行不需要樣式
13         if(row.getRowNum()>3) {
14             //偶數行為Lime,奇數行為Yellow
15             if(this.staffIndex%2==0) {
16                 SheetStyle.setCellStyleLime(style);
17             }else {
18                 SheetStyle.setCellStyleYellow(style);
19             }
20         }
21         row.setRowStyle(style);
22         cell.setCellStyle(style);
23         return cell;
24     }

把樣式同時賦值給RowStyle和CellStyle,導出的效果理想

3、需要有合並單元格的情況,必須先創建合並單元格,然后在填值

CellRangeAddress callRangeAddress5 = new CellRangeAddress(start,end,0,0);//起始行,結束行,起始列,結束列
sheet.addMergedRegion(callRangeAddress5);

4、spring mvc導出excel文件,Controller部分的代碼:

主要用來設置導出的文件名

/**
     * excel排班
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @SuppressWarnings("resource")
    @RequestMapping(value = "exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String,Object> result=schedulingService.downloadScheduling(request.getParameter("x"));
        String name="導出結果"+result.get("month");
        byte [] bytes=(byte[]) result.get("data");
        response.setContentType("application/binary;charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(name+".xlsx", "UTF-8"));
        response.setContentLength(bytes.length);
        response.getOutputStream().write(bytes);
        response.getOutputStream().flush();
        response.getOutputStream().close();
    }

 

將SheetStyle完整代碼貼出,有需要的可以拿來復制

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * 設置excel的樣式類
 *
 */
public class SheetStyle {

    public static void setColumnWidth() {
        
    }
    /**
     * 水平居中、垂直居中
     * 字體:宋體
     * 字體大小:16號
     * 加粗
     * @param workbook
     * @return
     */
    public static CellStyle getStyle(XSSFWorkbook workbook) {
        CellStyle cellstyle=workbook.createCellStyle();
        cellstyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
        cellstyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        Font font=workbook.createFont();//字體
        font.setFontName("宋體");//字體
        font.setFontHeightInPoints((short)16);//字號
        font.setBold(true);//加粗
        cellstyle.setFont(font);
        setBorderStyle(cellstyle);
        return cellstyle;
    }
    
    /**
     * 獲取默認的cell表格樣式,加邊框,水平居中,垂直居中
     * @param workbook
     * @return
     */
    public static CellStyle getDefaultCellStyle(XSSFWorkbook workbook) {
        CellStyle style=workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);//水平居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        setBorderStyle(style);
        return style;
    }
    
    /**
     * 邊框樣式
     * @param style
     */
    public static void setBorderStyle(CellStyle style) {
        style.setBorderBottom(BorderStyle.THIN); //下邊框
        style.setBorderLeft(BorderStyle.THIN);//左邊框
        style.setBorderTop(BorderStyle.THIN);//上邊框
        style.setBorderRight(BorderStyle.THIN);//右邊框
    }
    
    /**
     * 奇數行
     * 背景顏色為黃色
     * @param style
     */
    public static void setCellStyleYellow(CellStyle style) {
        style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    }
    /**
     * 偶數行
     * 背景顏色為LIME 
     * @param style
     */
    public static void setCellStyleLime(CellStyle style) {
        style.setFillForegroundColor(IndexedColors.LIME.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    }
    /**
     * 字體設置紅色
     * @param workbook
     * @param style
     */
    public static void setFontRedColor(XSSFWorkbook workbook,CellStyle style) {
        Font font=workbook.createFont();//字體
        font.setColor(IndexedColors.RED.getIndex());
        style.setFont(font);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM