如何使用apache poi检查excel中的单元格值是否包含alt + enter?

时间:2018-10-23 07:01:59

标签: java excel apache-poi xlsx

Sample Excel file I am using下面提到的是我使用的代码,但是它不起作用。我的要求是将每个测试步骤分开并存储。我已经使用“ alt + enter”在同一单元格中写了多行。如果我在使用Apache POI处理单元格数据时如何检测“ alt + enter”,我可以轻松地将我提到的行分开。

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


    }

}

1 个答案:

答案 0 :(得分:0)

我认为问题在于 System.out.println(temp.contains("\\n"));

应该是 System.out.println(temp.contains("\n")); 换行符由“\n”而不是“\\n”表示

以下对我来说很好用:

//读取单元格值

String tempName = cell.getStringCellValue();

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

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