我想将displayDirectoryContents的输出写入Excel工作表

问题描述 投票:0回答:1

我想将displayDirectoryContents的输出写入Excel工作表

我尝试使用Apache POI方法将输出输出到Excel工作表一列中的文件夹和文件名另一列中文件的名称进口声明

public class Excel {

private static String dest = "C:\\Users\\mahselva\\testexcel.xls";
private static HSSFWorkbook myWorkBook = new HSSFWorkbook();
private static HSSFSheet mySheet = myWorkBook.createSheet();

public static void excelLog(String filename, String message, int rowNum) 
{

HSSFRow myRow = null;
HSSFCell myCell = null;
String excelData[][] = new String[1][2];
excelData[0][0] = filename;
excelData[0][1] = message;

myRow = mySheet.createRow(rowNum);

for (int cellNum = 0; cellNum < 2; cellNum++) {
    myCell = myRow.createCell(cellNum);
    myCell.setCellValue(excelData[0][cellNum]);

}
try {
    FileOutputStream out = new FileOutputStream(dest);
    myWorkBook.write(out);
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}
}

public static void main(String[] args) {
File currentDir = new File("C:\\OracleATS\\openScript"); // current 
directory
displayDirectoryContents(currentDir);
}
public static void displayDirectoryContents(File dir) {
try {
    int i = 0;
    File[] files = dir.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            Path path = Paths.get(file.getCanonicalPath());
            //System.out.println("Folder" 
+path.getFileName().toString());
            excelLog("Folder",path.getFileName().toString(),i);
            i++;
            displayDirectoryContents(file);
        } else {
            Path path = Paths.get(file.getCanonicalPath());
            //System.out.println(path.getFileName().toString());
            excelLog("File",path.getFileName().toString(),i);
            i++;
        }

    }
} catch (IOException e) {
    e.printStackTrace();
} 
}
}

我想要Excel表格中的两列,其中第一列包含File或包含文件/文件夹名称的文件夹和列2例如文件books.xml文件夹脚本

因此我想将输出写入Excel工作表我正在使用功能excel日志写入输出屏幕

java eclipse
1个回答
0
投票
我使用它来写一个excel-但是我将其设置为.csv格式,而不是xls。因此,这可能不是您所需要的,但是它仍在部分有用,因为它正在写入可以轻松地由excel打开的文件中。
© www.soinside.com 2019 - 2024. All rights reserved.