繁体   English   中英

如何使用Apache POI从Excel中获得价值

[英]How to get value from excel using apache poi

我只是想从excel文件中获取每个单元格的值,但是我已经合并了每个单元格,但是我希望每个单元格都不同

import java.io.File;import java.io.FileInputStream;import java.util.Iterator;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class Read {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

            try
            {
                FileInputStream file = new FileInputStream(new File("D://new/excelnew/student_usr_mst_dtls.xlsx"));

                //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);

                //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();
                        //Check the cell type and format accordingly
                        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());
                                break;
                        }
                    }
                    System.out.println("");
                }
                file.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

}

而OutPut就像字符串一样

IDNAMELASTNAME 1.0tAmitShukla 2.0tLokeshGupta

我希望每个单元格都是唯一的,所以该怎么做。 如果可能的话请举一个例子。

在您打印的每个单元格值后放置一个逗号。

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

System.out.print(cell.getStringCellValue() + ",");

暂无
暂无

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

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