java - 读取包含特殊 (UTF-8) 字符的 Excel 单元格值 - Selenium/Java
问题描述
我有以下代码:
@Test(groups = {"customer"}, dataProvider = "customerData", dataProviderClass = Testdata.class, priority = 0)
public void createCustomer(String cName, String cAddress, String cAddress2, String cCity, String cState, String cZip, String cContact,
String cPhone) throws InterruptedException {
NAV.customer.create(driver, wait, js);
NAV.customer.edit(driver, wait, js, cName, cAddress, cAddress2, cCity, cState, cZip, cContact, cPhone);
NAV.windowClose(driver, wait, js);
}
并使用以下代码读取我的 Excel 文件:
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Testdata {
public static Object[][] getExcel(String filepath, String sheetName) throws InvalidFormatException, IOException {
FileInputStream file = new FileInputStream(filepath);
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet sheet = wb.getSheet(sheetName);
int rowCount = sheet.getLastRowNum();
int column = sheet.getRow(0).getLastCellNum();
Object[][] data = new Object[rowCount][column];
for (int i = 1; i <= rowCount; i++) {
XSSFRow row = sheet.getRow(i);
for (int j = 0; j < column; j++) {
XSSFCell cell = row.getCell(j);
DataFormatter formatter = new DataFormatter();
String val = formatter.formatCellValue(cell);
data[i - 1][j] = val;
}
}
wb.close();
return data;
}
}
我使用 Selenium 3.14(UTF-8 文本文件编码)和 Apache POI 4.1.0。我的 Excel 文件是 UTF-8 编码的,所有单元格都被格式化为文本。
不幸的是,某些特殊字符(在我的例子中:é、ç 和 Î,但我确信还有更多)无法正确读取。任何帮助将非常感激。
解决方案
我不确定您的特定字符,但是当我阅读具有 / \ [ ] * 的表格时?or : 在它的名字中,例如,它用“_”替换那些无效字符
尝试在任何有特殊字符的地方用“_”替换工作表名称。
推荐阅读
- java - 如何分块读取大型 Json 文件
- graphql - PostGraphile - exportGqlSchemaPath 将主 uuid 列从 id 重命名为 rowId?
- c# - 在 Azure DevOps 管道中包含本地 dll
- javascript - 使用自定义钩子创建类似商店的 redux
- linux - 错误的完整快照版本,应为 '71bf5.... 发现 'aec28...' Dart 编译器意外退出
- sql - Scala 模式匹配 - 识别 Java.Sql 中的连接(基础设施)异常和数据(查询)异常
- mongodb - $MongoDB $lookup foreach 同一集合上数组中的元素
- python - 从两个 numpy 数组中创建一个新数组
- datetime - 如何使用 jest 将带有 Date 的 expect.toMatchObject() 作为预期属性值之一传递?
- powerbi - Power BI - 使用参考表进行合并