当前位置: 首页 > 工具软件 > EXCEL-UTIL4J > 使用案例 >

java数据导出Excel-Dome(修改字体,大小)

许涵容
2023-12-01

方法一:

依赖


        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.0.0</version>
        </dependency>

dome



import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.servlet.http.HttpServletResponse;

import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
/**
 *
 * @ClassName: ExcelUtils
 * @Description: excle工具类
 * @author 
 * @date
 *
 */
@Slf4j
public class ExcelUtils {
    /**
     *
     * @Title: importData
     * @Description: 导入excle 数据
     * @param file  文件
     * @param headerRows  忽略头行数
     * @param pojoClass   转换的实体
     * @return List<User>  返回的集合
     */
    public static <T> List<T> importData(MultipartFile file, Integer headerRows,
                                         Class<T> pojoClass){
        if (file == null) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     *
     * @Title: exportExcel
     * @Description: 导出excel
     * @param list  导出的数据
     * @param title  文件标题
     * @param sheetName  sheet名称
     * @param pojoClass  集合的类
     * @param fileName   文件名
     * @param response
     * @return void
     */
    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,HttpServletResponse response) {
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, sheetName), pojoClass, list);
        if (workbook != null) {
            try {
//                response.setContentType("application/download");
                response.setCharacterEncoding("UTF-8");
                response.setHeader("content-Type", "application/vnd.ms-excel");
                response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
                response.setHeader("Pragma", "no-cache");
                response.setHeader("Cache-Control", "no-cache");
                response.setDateHeader("Expires", 0);
                workbook.write(response.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



}
 List<UserInviteInfo> rst = channelService.searchInviteUser(search, new PageRequest(0, Integer.MAX_VALUE)).getContent();
        ExcelUtils.exportExcel( rst,null,"登录用户列表", UserInviteInfo.class,"登录用户列表.xls", response);
   

方法二(最简单):

依赖

dome:


import com.chenyou.admin.projection.wx.BaseEntity;
import lombok.Data;

@Data
public class ExcleDome extends BaseEntity {

      private  Integer id;
      private  String  name;
      private  String  gender;
}



import com.chenyou.admin.models.ExcleDome;
import org.apache.poi.hssf.usermodel.HSSFCell;
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.CellStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/excelDome")
public class ExcelDomeController {

    /**
     * 导出excel表格
     *  在excel表中,无论是行还是列都是从0开始
     * @param response
     * @return
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        /**
         *  实际工作中,要导出的数据都是从数据库中取出来的,
         *  肯定是根据前端传过来的某个查询条件去查出一个集合,然后导出
         */
        List<ExcleDome> sList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            // 创建一个类,用于模拟数据
            ExcleDome s = new ExcleDome();
            s.setName("666==="+i);
            s.setGender("男");
            s.setId(i);
            sList.add(s);
        }
        //创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook wb = new HSSFWorkbook();
        //建立新的sheet对象(excel的表单)
        HSSFSheet sheet = wb.createSheet("测试表");

        // 1、获取样式
        CellStyle cellStyle = wb.createCellStyle();
        //2、 给样式设置对齐方式(水平对齐)
        //cellStyle.setAlignment(HorizontalAlignment.CENTER);

        //在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个
        HSSFRow row1 = sheet.createRow(0);
        //创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个
        HSSFCell cell = row1.createCell(0);
        //设置单元格内容
        cell.setCellValue("学生中考考试信息表");
        //3.设置单元格样式居中
        cell.setCellStyle(cellStyle);
        //合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
        //在sheet里创建第二行
        HSSFRow row2 = sheet.createRow(1);
        //创建单元格并设置单元格内容
        row2.createCell(0).setCellValue("姓名");
        row2.createCell(1).setCellValue("班级");
        row2.createCell(2).setCellValue("地址");
        row2.createCell(3).setCellValue("手机号");
        // 判断要导出的数据是否为空,不为空遍历集合
        if(null!=sList){
            int j = 2;
            for (int i = 0; i <sList.size() ; i++) {
                //在sheet里创建第三行,按照表头,添加数据
                HSSFRow row3 = sheet.createRow(j);
                //创建第三行,第一个单元格、
                row3.createCell(0).setCellValue(sList.get(i).getName());
                //第二个单元格
                row3.createCell(1).setCellValue(sList.get(i).getGender());
                j++;
            }
        }
        //输出Excel文件
        OutputStream output = response.getOutputStream();
        //response.reset();
        response.setHeader("Content-disposition", "attachment; filename=details.xls");
        response.setContentType("application/msexcel");
        wb.write(output);
        output.close();
        //@TODO
    }
}

方法三:

使用 hutool导出

工具类:

 类似资料: