如何阅读特定的Excel列并使用其单元名称创建一个新目录?

时间:2017-04-19 11:27:02

标签: java excel apache-poi

我有一个Excel文件,其中包含A,B和C列以及N行数,我想读取C列并为每个单元格创建一个新目录,并将该单元格名称作为目录名称 任何帮助?这是我到目前为止写的

    void CreateDir(String xlsPath) throws IOException {
    ChooseFileClass cf = new ChooseFileClass();
    ChooseWorkSpace ws = new ChooseWorkSpace();
    try {
        Workbook workbook = new XSSFWorkbook(new FileInputStream(xlsPath));
        Sheet sheet = workbook.getSheetAt(0);

        for (Row row : sheet) {
            for (Cell cell : row) {
                cell.setCellType(Cell.CELL_TYPE_STRING);
                System.out.print(cell.getStringCellValue() + "\t");
                String folder = cell.getStringCellValue();
                File file = new File(ws.getWorkSpacePath() + folder);
                if (!file.exists()) {
                    if (file.mkdir()) {
                        // directory created
                    } else {
                        System.err.println("Directory generation failed");
                    }
                }
            }
            System.out.println();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

2 个答案:

答案 0 :(得分:0)

假设循环遍历excel单元格的实现工作正常,您可以使用java.io.File类创建您选择的目录。

boolean isCreated = new File("/usr/tmp/newdir").mkdir();

在for循环中,执行以下操作

for(Cell cell  : row) {                                            
     cell.setCellType(Cell.CELL_TYPE_STRING);
     System.out.print(cell.getStringCellValue() + "\t");
     String folder = cell.getStringCellValue();
     File file = new File(STATIC_DIRECTORY + DELIMITER + folder);
     if (!file.exists()) {
        if (file.mkdir()) {
            // directory created
        } else {
            System.err.println("Directory generation failed");
        }
    }
 }

STATIC_DIRECTORYDELIMITER是您必须根据您的环境分配的常量。

答案 1 :(得分:0)

结合@ aksappy的代码,你可以优化你的for循环:

void CreateDir(String xlsPath) throws IOException {
  ChooseFileClass cf = new ChooseFileClass();
  ChooseWorkSpace ws = new ChooseWorkSpace();
  Cell cell;

  try {
     Workbook workbook = new XSSFWorkbook(new FileInputStream(xlsPath));
     Sheet sheet = workbook.getSheetAt(0);

     for (int i = 0; i < N; i++) {  /* use N Since you know the number of rows */
        cell = sheet.getCell(3,i);  /* Assuming column C corresponds to 3rd column */
        cell.setCellType(Cell.CELL_TYPE_STRING);
        System.out.print(cell.getStringCellValue() + "\t");
        String folder = cell.getStringCellValue();
        File file = new File(STATIC_DIRECTORY + DELIMITER + folder);
        if (!file.exists()) {
           if (file.mkdir()) {
           // directory created
           } 
           else {
              System.err.println("Directory generation failed");
           }
        }

        System.out.println();
     }
  } 
  catch (FileNotFoundException e) {
     e.printStackTrace();
  } 
  catch (IOException e) {
     e.printStackTrace();
  }

}