通过阅读Excel工作表生成文件

时间:2013-04-01 11:31:45

标签: java excel file

有一个excel文件,其中一个列中有很多文件名。我需要编写一个java代码,该代码应该读取这些文件名并在destination中生成相同的内容。任何人都可以帮助我吗?

3 个答案:

答案 0 :(得分: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();
        }
    }
}

答案 1 :(得分: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() );

答案 2 :(得分: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();
        }

} }