简体   繁体   English

如何使用Apache POI将动态Excel文件(可以超过列数)写入Java对象

[英]how to write dynamic excel file (number of column can exceed) into java objects using apache poi

I understand that we can create model class whose each instance variable will be bound to each excel column like below 我知道我们可以创建模型类,其每个实例变量都将绑定到每个excel列,如下所示

class emp{
    String name;
    String empId;

   //getter and setter method of each
}

excel file: Excel文件:

name | 名称| empid EMPID

abc | abc | 123 123

bcd | bcd | 3232 3232

xyz | xyz | ee32 EE32

how to handle this case when number of column in excel increases dynamically 当excel中的列数动态增加时如何处理这种情况

I would doing this using a kind of table model. 我将使用一种表模型来执行此操作。 A ExcelTableModel similar to the javax.swing.table.DefaultTableModel . 一个类似于javax.swing.table.DefaultTableModel的 ExcelTableModel

Complete example showing a blueprint of this approach: 显示此方法蓝图的完整示例:

import java.io.*;
import org.apache.poi.ss.usermodel.*;

public class UseExcelTableModel {
    public static void main(String[] args) throws Exception {
        Workbook workbook = WorkbookFactory.create(new FileInputStream("ExcelFile.xlsx"));
        Sheet sheet = workbook.getSheetAt(0);
        ExcelTableModel excelTableModel = new ExcelTableModel(sheet);

        System.out.println(excelTableModel.getColumnCount());
        System.out.println(excelTableModel.getRowCount());

        System.out.println(excelTableModel.getColumnNames());
        System.out.println(excelTableModel.getData());

        System.out.println(excelTableModel.getColumnName(1));
        System.out.println(excelTableModel.getValueAt(2, 1));

        workbook.close();
    }
}
import java.util.Vector;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

public class ExcelTableModel {
    private Vector<String> columnNames;
    private Vector<Vector<String>> data;
    private DataFormatter dataFormatter = new DataFormatter();

    public ExcelTableModel(Sheet sheet) {
        this.columnNames = new Vector<String>();
        this.data = new Vector<Vector<String>>();

        int firstRow = sheet.getFirstRowNum();
        Row colNamesRow = sheet.getRow(firstRow);
        int firstCol = colNamesRow.getFirstCellNum();
        int lastColP1 = colNamesRow.getLastCellNum();

        for (int c = firstCol; c < lastColP1; c++) {
            Cell cell = colNamesRow.getCell(c);
            String columnName = dataFormatter.formatCellValue(cell);
            this.columnNames.add(columnName);
        }

        int lastRow = sheet.getLastRowNum();
        for (int r = firstRow + 1; r < lastRow + 1; r++) {
            Vector<String> cells = new Vector<String>();
            Row row = sheet.getRow(r);
            if (row == null) {
                row = sheet.createRow(r);
            }
            for (int c = firstCol; c < lastColP1; c++) {
                Cell cell = row.getCell(c);
                String cellValue = dataFormatter.formatCellValue(cell);
                cells.add(cellValue);
            }
            this.data.add(cells);
        }
    }

    public int getColumnCount() {
        return this.columnNames.size();
    }

    public int getRowCount() {
        return this.data.size();
    }

    public String getColumnName(int columnIndex) {
        return this.columnNames.get(columnIndex);
    }

    public String getValueAt(int rowIndex, int columnIndex) {
        return this.data.get(rowIndex).get(columnIndex);
    }

    public Vector getColumnNames() {
        return this.columnNames;
    }

    public Vector getData() {
        return this.data;
    }

    // More getters and setters...
}

The first sheet in ExcelFile.xlsx can be something like this: ExcelFile.xlsx的第一张表可以是这样的:

在此处输入图片说明

As you see, the table will be got from the used range and the first row of the used range will be got as column names. 如您所见,该表将从使用的范围获得,而使用的范围的第一行将作为列名获得。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM