首页 > 其他分享 >excel递归读取目录并输出

excel递归读取目录并输出

时间:2023-08-03 17:24:37浏览次数:32  
标签:读取 递归 org excel File poi apache import folder

添加依赖


		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>4.1.2</version>
		</dependency>

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

第一种:
package com.xxl.job.admin.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class FolderToExcel {
    private static int rowNum = 0;

    public static void main(String[] args) throws IOException {
        String folderPath = "E:/repo"; // 文件夹路径
        String excelPath = "E:/output.xlsx"; // Excel文件路径

        XSSFWorkbook workbook = new XSSFWorkbook();
        org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet("Folder");

        File folder = new File(folderPath);
        if (folder.isDirectory()) {
            createFolderSheet(folder, sheet, "");
        }

        FileOutputStream outputStream = new FileOutputStream(excelPath);
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();

        System.out.println("Exported to " + excelPath);
    }

    private static void createFolderSheet(File folder, org.apache.poi.ss.usermodel.Sheet sheet, String parentPath) {
        Row row = sheet.createRow(rowNum++);
        Cell cell = row.createCell(0);
        cell.setCellValue(parentPath + folder.getName());

        if (folder.isDirectory()) {
            for (File subFolder : folder.listFiles()) {
                createFolderSheet(subFolder, sheet, parentPath + folder.getName() + " ");
            }
        }
    }
}


第二种
package com.xxl.job.admin.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class FolderToExcel {
    private static int rowNum = 0;

    public static void main(String[] args) throws IOException {
        String folderPath = "E:/repo"; // 文件夹路径
        String excelPath = "E:/output2.xlsx"; // Excel文件路径

        XSSFWorkbook workbook = new XSSFWorkbook();
        org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet("Folder");

        File folder = new File(folderPath);
        if (folder.isDirectory()) {
            createFolderSheet(folder, sheet, "");
        }

        FileOutputStream outputStream = new FileOutputStream(excelPath);
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();

        System.out.println("Exported to " + excelPath);
    }

    private static void createFolderSheet(File folder, org.apache.poi.ss.usermodel.Sheet sheet, String parentPath) {
        if (folder.isDirectory()) {
            Row row = sheet.createRow(rowNum++);
            Cell cell = row.createCell(0);
            cell.setCellValue(parentPath + folder.getName());

            for (File subFolder : folder.listFiles(File::isDirectory)) {
                createFolderSheet(subFolder, sheet, parentPath + folder.getName() + " ");
            }
        }
    }
}

标签:读取,递归,org,excel,File,poi,apache,import,folder
From: https://www.cnblogs.com/szrup0126/p/17603884.html

相关文章

  • Excel中Hyperlink函数的使用
    Hyperlink函数是将文本形式的链接转换为超链接。调用格式:=HYPERLINK(链接,标题)或者:=HYPERLINK(链接)具体可参考Hyperlink函数Microsoft官方文档视频演示:......
  • Vuejs+WebApi导出Excel
    前后端分离,前端Vuejs,后端.Net6WebApi后端代码1publicclassSalesReportController:BaseController2{3privateSerilog.ILogger_log=GetLogger<SalesReportController>();4privatereadonlyISqlSugarClient_db;5privateIHostEnvironme......
  • UE5 蓝图运行时错误:"“无访问”正在尝试读取属性
    场景测试DBBrowser控件,打开网页功能,调试错误:蓝图运行时错误:"“无访问”正在尝试读取属性DBBrowserUI0"。节点:LoadURL图表:EventGraph函数:ExecuteUbergraphLoginUI蓝图:LoginUI分析当前问题是创建了一个DBBrowser的变量,但是没有给它赋值,就直接调用了,导致该值是空的,从而......
  • 循环与递归输出数字的每一位
    #include<stdio.h>//循环voidprintN(intn){intmod=0;while((mod=n%10)!=0){printf("%d\n",mod);n=n/10;}}//递归voidprintn(intn){printf("%d\n",n%10);if(n>=10)......
  • 递归
    递归的两个基本法则1.必须有基准情形(basecase)必须有某些基准情形,它们不用递归就能求解2.必须不断推进(makingprogress)对于需要递归的情形,递归调用必须能够朝着产生基准情形的方向推进......
  • C# 解决导出Excel长数字变成10次幂
    在做项目的时候遇到一个bug,当编号(仅针对纯数字)长度达到一定长度,比如超过11位之后,导出Excel后就转换成了10的幂次方。这是Excel特有的转换,而且当数字的位数达到15,以后的数字就会全部转化成0,造成精度的缺失。这显然是不符合要求的。 原始的赋值代码是这样的。dr["编号"]......
  • 通过读取yaml文件获得多个参数
    importpytestimportrequestsfromutils.read_yamlimportget_yaml_data#多个参数(’class1,class2‘,[('age','eat'),('age','eat')])@pytest.mark.parametrize('title,body,userId',[get_yaml_data()['list_test&#......
  • 单细胞实战(1)数据下载-数据读取-seurat对象创建
    这篇文章我们将介绍从geo数据库下载单细胞测序数据后,多种数据格式多样本情况下,如何读取数据并创建seurat对象。本文主要结构:一、数据下载二、数据读取与seurat对象创建单样本情况下各种格式数据的读取,读取后seurat对象的创建多样本情况下各种格式数据的读取,读取后seurat对象......
  • [转载]Vbox中自动挂载共享文件夹的读取
    转载自Ubuntu中文论坛本文只解决在使用共享文件夹时勾选自动挂载的选项自动挂载被勾选后,虚拟机会自动在目录/media下建立"sf_NAME"的挂载点,其中NAME为在Windows的Vbox中设置的共享文件夹的名称./media目录为Linux为了挂载外部存储设备而设立的目录问题就出在这个挂......
  • # yyds干货盘点 # 盘点一个Python递归的基础题目
    大家好,我是皮皮。一、前言前几天在Python黄金群【维哥】问了一个Python递归的基础问题,一起来看看吧。看上去代码没多少哈,但是韵味无穷。二、实现过程很多初学者遇到这个问题,很容易把答案说成是3,2,2这样,其实正好相反,这里【巭孬嫑勥烎】给了一个解释。这么一看好像还是不太好理解,看看......