栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

批量展示和修改指定目录下的Excel文件的worksheet名称

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

批量展示和修改指定目录下的Excel文件的worksheet名称

自动把指定目录下的所有Excel文件的worksheet名展示在0000.xml中,并可以批量修改指定的sheet名

项目是springboot框架,需要maven打包成jar

https://gitee.com/luemeng233_admin/workbook.git

package com.example.workbook.controller;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

@RestController
public class ChShName {

    @RequestMapping("/read")
    public String read(String address) throws IOException {
        File flie = new File(address);
        String[] fileNameLists = flie.list();
        Workbook workbook0 = new HSSFWorkbook();
        Sheet sheet = workbook0.createSheet();
        Workbook workbook;
        int sum = 0;
        String s;
        FileInputStream fileInputStream;

        for (int i = 0; i < fileNameLists.length; i++) {
            s = fileNameLists[i];
            String suffix = s.substring(s.indexOf(".") + 1);
            switch (suffix) {
                case "xls": {
                    fileInputStream = new FileInputStream(address + "\" + s);
                    workbook = new HSSFWorkbook(fileInputStream);
                    fileInputStream.close();
                    int j;
                    for (j = 0; j < workbook.getNumberOfSheets(); j++) {
                        Row row = sheet.createRow(sum + j);
                        row.createCell(0).setCellValue(fileNameLists[i]);
                        row.createCell(1).setCellValue(workbook.getSheetName(j));
                    }
                    sum += j + 1;
                    break;
                }

                case "xlsx": {
                    fileInputStream = new FileInputStream(address + "\" + s);
                    workbook = new XSSFWorkbook(fileInputStream);
                    fileInputStream.close();
                    int j;
                    for (j = 0; j < workbook.getNumberOfSheets(); j++) {
                        Row row = sheet.createRow(sum + j);
                        row.createCell(0).setCellValue(fileNameLists[i]);
                        row.createCell(1).setCellValue(workbook.getSheetName(j));
                    }
                    sum += j + 1;
                    break;
                }

                default:
                    break;
            }
        }
        FileOutputStream fileOutputStream = new FileOutputStream(address + "\0000.xls");
        workbook0.write(fileOutputStream);
        fileOutputStream.close();
        return "OK";
    }

    @RequestMapping("/change")
    public String change(String address) throws IOException {
        FileInputStream fileInputStream;
        fileInputStream = new FileInputStream(address + "\0000.xls");
        Workbook workbook0 = new HSSFWorkbook(fileInputStream);
        Sheet sheet = workbook0.getSheetAt(0);
        fileInputStream.close();
        Workbook workbook;
        int sum = 0;

        for (int i = 0; i < sheet.getLastRowNum(); i += sum) {
            Row row = sheet.getRow(i);
            String filename = row.getCell(0).getStringCellValue();
            String suffix = filename.substring(filename.indexOf(".") + 1);
            switch (suffix){
                case "xls":{
                    fileInputStream = new FileInputStream(address + "\" + filename);
                    workbook = new HSSFWorkbook(fileInputStream);
                    fileInputStream.close();
                    int j;
                    for (j = 0; j < workbook.getNumberOfSheets(); j++) {
                        Row row1 = sheet.getRow(i + j);
                        Cell cell = row1.getCell(1);
                        cell.setCellType(CellType.STRING);
                        String sheetname = cell.getStringCellValue();
                        workbook.setSheetName(j, sheetname);
                    }
                    FileOutputStream fileOutputStream = new FileOutputStream(address + "\" + filename);
                    workbook.write(fileOutputStream);
                    fileOutputStream.close();
                    sum = j + 1;
                    break;}

                case "xlsx":{
                    fileInputStream = new FileInputStream(address + "\" + filename);
                    workbook = new XSSFWorkbook(fileInputStream);
                    fileInputStream.close();
                    int j;
                    for (j = 0; j < workbook.getNumberOfSheets(); j++) {
                        Row row1 = sheet.getRow(i + j);
                        Cell cell = row1.getCell(1);
                        cell.setCellType(CellType.STRING);
                        String sheetname = cell.getStringCellValue();
                        workbook.setSheetName(j, sheetname);
                    }
                    FileOutputStream fileOutputStream = new FileOutputStream(address + "\" + filename);
                    workbook.write(fileOutputStream);
                    fileOutputStream.close();
                    sum = j + 1;
                    break;}

                default:
                    break;
            }
        }
        return "OK";
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/396835.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号