使用Apache POI在Java中读取/写入Excel文件的问题

时间:2014-06-15 09:08:35

标签: java loops apache-poi

大家好,我是java poi库的新手,我正在努力学习这个库,但仍然没有运气

我想有这个输出

Excel1.xls有此数据

  

ZIP CODE | PLACE |日期
  211

我希望复制所有第一行数据。

  

ZIP CODE | PLACE | DATE

并将其放在另一张纸上

这是我制作的代码

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

    try {
        FileInputStream file = new FileInputStream(new File("d:\\input.xls"));

        HSSFWorkbook workbook = new HSSFWorkbook(file);
        HSSFSheet sheet = workbook.getSheetAt(0);
        HSSFSheet zip1 = workbook.createSheet("ZIP CODE 1");


        for(Row row : sheet){
            int i=0;

            for(Cell cell : row){

                cell.setCellType(Cell.CELL_TYPE_STRING);
                System.out.print(cell.getStringCellValue() + "\t");
                String a = cell.getStringCellValue();

                cell = zip1.createRow(i).createCell(i);

                i++;
                cell.setCellValue(a);
             }
             break;

         }

        file.close();
        FileOutputStream outFile =new FileOutputStream(new File("d:\\output.xls"));
        workbook.write(outFile);
        outFile.close();

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

2 个答案:

答案 0 :(得分:3)

发现的问题

  1. 对于.xlsx文件 - > HSSFWorkbook应为XSSFWorkbook

  2. 循环。你不想循环你只想要第一行的每一行。只需循环列

  3. 每次要写入单元格时都不要创建行。仅创建一个新行。

  4. 工作示例:

    try {
        FileInputStream file = new FileInputStream(new File(
                "C:\\path\\Book1.xlsx"));
    
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheetAt(0);
        XSSFSheet zip1 = workbook.createSheet("ZIP CODE 1");
    
        Row readFirstRow = sheet.getRow(0);
        Row writeFirstRow = zip1.createRow(0);
    
        for (Cell cell : readFirstRow) {
    
            cell.setCellType(Cell.CELL_TYPE_STRING);
            String a = cell.getStringCellValue();
    
            cell = writeFirstRow.createCell(cell.getColumnIndex());
            cell.setCellValue(a);
        }
    
        file.close();
        FileOutputStream outFile = new FileOutputStream(new File(
                "C:\\path\\BookOut.xlsx"));
        workbook.write(outFile);
        outFile.close();
    
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    

答案 1 :(得分:0)

Try to use Xcelite
https://github.com/eBay/xcelite#writing

Write:

public class User { 

  @Column (name="Firstname")
  private String firstName;

  @Column (name="Lastname")
  private String lastName;

  @Column
  private long id; 

  @Column
  private Date birthDate; 
}

Xcelite xcelite = new Xcelite();    
XceliteSheet sheet = xcelite.createSheet("users");
SheetWriter<User> writer = sheet.getBeanWriter(User.class);
List<User> users = new ArrayList<User>();
// ...fill up users
writer.write(users); 
xcelite.write(new File("users_doc.xlsx"));