whycxzp
2023-06-25 19e5ccb1aee7c0e0d308386e03cad72bf4997d0d
src/main/java/com/whyc/util/ExcelUtil.java
@@ -1,10 +1,12 @@
package com.whyc.util;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
/**
 * @Description: 表格工具类
@@ -19,6 +21,8 @@
        }
        //第二步,在webbook中添加一个sheet,即excel的表单
        HSSFSheet sheet = wb.createSheet(sheetName);
        CellRangeAddress rangeAddress = CellRangeAddress.valueOf("A1:J1");
        sheet.setAutoFilter(rangeAddress);
        //设置列宽度
        //判断是否为空
        if (values != null && values.length > 0) {
@@ -28,6 +32,13 @@
        }
        //第三步,在sheet中添加表头第0行,即excel的行
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle titleCellStyle = wb.createCellStyle();
        titleCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont font = wb.createFont();
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        titleCellStyle.setFont(font);
        row.setRowStyle(titleCellStyle);
        //第四步,创建单元格,并设置值表头,设置表头居中,即excel格子单元
        HSSFCellStyle style = wb.createCellStyle();
        //居中格式
@@ -45,7 +56,7 @@
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i].toString());
            cell.setCellStyle(style);
            cell.setCellStyle(titleCellStyle);
        }
        // 创建内容
        for (int i = 0; i < values.length; i++) {
@@ -68,8 +79,9 @@
            e.printStackTrace();
        }
        try {
            //response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes()) + ".xls");
            //response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1) + ".xls");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode (fileName+ ".xls", "utf-8"));
            response.setCharacterEncoding("UTF-8");
            //不保存缓存信息与response.reset同样效果
            response.addHeader("Pragma", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
@@ -82,13 +94,13 @@
    }
    //构造指定的单元格样式
    public static HSSFCellStyle createCellStyle(HSSFWorkbook wb,String fontType,int fontSize,boolean center,boolean blod){
    public static HSSFCellStyle createCellStyle(HSSFWorkbook wb,String fontType,int fontSize,boolean center,boolean bold){
        HSSFCellStyle cellStyle = wb.createCellStyle();                                                      //单元格样式
        HSSFFont font = wb.createFont();
        font.setFontName(fontType);
        font.setFontHeightInPoints((short) fontSize);                                                      //设置字体大小
        font.setBoldweight(blod?HSSFFont.BOLDWEIGHT_BOLD:HSSFFont.BOLDWEIGHT_NORMAL);                              //粗体显示
        font.setBoldweight(bold?HSSFFont.BOLDWEIGHT_BOLD:HSSFFont.BOLDWEIGHT_NORMAL);                              //粗体显示
        cellStyle.setFont(font);                                                                        //标题样式
        cellStyle.setAlignment(center?HSSFCellStyle.ALIGN_CENTER_SELECTION:HSSFCellStyle.ALIGN_LEFT);                   // 居中
        return cellStyle;