0

有一个 excel 文件,其中一个列中有很多文件名。我需要编写一个 java 代码来读取这些文件名并在目的地生成相同的文件名。有人可以帮助我吗?

4

3 回答 3

0
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;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Test {

    public static void main(String[] args) throws IOException {

        try {

            FileInputStream file = new FileInputStream(new File("C:\\test.xls"));

            HSSFWorkbook workbook = new HSSFWorkbook(file);
            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();
                    System.out.print(cell.getStringCellValue() + "\t\t");
                    FileOutputStream outFile =new FileOutputStream(new File("C:\\"+cell.getStringCellValue()));
                    workbook.write(outFile);
                }
                System.out.println("");
            }
            file.close();

          /*  FileOutputStream outFile =new FileOutputStream(new File("C:\\update.xls"));
            workbook.write(outFile);*/
            //outFile.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
于 2013-04-01T11:49:43.747 回答
0

公共类测试{

public static void main(String[] args) throws IOException {

    try {

        FileInputStream file = new FileInputStream(new File("D:\\sampleFileNames.xls"));

        HSSFWorkbook workbook = new HSSFWorkbook(file);
        FileOutputStream outFile= null;
        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();
                    Cell cell = cellIterator.next();
                    File file1 = new File("D:\\Ganesh\\"+cell.getStringCellValue().toString());
                    if(!file1.createNewFile()) {
                        System.out.println("File already exists");


              /*  Cell cell = cellIterator.next();
                System.out.print(cell.getStringCellValue() + "\t\t");
                outFile =new FileOutputStream(new File("D:\\Ganesh\\"+cell.getStringCellValue().toString()));
                //workbook.write(outFile);
            System.out.println("");
        }
        file.close();
        outFile.close();
      /*  FileOutputStream outFile =new FileOutputStream(new File("C:\\update.xls"));
        workbook.write(outFile);*/
        //outFile.close();


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

} }

于 2013-04-02T06:57:40.107 回答
0

您可以使用一些库来读取 excel 文件。就我而言,我选择了Apache POI,您可以免费下载它然后添加到您的项目中。添加后,创建您的文档:

HSSFWorkbook workbook = new HSSFWorkbook( new FileInputStream("filename.xls") );

或者,如果它是 2007 年或之后的 excel 文档:

XSSFWorkbook workbook = new XSSFWorkbook( new FileInputStream("filename.xlsx") );

现在您可以遍历每一行并读出第一列:

for( Row row : workbook.getSheetAt(0) ) // Go through each row in sheet 0
    System.out.println( row.getCell(0).getStringCellValue() );
于 2013-04-01T11:55:19.820 回答