HuTool工具包实现Excel文件的上传、下载以及修改文件名称

加载依赖文件

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>

下载文件

    /**
     * 下载文件
     * @throws Exception
     */
    public static void downFile() throws Exception{
        List<User> list = new ArrayList<>();
        list.add(new User("雨院长","yupeng@163.com","测试","地址"));
        list.add(new User("张博士","boshi@163.com","测试","地址"));
        list.add(new User("李教授","jiaoshou@163.com","测试","地址"));
        list.add(new User("徐校长","xiaozhang@163.com","测试","地址"));
        list.add(new User("杨老湿","laoshi@163.com","测试","地址"));

        String url = "/Users/xxx/Desktop/文件管理/";
        File ula = new File(url+"/abc.xlsx");
        if (!ula.exists()){
            ula.createNewFile();
        }
        System.out.println(ula.getPath());
        ExcelWriter writer = ExcelUtil.getWriter();
        writer.renameSheet("急急鹏的故事");  //设置sheet的名称
        writer.addHeaderAlias("name","姓名");
        writer.addHeaderAlias("email","邮箱");
        writer.addHeaderAlias("part","部分");
        writer.addHeaderAlias("ads","地址");
        writer.merge(3,"测试信息");
        CellStyle headCellStyle = writer.getStyleSet().getHeadCellStyle(); // 设置标题样式
        setSheetStyle(headCellStyle,writer);
        writer.write(list,true);
        OutputStream out = new FileOutputStream(ula.getPath());
        writer.flush(out,true);
        writer.close();
        out.close();
        System.out.println("执行结束!");
    }

// 设置表格样式
    /**
     * 添加表格样式
     * @param headCellStyle
     * @param writer
     */
    public static void setSheetStyle(CellStyle headCellStyle,ExcelWriter writer){
        //writer.merge(0, 0, 0, 1,"中心文档", true);  // 设置合并单元格
        headCellStyle.setAlignment(HorizontalAlignment.CENTER);  //水平居中
        headCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);   //垂直居中
        Font font = writer.createFont();  //设置内容字体
        font.setBold(true);  //加粗
        font.setColor((short) 6);  // 颜色设置
        font.setFontHeightInPoints((short)12);  //设置标题字体大小
        headCellStyle.setFont(font);
    }    

// 内部类
class User{
    private String name;
    private String email;
    private String part;
    private String ads;

    public User(String name,String email,String part,String ads){
        this.name = name;
        this.email = email;
        this.part = part;
        this.ads = ads;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPart() {
        return part;
    }

    public void setPart(String part) {
        this.part = part;
    }

    public String getAds() { return ads; }

    public void setAds(String ads) {
        this.ads = ads;
    }
}

上传文件

    /**
     * 上传文件
     * @throws Exception
     */
    public static void updateFile() throws Exception{
        String url = "/Users/xxx/Desktop/文件管理/";
        File ula = new File(url+"/abc.xlsx");
        InputStream fis = new FileInputStream(ula);
        ExcelReader reader = ExcelUtil.getReader(fis);

        // huTool读取excel 1:表示表格头所在行,2:从第几行开始读取,2147483647:行的最大值
        // 因为自定义了表格头别名,所以只能使用map接收,如果没有设置别名,可以使用实体接收
        List<Map<String,Object>> readAll = reader.read(1,2,2147483647);
        // 打印读取的文件信息
        System.out.println(readAll);
        // 数据处理,略...
    }

修改文件名称

    /**
     * 修改文件的名称
     */
    public static void updateFileName() throws Exception{
        String url = "/Users/xxx/Desktop/文件管理/我的文档";
        File file = new File(url);
        File[] files = file.listFiles();
        List<File> filesList = Arrays.asList(files);
        for (File fileNode: filesList) {
            // String fileNameTest = "这是一个视频文件[这是广告信息].mp4";
            String name = fileNode.getName();
            int index = name.indexOf("[");
            if (-1 != index){
                // 截取 "[" 之前的文件名称
                String substring = name.substring(0, index);
                name = substring+".mp4";
                String newName = url + File.separator + name;
                File newFile = new File(newName);
                // 重命名
                fileNode.renameTo(newFile);
            }
        }
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您可以使用Hutool的ZipUtil类实现批量压缩Excel文件的功能,具体实现步骤如下: 1.使用SpringBoot的文件IO相关的工具将要压缩的Excel文件读取出来,并且将其放入一个List集合中。 2.遍历这个List集合,将每个Excel文件都压缩成一个zip文件,可以使用ZipOutputStream及其相关的方法进行实现,将压缩后的zip文件保存到指定路径中。 3.最后将所有的压缩后的zip文件打包成一个总的zip文件,并返回该文件的路径。 示例代码如下: ``` // 定义需要压缩的Excel文件路径 String[] excelFilePaths = {"path/to/excel1.xlsx", "path/to/excel2.xlsx", "path/to/excel3.xlsx"}; // 将Excel文件读取到List集合中 List<File> excelFiles = new ArrayList<>(); for (String excelFilePath : excelFilePaths) { excelFiles.add(new File(excelFilePath)); } // 定义压缩后的zip文件保存路径 String zipPath = "path/to/zip.zip"; // 遍历Excel文件集合,将每个文件压缩成zip文件并保存到指定路径 for (File excelFile : excelFiles) { try { // 构建zip文件输出流 ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipPath)); // 创建压缩文件的输出流 ZipEntry zipEntry = new ZipEntry(excelFile.getName().replace(".xlsx", ".zip")); zipOut.putNextEntry(zipEntry); // 读取Excel文件并进行压缩 BufferedInputStream excelIn = new BufferedInputStream(new FileInputStream(excelFile)); byte[] buffer = new byte[1024]; int len = 0; while ((len = excelIn.read(buffer)) > 0) { zipOut.write(buffer, 0, len); } // 关闭输入输出流 excelIn.close(); zipOut.flush(); zipOut.closeEntry(); } catch (Exception e) { e.printStackTrace(); } } // 将所有压缩后的zip文件打包成一个总的zip文件 ZipUtil.zip(zipPath, "path/to/total.zip"); // 返回总的zip文件路径 return "path/to/total.zip"; ``` 希望能够解决您的问题,如果有任何疑问,请随时询问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值