快速生成Excel表并保存到桌面

使用的是poi 最新的版本

<dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.0</version>
        </dependency>
       
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.0</version>
        </dependency>

下面是我封装的一个工具类

/**
 * ssbfenqi 快速生成Excel表并保存到桌面
 * Date:2019/7/20
 * Timme:14:06
 * author: libanglei 李帮磊
 */
/**
 * 快速生成Excel表并保存到桌面
 *
 */
public class ExcelUtil {
    public static void main(String[] args) throws IOException {
        //设置表头
        String[] headContent = new String[]{"序号", "姓名", "性别", "年龄", "电话"};
        //二维数组
        String[][] particularDetails = new String[][]{
                {"张三", "男", "25", "185****3454"},
                {"李四", "男", "25", "185****3454"},
                {"貂蝉", "女", "18", "185****3454"},
                {"西施", "女", "18", "185****3454"},
        };
        areateHead(5, headContent,4,particularDetails,"测试生成表张三");

    }

    /**
     *快速生成Excel表并保存到桌面
     * @param line_br  列数
     * @param headContent 表头内容
     * @param sum 行数
     * @param particularDetails 二维数组装上表中详细内容
     * @param name 生成文件的名字
     * @throws IOException
     */
    private static void areateHead(Integer line_br, String[] headContent,Integer sum,String[][] particularDetails,String name) throws IOException {
        //创建一个工作薄
        HSSFWorkbook wk = new HSSFWorkbook();
        //创建Excel工作表对象
        //创建一个sheet页row.createCell(0).setCellValue("编号");
        Sheet sh = wk.createSheet("第一页");
        //创建第一行
        Row row = sh.createRow(0);

        //为每一列分配宽度 line_br 多少列 i
        for (int i = 0; i < line_br; i++) {
            sh.setColumnWidth(line_br, 256 * 10 + 184);
        }

        //为第一行的每一个单元格进行赋值 多少列 i
        for (int i = 0; i < line_br; i++) {
            //数组内容
            String content = headContent[i];
            //i 代表的列数
            row.createCell(i).setCellValue(content);
        }
        //创建第二行到第sum行 并遍历每一行添加详细细节
        for (int i = 1; i <=sum; i++) {
            Row row1 = sh.createRow(i);
            row1.createCell(0).setCellValue(i);
            for (int j = 1; j <= particularDetails.length; j++) {
                //数组内容 这里应该用二维数组
                String Details = particularDetails[i-1][j-1];
                //j 代表的列数
                row1.createCell(j).setCellValue(Details);

            }
        }
        //创建Random类对象
        Random random = new Random();
        //产生随机数
        int number = random.nextInt(999999 - 100000 + 1);
        //当前用户桌面路径
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File com=fsv.getHomeDirectory(); //这便是读取桌面路径的方法了
        String path = com.getPath();
        //转换路径格式
        String replace = path.replace("\", "\\");
        System.out.println(replace);

        //生成excel表 导出到桌面
        FileOutputStream fs = new FileOutputStream(replace+"\" + number +name+ ".xlsx");

        wk.write(fs);
    }

}
积少成多, 积沙成塔.
原文地址:https://www.cnblogs.com/lei0913/p/11949695.html