JavaでPOIを使用して、Excelシートから特定の列データを取得してオブジェクトの配列リストに格納するにはどうすればよいですか?

Ali.bu

Firstname、LastName、IDなどの列を含むExcelファイルがあります。指定されたすべての列のすべてのデータを取得して、オブジェクトの配列リストに格納したいと思います。問題は、取得しているデータがFirstname列やLastNameなどの特定の列名に属していることを確認できないことです。たとえば、このシーケンス(LastName、Firstname、ID)のExcelファイルと、異なるシーケンス(ID、LastName、Firstname)の2番目のExcelファイルがありますこの姓がこの名とID用であることを確認するにはどうすればよいですか?私はいくつかの研究をしましたが、私はまったく同じようなものを得ることができませんでした。誰かが私を助けてくれれば幸いです。

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
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 ReadExcelDemo 
{
ArrayList<Data> list = new ArrayList<>();
String path;

public ReadExcelDemo(String path)
{

   this.path = path;

    try
    {
        FileInputStream file = new FileInputStream(new File(path));

        //Create Workbook instance holding reference to .xlsx file
        XSSFWorkbook workbook = new XSSFWorkbook(file);

        //Get first/desired sheet from the workbook
        XSSFSheet sheet = workbook.getSheetAt(0);

         System.out.println("");

        //Iterate through each rows one by one
        Iterator<Row> rowIterator = sheet.iterator();
        while (rowIterator.hasNext()) 
        {
            Row row = rowIterator.next();
            //For each row, iterate through all the columns
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) 
            {

                Cell cell = cellIterator.next();

                cell.setCellType(Cell.CELL_TYPE_STRING);
                //Check the cell type and format accordingly



                JOptionPane.showMessageDialog(null,cell.getAddress()); 
                switch (cell.getCellType()) 
                {

                    case Cell.CELL_TYPE_NUMERIC:

                        System.out.print(cell.getNumericCellValue() + "\t");

                        break;
                    case Cell.CELL_TYPE_STRING:

                        System.out.print(cell.getStringCellValue() + "\t");
                        break;
                }

            }


            System.out.println("");
        }
        file.close();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}
}

これはArraylistの私の学生クラスです

public class Student {

private String Firstname;
private String LastName;
private String Username;


public Student(String firstname, String lastName, String username) {

    this.Firstname = firstname;
    this.LastName = lastName;
    this.Username = username;
}

public String getFirstname() {
    return Firstname;
}
public void setFirstname(String firstname) {
    Firstname = firstname;
}

public String getLastName() {
    return LastName;
}
public void setLastName(String lastName) {
    LastName = lastName;
}
public String getUsername() {
    return Username;
}
public void setUsername(String username) {
    Username = username;
}

}

これは私のメインクラスです

import java.io.IOException;

public class Main {

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

ReadExcelDemo re = new ReadExcelDemo( "E:/St.xlsx");
}

}

これは私の現在のコードの結果です

これは私のExcelファイルの例です

ゴミ箱

あなたの場合、あなたが読む最初の3つのセルは列の名前です

row.getCell(columnIndex)を使用できます。そのコードを見てください。列ごとにデータを取得しています。

for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
  row = sheet.getRow(rowIndex);
  if (row != null) {
    String cellValueMay = null;
    for (int colIndex = 0; colIndex < colCount; colIndex++) {
      if (colIndex == theColIndexYouWant) {
        cell = row.getCell(colIndex);
        if (cell != null) {
          // Found column and there is value in the cell.
          cellValueMaybeNull = cell.getStringCellValue();
          break;
        }
    }

    // Do something with the cellValueMaybeNull here ...
  }
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ