如何在Java中读取Excel (.xlsx) 文件?

5

我正在试图使用Java读取给定的.xlsx文件中的数据。

enter image description here

读取文件的代码如下:

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

        FileInputStream file = new FileInputStream(new File("E:\\test1.xlsx"));
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet test = workbook.getSheetAt(0);

        student emp = new student();
        Iterator<Row> itr = test.iterator();
        itr.next();
        while(itr.hasNext()){
               Row row = itr.next();
               emp.reedData(row);
               System.out.println(id+","+name+","+options);
        }

以下是方法步骤:
void reedData(Row row){
    id= row.getCell(0).toString();
    name= row.getCell(1).toString();
    options= row.getCell(2).toString();
}

但是,我得到的输出如下:
1.0,X,play game
,,sing song
2.0,Y,play game
,,sing song

与其上述方式不同,我希望输出的结果如下所示:

1.0,X,{play game,sing song}
2.0,Y,{play game,sing song}

这个问题是因为我正在合并 .xlsx 文件中的两个单元格。有什么建议吗?谢谢您提前的帮助。

你需要读取包含选项的列的两行,以及其他列中的每个多行。 - trappski
2个回答

4
这是我完成它的方式。
import static java.lang.System.out;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
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.DataFormatter;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public  class  read {
    public static void main(String [] args){
        FileInputStream excelFile = new FileInputStream(new File(keep));
        XSSFWorkbook wb = new XSSFWorkbook(excelFile);
        XSSFSheet sheet = wb.getSheetAt(0);

        int rowCount; // number of rows of newly form excel files 
        rowCount= sheet.getPhysicalNumberOfRows();
        out.println(rowCount);

        //trying to iterate the excel file
        int i =0;
        Iterator<Row> rowIterator     = sheet.iterator();
         do{
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while(cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                if(cell.getColumnIndex()==0) { // to read the first column 
                    DataFormatter df = new DataFormatter();
                    String str2      = df.formatCellValue(cell);
                    customerNo.add(str2);
                }
            }
            ++i;
            if(i>rowCount)break;
            //break;
        }while(rowIterator.hasNext());
        wb.close();
   }
}

希望这有所帮助。

0

看起来您正在使用Apache POI。 请尝试在您的代码中合并这两个单元格:

test.addMergedRegion(1,2,0,0);

或者:

sheet.addMergedRegion(new CellRangeAddress(1,2,0,0))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接