简体   繁体   中英

How to check if a cell value in excel contains alt+enter using apache poi?

Sample Excel file I am using Below mentioned is the code i have used but it isnt working. My requirement is to separate out each test steps and store it. I have used "alt+enter" to write multiple lines in the same cell. If some how i can detect "alt+enter" while proccessing cell data with apache POI, I can easily separate my lines mentioned.

package excelExportAndFileIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.XSSFWorkbook;
import org.junit.Test;


public class ExcelHelper {


    public static Map<String,String> GetData(String key,String sheetName) throws IOException{
        HashMap<String, String> hm= new HashMap<String, String>();

        try {
             List<String> keyList=new ArrayList<>();
                List<String> valueList=new ArrayList<>();
            //String path = new File(".").getCanonicalPath();
            //String spath = "C:\\Users\\gur39708\\Documents\\INS500_TestPlan_ver2.xlsx";
            String spath = "mypath"+"\\INS500_TestPlan_ver2.xlsx";

            String fileExtensionName = spath.substring(spath.indexOf("."));

            File file =    new File(spath);
            //Create an object of FileInputStream class to read excel file

            FileInputStream inputStream = new FileInputStream(file);
            Workbook wb = null;
            //Find the file extension by spliting file name in substring and getting only extension name


            //Check condition if the file is xlsx file

            if(fileExtensionName.equals(".xlsx")){

            //If it is xlsx file then create object of XSSFWorkbook class
                wb = new XSSFWorkbook(inputStream); 

            }

            //Check condition if the file is xls file

            else if(fileExtensionName.equals(".xls")){

            //If it is xls file then create object of XSSFWorkbook class

                wb = new HSSFWorkbook(inputStream);

            }


            //Read sheet inside the workbook by its name

            Sheet sh = wb.getSheet(sheetName);

            //Get the current count of rows in excel file

            int rowCount = sh.getLastRowNum()+1;

            System.out.println("rowcount is "+rowCount);

          //Create a loop over all the rows of excel file to read it
            int start=0;
            for (int i = 0; i < rowCount; i++) {

               Row row = sh.getRow(i);

                //Create a loop to print cell values in a row

                for (int j = 0; j < row.getLastCellNum(); j++) {

                    if (row.getCell(j).getStringCellValue().equals(key)){
                        String temp = row.getCell(j+1).getStringCellValue();
                        System.out.println(temp.contains("\\n"));
                    }

                }
                start ++;

            }    
            for(int i=0;i<=keyList.size()-1;i++){
                hm.put(keyList.get(i), valueList.get(i));
            }

        }catch(Exception e){
            System.out.println(e);
        }
        return hm;
    }

    public int getIndex(String keyword, String SheetName){


        return 0;

    }

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

        Map<String, String> hm1= new HashMap();
        hm1 = GetData("device types","Sheet1");
        //System.out.println(hm1.size());


    }

}

I think the problem is with System.out.println(temp.contains("\\\\n"));

it should be System.out.println(temp.contains("\\n")); the newline char is represented by "\\n" not "\\\\n"

the following works fine for me :

// reading cell value

String tempName = cell.getStringCellValue();

if(tempName.contains("\\n")) {

System.out.println("Contains alt+enter"); }

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