从现有Excel文件中读取单元格并使用Java将它们写入新的Excel文件

时间:2015-12-16 11:09:15

标签: java excel jxl

因此,我正在创建一个程序,使用JXL Java库读取,编辑现有Excel文件中的数据并将其放入新的Excel文件中。在这个阶段,我只想从现有的Excel文件中读取数据并将其放入新文件中。似乎无法找到一种好的方法来完成它并为将来的工作编辑数据(具有某些列)并将它们放入新的Excel文件中的最佳方法。

很抱歉,如果有什么不清楚,只需掌握Java和Stackoverflow。

import java.io.*;
import jxl.*;
import jxl.write.Number;
import jxl.write.*;

class Extraction {

private String inputFile;

String data;

public void setInputFile(String inputFile){

    this.inputFile = inputFile;

}

public void read() throws IOException {

    File spreadsheet = new File(inputFile);

    Workbook w;

    try {

        w = Workbook.getWorkbook(spreadsheet);

        Sheet page = w.getSheet(0);

        File f = new File("C:\\Users\\alex\\Desktop\\New_Export.xls");                                                                                      

            if (!f.exists()){

                String FileName = "C:\\Users\\alex\\Desktop\\New_Export.xls";

                WritableWorkbook excel = Workbook.createWorkbook(new File(FileName));

                WritableSheet sheet = excel.createSheet("Sheet1", 0);                                           

                    for (int y = 0; y < page.getRows(); y++){

                        for (int x = 0; x < page.getColumns(); x++){

                            Cell cell = page.getCell(x ,y +1);
                            CellType type = cell.getType();

                                if(type == CellType.LABEL || type == CellType.NUMBER){

                                    data = cell.getContents();

                                    System.out.println(data);




                                    //To now input the read data into the new Excel File

                                    sheet.addCell();

                                }

                        }

                        System.out.println(" ");

                    }   

                System.out.println("The File Has Successfully Been Created!");

                excel.write();

                excel.close();

                }

                else{

                    System.err.println("File is already created!");

                }

    }
    catch(Exception e){

        System.out.println(" ");  

    }       

}       

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

    Extraction reading = new Extraction();

    reading.setInputFile("C:\\Users\\alex\\Desktop\\export.xls"); 

    reading.read();

}
}

1 个答案:

答案 0 :(得分:1)

使用apache中的“poi”,你可以使用下面的代码来读取excel(* .xls)

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;

private void readingExcel(String fileName) {
    try (FileInputStream file = new FileInputStream(new File(fileName))) {
        HSSFWorkbook workbook = new HSSFWorkbook(file);
        HSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();
        for (Row row : sheet) {
            Iterator<Cell> cellIterator = row.cellIterator();
            for (Cell cell : row) {
                System.out.println(cell.getStringCellValue());
            }
        }
    } catch (IOException ioException) {
        ioException.printStackTrace();
    }
}

写入excel

private File writingToExcel() {
    HSSFWorkbook workbook = new HSSFWorkbook();
    HSSFSheet sheet = workbook.createSheet("Sample-sheet");
    Row row = sheet.createRow(0);
    Cell cell = row.createCell(0);
    cell.setCellValue("My Sample Value");

    File file = new File(sheet.getSheetName());
    return file;
}

但是,如果要使用* .xlsx,则用于读取的类是

XSSFWorkbook workbook = new XSSFWorkbook (file); 
XSSFSheet sheet = workbook.getSheetAt(0);