Java POI導出excel 指定名稱分組折疊


package com.java.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * excel 導出
 *
 * @author Admin
 * @date 2020年5月20日
 */
@RestController
public class TestExcel {

    @GetMapping(value = "/export", produces = "application/json; charset=utf-8")
    public String createExcel(HttpServletResponse response) throws IOException {

        // 創建HSSFWorkbook對象(excel的文檔對象)
        Workbook wb = new HSSFWorkbook();
        // HSSFCellStyle style = this.getStyle(wb);
        // 建立新的sheet對象(excel的表單)
        Sheet sheet = wb.createSheet("成績表");

        // 在sheet里創建第一行,參數為行索引(excel的行),可以是0~65535之間的任何一個
       // HSSFRow row1 = sheet.createRow(0);
        Row row1= sheet.createRow(0);
        // 創建單元格(excel的單元格,參數為列索引,可以是0~255之間的任何一個
        Cell cell = row1.createCell(0);
        // 設置單元格內容
        cell.setCellValue("學員考試成績一覽表");
        // cell.setCellStyle(style);
        // 合並單元格CellRangeAddress構造參數依次表示起始行,截至行,起始列, 截至列
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
        // 在sheet里創建第二行
        //HSSFRow row2 = sheet.createRow(1);
        Row row2= sheet.createRow(1);
        // 創建單元格並設置單元格內容
        row2.createCell(0).setCellValue("姓名");
        row2.createCell(1).setCellValue("班級");
        row2.createCell(2).setCellValue("筆試成績");
        row2.createCell(3).setCellValue("機試成績");
        // row2.getCell(0).setCellStyle(style);
        // row2.getCell(1).setCellStyle(style);
        // row2.getCell(2).setCellStyle(style);
        // row2.getCell(3).setCellStyle(style);
        CellRangeAddress c = CellRangeAddress.valueOf("A2:D2");
        sheet.setAutoFilter(c);
        //c.formatAsString();
        List<StudentInfo> stuList = new ArrayList<StudentInfo>();
        StudentInfo stu1 = new StudentInfo();
        stu1.setStuName("狗老齊");
        stu1.setStuClass("5班");
        stu1.setBishiResult("28");
        stu1.setJishiResult("29");
        stuList.add(stu1);
        StudentInfo stu2 = new StudentInfo();
        stu2.setStuName("靚仔");
        stu2.setStuClass("5班");
        stu2.setBishiResult("82");
        stu2.setJishiResult("92");
        stuList.add(stu2);
        StudentInfo stu3 = new StudentInfo();
        stu3.setStuName("陽仔");
        stu3.setStuClass("8班");
        stu3.setBishiResult("82");
        stu3.setJishiResult("92");
        stuList.add(stu3);
        StudentInfo stu4 = new StudentInfo();
        stu4.setStuName("1仔");
        stu4.setStuClass("8班");
        stu4.setBishiResult("82");
        stu4.setJishiResult("92");
        stuList.add(stu4);
        StudentInfo stu5 = new StudentInfo();
        stu5.setStuName("2仔");
        stu5.setStuClass("8班");
        stu5.setBishiResult("82");
        stu5.setJishiResult("92");
        stuList.add(stu5);
        StudentInfo stu6 = new StudentInfo();
        stu6.setStuName("3仔");
        stu6.setStuClass("8班");
        stu6.setBishiResult("82");
        stu6.setJishiResult("92");
        stuList.add(stu6);
        int len=0;
        for (int i = 0; i < stuList.size(); i++) {
            StudentInfo stu = stuList.get(i);
            Row row3 = sheet.createRow(2 + i);
            row3.createCell(0).setCellValue(stu.getStuName());
            row3.createCell(1).setCellValue(stu.getStuClass());
            row3.createCell(2).setCellValue(stu.getBishiResult());
            row3.createCell(3).setCellValue(stu.getJishiResult());
            // row3.getCell(0).setCellStyle(style);
            // row3.getCell(1).setCellStyle(style);
            // row3.getCell(2).setCellStyle(style);
            // row3.getCell(3).setCellStyle(style);
            String stringCellValue = row3.getCell(0).getStringCellValue();
            if("陽仔".equals(stringCellValue)){
                len=i+2;
            }

        }
        sheet.groupRow(2,len-1);
        sheet.groupRow(len+1,stuList.size());

        //sheet.getCellRange("A4:A5").groupByRows(true);

        // 輸出Excel文件
        OutputStream output = response.getOutputStream();
        response.reset();
        response.setHeader("Content-disposition", "attachment; filename=test.xls");
        response.setContentType("application/msexcel");
        wb.write(output);
        output.close();
        return null;
    }

   

}

 

轉載於:https://blog.csdn.net/weixin_43840872/article/details/108280680

 


免責聲明!

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



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