Springboot uses POI to operate excel

In order to conveniently use poi to operate excel, here, the class BubbleSheet is used to encapsulate the Sheet in Poi. The BubbleSheet class is as follows:

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;

import java.util.ArrayList;
import java.util.List;

public class BubbleSheet {

    private Sheet sheet;

    private Row nowRow;

    private Integer nowRowNo;

    private HSSFWorkbook wb;

    private List<HSSFCellStyle> cellStylesList = new ArrayList<HSSFCellStyle>();

    BubbleSheet(HSSFWorkbook wb) {
        this.wb = wb;
        this.sheet = wb.createSheet();
        createCellStyleList();
        nowRowNo = -1;
        setNowRowWithRowNo(0);
    }

    private HSSFCellStyle createCellStyle(int poiColorIndex, int r, int g, int b) {
        HSSFCellStyle cellStyle =  wb.createCellStyle();

        cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //bottom border
        cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); //左边框
        cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); //Top border
        cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); //右边框

        HSSFPalette palette = wb.getCustomPalette();
        palette.setColorAtIndex((short)poiColorIndex, (byte) r, (byte) g, (byte) b);
        cellStyle.setFillForegroundColor((short)poiColorIndex);
        cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        return cellStyle;
    }

    private void createCellStyleList() {
        cellStylesList.add(createCellStyle(HSSFColor.LIME.index, 255, 255, 255)); // default white
        cellStylesList.add(createCellStyle(HSSFColor.BLUE.index, 211, 209, 207)); // content light gray
        cellStylesList.add(createCellStyle(HSSFColor.GREEN.index, 186, 210, 170)); // the header is light green
    }

    public void mergeCells(Integer firstRow, Integer lastRow, Integer firstCol, Integer lastCol, String content, int colorIndex) {
        CellRangeAddress cra = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
        //Add merged cells to sheet
        sheet.addMergedRegion(cra);
        Cell cell_1 = nowRow.createCell(firstCol); // Select the first cell of the first row, because the position is at this position, the merged cell is set
        cell_1.setCellStyle(cellStylesList.get(colorIndex));
        // set the content of the cell
        cell_1.setCellValue(content);

        // Set the border of merged cells
        RegionUtil.setBorderBottom(HSSFCellStyle.BORDER_THIN, cra, sheet, wb); // 下边框
        RegionUtil.setBorderLeft(HSSFCellStyle.BORDER_THIN, cra, sheet, wb); // 左边框
        RegionUtil.setBorderRight(HSSFCellStyle.BORDER_THIN, cra, sheet, wb); // with border
        RegionUtil.setBorderTop(HSSFCellStyle.BORDER_THIN, cra, sheet, wb); // 上边框
    }

    public void setLineValues(Integer col, String[] titleNames, int colorIndex) {
        for(String stp: titleNames) {
            Cell ctp = nowRow.createCell(col ++);
            ctp.setCellValue(stp);
            ctp.setCellStyle(cellStylesList.get(colorIndex));
        }
    }

    public void setNowRowWithRowNo(Integer rowNo) {
        // The logic here feels weird, mainly the result of the interaction between getRow, createRow, and styling.
        if (rowNo == null) rowNo = 0;
        if(nowRowNo.equals(rowNo)) { // If the cell is created after the merged cell row, this row does not need to be regenerated.
            return ;
        }
        Row row = sheet.getRow(rowNo); // The reason for using this is: you need to set the outer border style of the merged cell, if you don't use this, you will get an error.
        if(row == null){
            row = sheet.createRow(rowNo);
        }
        nowRowNo = rowNo;
        setNowRow(row);
    }

    private void setNowRow(Row nowRow) {
        this.nowRow = nowRow;
    }

    public void createFreezePane(int startRowNo, int endRowNo, int startColNo, int endColNo) {
        sheet.createFreezePane( startRowNo, endRowNo, startColNo, endColNo);
    }
}

The next example of using BubbleSheet to easily manipulate the table is as follows:

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

public class bubbleSheet_ex {

    static final  private String[] titleNames = {
            "Ad Slot", "Monthly", "Project Name", "Reservation",
            "Estimated amount this month", "Direct customer company",
            "Region", "Corresponding Sales", "Medium", "Ratio of the current month"};

    public static int stepHigh = 0;

    public static void main(String[] args) {

        HSSFWorkbook wb =  new HSSFWorkbook();
        BubbleSheet bs = new BubbleSheet(wb);
        bs.setNowRowWithRowNo(0); // set which row to operate
        bs.mergeCells(0, 0, 0, 1, titleNames[0], 0); // set the first title of the first row
        final String[] titleNames_temp = Arrays.copyOfRange(titleNames, 1, titleNames.length);

        bs.setNowRowWithRowNo(0); // set which row to operate
        bs.setLineValues(2, titleNames_temp, 0); // set the title of the first line except the first

        stepHigh = 10;
        int firstRow_temp = 1;
        bs.setNowRowWithRowNo(firstRow_temp); // Set which row to operate
        bs.mergeCells(firstRow_temp, firstRow_temp + stepHigh, 0, 1, "模块1", 1);

        firstRow_temp = 13;
        bs.setNowRowWithRowNo(firstRow_temp); // Set which row to operate
        bs.mergeCells(firstRow_temp, firstRow_temp + stepHigh, 0, 1, "模块2", 2);

        saveexcel(wb, "e:/test_bs.xls");
    }

    private static void saveexcel(HSSFWorkbook wb, String filename) {
        // save document
        File file = new File(filename);
        try {
            file.createNewFile(); // create a file
        } catch (IOException e) {
            e.printStackTrace ();
        }
        try {
            FileOutputStream fos = new FileOutputStream(file);
            try {
                wb.write(fos);
                fos.close();
            } catch (IOException e) {
                e.printStackTrace ();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace ();
        }
    }
}

As can be seen from the example, the commonly used methods of BubbleSheet are mergeCells and setLineValues. From the name, we can see the purpose of the two. Before using these two methods, the setNowRowWithRowNo method must be used to indicate which line to operate. .

Tested in my work, BubbleSheet is reliable and very convenient when manipulating sheets.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325848671&siteId=291194637