whycxzp
2022-07-28 8fe657f1d00f27307b51bc609554fe4a996a15ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.whyc.util;
 
import org.apache.poi.hssf.usermodel.*;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
 
/**
 * @Description: 表格工具类
 */
public class ExcelUtil {
 
    //excel导出
    public static void exportExcel(String fileName, String sheetName, String[] title, Object[][] values, HSSFWorkbook wb, HttpServletResponse response) {
        //第一步,创建一个webbook,即excel的文档对象
        if (wb == null) {
            wb = new HSSFWorkbook();
        }
        //第二步,在webbook中添加一个sheet,即excel的表单
        HSSFSheet sheet = wb.createSheet(sheetName);
        //设置列宽度
        //判断是否为空
        if (values != null && values.length > 0) {
            for (int i = 0; i < values[0].length; i++) {
                sheet.setColumnWidth(i, 256 * 15);
            }
        }
        //第三步,在sheet中添加表头第0行,即excel的行
        HSSFRow row = sheet.createRow(0);
        //第四步,创建单元格,并设置值表头,设置表头居中,即excel格子单元
        HSSFCellStyle style = wb.createCellStyle();
        //居中格式
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        //边框填充
        ///*style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
        //style.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
        //style.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
        //style.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框*/
        //背景颜色
        //style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//设置前景填充样式
        //style.setFillForegroundColor(HSSFColor.DARK_RED.index);//前景填充色
        HSSFCell cell;
        //创建标题
        for (int i = 0; i < title.length; i++) {
            cell = row.createCell(i);
            cell.setCellValue(title[i].toString());
            cell.setCellStyle(style);
        }
        // 创建内容
        for (int i = 0; i < values.length; i++) {
            row = sheet.createRow(i + 1);
            for (int j = 0; j < values[i].length; j++) {
                cell = row.createCell(j);
                if (values[i][j] != null) {
                    cell.setCellValue(values[i][j].toString());
                } else {
                    cell.setCellValue("");
                }
                cell.setCellStyle(style);
            }
        }
        //将文件存到指定位置
        OutputStream os = null;
        try {
            os = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            //response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes()) + ".xls");
            //不保存缓存信息与response.reset同样效果
            response.addHeader("Pragma", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            wb.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    //构造指定的单元格样式
    public static HSSFCellStyle createCellStyle(HSSFWorkbook wb,String fontType,int fontSize,boolean center,boolean blod){
 
        HSSFCellStyle cellStyle = wb.createCellStyle();                                                                        //单元格样式
        HSSFFont font = wb.createFont();
        font.setFontName(fontType);
        font.setFontHeightInPoints((short) fontSize);                                                                        //设置字体大小
        font.setBoldweight(blod?HSSFFont.BOLDWEIGHT_BOLD:HSSFFont.BOLDWEIGHT_NORMAL);                                        //粗体显示
        cellStyle.setFont(font);                                                                                               //标题样式
        cellStyle.setAlignment(center?HSSFCellStyle.ALIGN_CENTER_SELECTION:HSSFCellStyle.ALIGN_LEFT);                         // 居中
        return cellStyle;
    }
}