简体   繁体   中英

how to convert excel cell value to string type?setting cell to text type in excel is not working

I am trying to read a value from excel sheet and enter it on text field there by 在此处输入图片说明 comparing the values from webelement. 在此处输入图片说明

how to get this value from cell as string and not numeric? below is given code for reading and wrting excel

public String getValueFromCell(int iRowNumber, int iColNumber)
   {
      XSSFRow row = sheet.getRow(iRowNumber);
      Cell cell = row.getCell(iColNumber);

      return cell == null ? "" : cell.toString();
   }

   /**
    * returns no. of rows of excel sheet
    * 
    * @return
    */
   public int getRowCount()
   {
      return sheet.getLastRowNum();
   }

   /**
    * returns no. of columns of excel sheet
    * 
    * @param sheetName
    * @return
    */
   public int getColumnCount(String sheetName)
   {

      sheet = workbook.getSheet(sheetName);
      row = sheet.getRow(0);
      return row.getLastCellNum();
   }

   /**
    * return row number of a String value
    * 
    * @param value
    * @return
    */
   public int getRowNumber(String value)
   {
      XSSFSheet indexsheet = workbook.getSheet("Index");
      for(int i = 0; i <= indexsheet.getLastRowNum(); i++)
      {
         row = indexsheet.getRow(i);
         if(value.equalsIgnoreCase(row.getCell(0).getStringCellValue()))
         {
            return i + 1;
         }
      }
      return 0;
   }

   /**
    * sets data in a cell
    * 
    * @param path
    * @param sheetName
    * @param colName
    * @param rowNum
    * @param data
    * @return
    */
   public boolean setCellData(String path, String sheetName, String colName, int rowNum, String data)
   {
      try
      {
         if(rowNum <= 0)
            return false;

         int index = workbook.getSheetIndex(sheetName);
         int colNum = -1;
         if(index == -1)
            return false;


         sheet = workbook.getSheetAt(index);


         row = sheet.getRow(0);
         for(int i = 0; i < row.getLastCellNum(); i++)
         {
            if(row.getCell(i).getStringCellValue().trim().equals(colName))
               colNum = i;
         }
         if(colNum == -1)
            return false;

         sheet.autoSizeColumn(colNum);
         row = sheet.getRow(rowNum - 1);
         if(row == null)
            row = sheet.createRow(rowNum - 1);

         cell = row.getCell(colNum);
         if(cell == null)
            cell = row.createCell(colNum);

         cell.setCellValue(data);

      }
      catch(Exception e)
      {
         e.printStackTrace();
         return false;
      }
      return true;
   }

}

As per my knowledge, we can achieve this in two ways.

Method 1: Add apostrophe(') before the numeric values on Excel. For Example, if you have 1234 in excel, then replace with '1234. Adding apostrophe will convert numeric into String.

Method 2: Handle it via Java. For example, do something like this,

String value = new String("value read from Excel");

Note: I haven't tested method 2 but it will actually work.

Hope this helps. Thanks.

I too faced lots of issue in this. Just write the below two lines of Code. It will work.

Cell cell= data.getRow(i).getCell(0);
String orderID1=cell.toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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