使用java对象映射excel字段的最佳方法

问题描述 投票:1回答:3

我正在尝试使用java对象映射excel字段。什么是最好的方法,而不是依赖于列号。

Cell orderId=row.getCell(0);
System.out.println("orderId" +orderId);
Cell trackingId=row.getCell(1);

Cell orderTitle = row.getCell(2);

Cell customerName = row.getCell(3);

Cell customerAddress = row.getCell(5);

如何使用对象变量映射列而不依赖于列号?

我正在寻找一些我可以用对象映射行标题的东西,其余的应该独立于行号。有什么建议?

java excel apache-poi spring-jdbc xssf
3个回答
1
投票

如果要使用对象变量映射列而不依赖于列号,则需要使用某种方法将类fieldName与excel文件头名称相匹配并相应地设置值。一种方法是使用reflection

  1. 使用反射读取给定类的所有字段,
  2. 遍历excel文件的所有行。
  3. 将字段名称与标题匹配,并获取所有字段的标题索引。
  4. 从每个索引的单元格中获取值。
  5. 最后使用反射将值设置到对象的每个字段。
  6. 返回对象列表。

我创建了一个简单的Java library,以便将Excel文件转换为对象列表。这可能对你有帮助。


0
投票

如果我正确理解了您的问题,您希望将属性映射到列,以便您可以通过引用属性而不是列号来获取订单的属性。

int orderIdColumn = 0;
int trackingIdColumn = 1;
int orderTitleColumn = 2;
int customerNameColumn = 3;
int customerAddressColumn = 5;

根据您的需要,您还可以声明一个int字段,表示包含标题单元格的行。

您声明这些变量的位置取决于程序其余部分的设计,但显然必须在表示订单的对象之外,因为它们不是特定于订单,而是特定于Excel电子表格。


0
投票
package com.jcg.example;

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.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WriteExcelFileExample {
    private static final String FILE_PATH = "<Path to ur excel file>";

    private static final WriteExcelFileExample INSTANCE = new WriteExcelFileExample();

    public static WriteExcelFileExample getInstance() {
        return INSTANCE;
    }

    private WriteExcelFileExample() {
    }

    public static void main(String args[]){

        List studentList = new ArrayList();
        studentList.add(new Student("Magneto","90","100","80"));
        studentList.add(new Student("Wolverine","60","60","90"));
        studentList.add(new Student("ProfX","100","100","100"));

        writeStudentsListToExcel(studentList);

    }

    public static void writeStudentsListToExcel(List studentList){

        // Using XSSF for xlsx format, for xls use HSSF
        Workbook workbook = new XSSFWorkbook();

        Sheet studentsSheet = workbook.createSheet("Students");

        int rowIndex = 0;
        for(Student student : studentList){
            Row row = studentsSheet.createRow(rowIndex++);
            int cellIndex = 0;
            //first place in row is name
            row.createCell(cellIndex++).setCellValue(student.getName());

            //second place in row is marks in maths
            row.createCell(cellIndex++).setCellValue(student.getMaths());

            //third place in row is marks in Science
            row.createCell(celenter code herelIndex++).setCellValue(student.getScience());

            //fourth place in row is marks in English
            row.createCell(cellIndex++).setCellValue(student.getEnglish());

        }

        //write this workbook in excel file.
        try {
            FileOutputStream fos = new FileOutputStream(FILE_PATH);
            workbook.write(fos);
            fos.close();

            System.out.println(FILE_PATH + " i[enter link description here][1]s successfully written");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}


  [1]: https://examples.javacodegeeks.com/core-java/writeread-excel-files-in-java-example/
© www.soinside.com 2019 - 2024. All rights reserved.