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



我正在使用的示例 Excel 文件 下面提到的是我使用过的代码,但它不起作用。我的要求是分离出每个测试步骤并存储它。我使用"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());

}
}

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

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

以下内容对我来说很好用:

读取单元格值

String tempName = cell.getStringCellValue();

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

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

最新更新