java - 使用jxl如何处理文件名变化?

标签 java jxl

我想使用 JXl 读取 xls 文件,其中 xls 文件名总是在变化,如何读取,请帮忙。 我尝试了下面的代码

FileInputStream filepath = new FileInputStream("C:\\Users\\sameer.joshi\\Downloads\\*.xls");

FileInputStream filepath = new FileInputStream("C:\\Users\\sameer.joshi\\Downloads\\");

最佳答案

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class Test {    
public static void main(String[] args) throws FileNotFoundException, IOException {

    File directory = new File("C:\\Users\\sameer.joshi\\Downloads\\");
    File[] all_XLS_Files = directory.listFiles(); //all files in that directory

    for (File file : all_XLS_Files) { // iterate through all files in that directory
      if(file.getName().endsWith(".xls")){ // select only xls files
        //do something with your xls files here
        //for example print out the file name
        System.out.println(file.getName());
        //or read one or all of them 
        FileInputStream fileInputStream = new FileInputStream(new File(file.getPath()));

        //Get the workbook instance for XLS file 
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);

        //Get first sheet from the workbook
        HSSFSheet sheet = workbook.getSheetAt(0);

        //Iterate through each rows from first sheet
        Iterator<Row> rowIterator = sheet.iterator();
        while(rowIterator.hasNext()) {
            Row row = rowIterator.next();

            //For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();
            while(cellIterator.hasNext()) {

                Cell cell = cellIterator.next();

                switch(cell.getCellType()) {
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.print(cell.getBooleanCellValue() + "\t\t");
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print(cell.getNumericCellValue() + "\t\t");
                        break;
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getStringCellValue() + "\t\t");
                        break;
                }
            }
            System.out.println("");
        }
        fileInputStream.close();
        FileOutputStream out = 
            new FileOutputStream(new File(file.getPath()));
        workbook.write(out);
        out.close();
      }
    }  
}

}

关于java - 使用jxl如何处理文件名变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35771040/

相关文章:

java - 从 Excel 文件中读取内容

java - 尝试添加数组列表

java - 无法创建生成的 HTTP 客户端所需的返回类型,因为没有从 ByteBuffer 到类 java.io.File : Micronaut 的 TypeConverter

java - 在另一个项目的属性文件中引用另一个项目的目录

java - 在多线程中,为什么当我暂停一个线程执行时其他线程不工作

java - Java中的Excel生成

java - 编译 : wrong version 50. 0 时出现异常,应为 49.0

具有最大行的 Java JXL 异常

java - 为什么 findViewById 返回 null?

java - 如何读取 Stringbuffer 中具有 html 表之类数据的 Read .xls 文件?