-2

我正在尝试使用以下代码来获取 excel 的所有数据。Excel中的行数为5,列数为20。

PFB 下面的代码

package testRunner;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;

public class ReadWriteExcel {

    public static void main(String args[])
    //public static void ReadExcel() throws IOException
    {
        try
        {
        //Specify the File path which you want to Create or Write
    File src=new File("D:\\eclipse-workspace\\CucumberWithTestNGForSelenium\\PersonalInformation.xlsx");
    //Load the file
    FileInputStream fis=new FileInputStream(src);
    //Load the Workbook
    @SuppressWarnings("resource")
    XSSFWorkbook wb=new XSSFWorkbook(fis);
    //get the sheet which you want to modify or create
    XSSFSheet sh1=wb.getSheetAt(0);

    //Finding the number of rows
    int firstRow=sh1.getFirstRowNum();
    int lastRow=sh1.getLastRowNum()+1;
    int no_of_rows=lastRow-firstRow;



    for(int i=0;i<no_of_rows;i++)
    {
        //Finding the number of Columns

        int no_of_columns=sh1.getRow(i).getLastCellNum();


        for(int j=0;j<no_of_columns;j++)
        {
    System.out.print(" "+ sh1.getRow(i).getCell(j).getStringCellValue()+ " ");
        }
        System.out.println();
    }
        }
        catch(Exception e)
        {
            e.getMessage();
        }}}

在控制台中,第一行显示所有列,但从第二行开始,它只显示 3-4 列。

PFB

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.apache.poi.openxml4j.util.ZipSecureFile$1 (file:/C:/Users/Mehak/.m2/repository/org/apache/poi/poi-ooxml/3.17/poi-ooxml-3.17.jar) to field java.io.FilterInputStream.in
WARNING: Please consider reporting this to the maintainers of org.apache.poi.openxml4j.util.ZipSecureFile$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
 Title  FirstName  LastName  EmailValidation  Password  Date  Month  Year  SignUpCheckbox  AddessFirstName  AddressLastName  Company  MainAddress  AddressLine2  city  State  PostalCode  Country  Mobile  AddressAlias 
 Mr  Simon Duffy  aduffy@abc.com

在此处输入图像描述

Excel 部分在第二张图片上继续:_

在此处输入图像描述

4

2 回答 2

0

我建议你看看Cell getStringCellValue()java 文档:

/**
 * Get the value of the cell as a string
 * <p>
 * For numeric cells we throw an exception. For blank cells we return an empty string.
 * For formulaCells that are not string Formulas, we throw an exception.
 * </p>
 * @return the value of the cell as a string
 */
String getStringCellValue();

可能是CellType第 2 行上其他单元格的 不是 type STRING。您可以尝试编写这样的实用程序方法(从 poi 3.16 开始工作):

private static String myCellStringValue(Cell cell, CellType cellType) {
 String data = EMPTY;

 try {
  switch (cellType) {
   case STRING:
    data = cell.getRichStringCellValue().toString();
    break;
   case NUMERIC:
    data = String.valueOf(cell.getNumericCellValue());
    break;
   case BOOLEAN:
    data = String.valueOf(cell.getBooleanCellValue());
    break;
   case FORMULA:
    data = myCellStringValue(cell, cell.getCachedFormulaResultTypeEnum());
    break;
   case BLANK:
   case ERROR:
   case _NONE:
    break;
  }
 } catch (Exception e) {
  //your catch clause
 }
 return data;
}

然后在循环中调用实用程序方法:

for (int j = 0; j < no_of_columns; j++) {
 Cell cell = sh1.getRow(i).getCell(j);
 System.out.print(" " + myCellStringValue(cell, cell.getCellTypeEnum()) + " ");
}
于 2018-06-05T04:55:01.390 回答
0

您可以尝试使用下面的代码更改您 System.out.print(" "+ sh1.getRow(i).getCell(j).getStringCellValue()+ " "); 的 for 循环内部,并检查它是否解决了您的问题。

DataFormatter formatter = new DataFormatter();
String val = formatter.formatCellValue(sh1.getRow(i).getCell(j));
System.out.print(" "+ val + " ");

或者

String val = sh1.getRow(i).getCell(j).toString();
System.out.print(" "+ val + " ");

或者

System.out.print(" "+ sh1.getRow(i).getCell(j)+ " ");
于 2018-06-06T04:50:04.820 回答