Java Selenium - CSV UTF-8 格式文件创建不适用于 Salesforce
Java Selenium - CSV UTF-8 format file creation is not working for Salesforce
我面临着一个独特的挑战。我的应用程序基于 SalesForce,我必须在其中创建一个列表(名字、姓氏、电子邮件)并在应用程序中上传。我正在使用 OpenCSV 生成文件并上传,但应用程序无法识别。
手动 工作原理:
- 下载开发者提供的.xls unicode格式模板
- 用微软打开Excel
- 填充数据并保存为 CSV UTF-8(逗号分隔)(*.csv)
手动修改当前模板时工作正常。
自动化方法:
- 借助 Java 和 OpenCSV 复制文件以使用相同的模板
- 填充数据
- 刷新文件
- 将文件复制为 CSV 格式
但是当我上传文件时,申请不被接受。也没有收到任何错误消息。
请在下面找到我目前拥有的代码。任何帮助将不胜感激。
public void prepareCSVfile(int numberOfRecords) throws IOException {
String filePath = Constants.CONFIGURATION_PATH + "csv\" + Constants.RUNNAME + ".xls";
String fileName = Constants.CONFIGURATION_PATH + "csv\" + Constants.RUNNAME + ".csv";
File source=new File(Constants.CONFIGURATION_PATH + "csv\" +"Template.xls");
File destination=new File(filePath);
copyFile(source,destination);
CSVWriter writer = new CSVWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), "UTF-8"), CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER,CSVWriter.DEFAULT_LINE_END);
for (int i = 0; i < numberOfRecords; i++) {
String firstName = Commons.getRandomAlphabet(4).toUpperCase();
String lastName = Commons.getRandomAlphabet(4).toUpperCase();
String emailAddress = Commons.getRandomAlphabet(8).toUpperCase() + "@email.com";
String lines[] = { firstName, lastName, emailAddress };
writer.writeNext(lines);
System.out.println(lines);
}
// Flushing data from writer to file
writer.flush();
writer.close();
copyFile(new File(filePath),new File(fileName));
System.out.println("Data entered");
}
只是想让社区知道我找到了解决方案。在写入文件时,openCSV 不是一个好的选择。我正在使用 Apache commons csv,它工作正常。
这是更新后的工作代码。
String filePath = Constants.CONFIGURATION_PATH + "csv\" + Constants.RUNNAME + ".csv";
File source=new File(Constants.CONFIGURATION_PATH + "csv\" +"MyTemplate.csv");
CSVPrinter writer = new CSVPrinter(new FileWriter(filePath), CSVFormat.EXCEL);
writer.printRecord("First Name","Last Name","Email");
for (int i = 0; i < numberOfRecords; i++) {
List<String> nextLine = new ArrayList<>();
nextLine.add(Commons.getRandomAlphabet(6).toUpperCase());
nextLine.add(Commons.getRandomAlphabet(6).toUpperCase());
nextLine.add(Commons.getRandomAlphanumeric(10).toUpperCase() + "@email.com");
writer.printRecord(nextLine);
//data.add(new String[]{ firstName, lastName, emailAddress });
}
writer.flush();
writer.close();
我面临着一个独特的挑战。我的应用程序基于 SalesForce,我必须在其中创建一个列表(名字、姓氏、电子邮件)并在应用程序中上传。我正在使用 OpenCSV 生成文件并上传,但应用程序无法识别。
手动 工作原理:
- 下载开发者提供的.xls unicode格式模板
- 用微软打开Excel
- 填充数据并保存为 CSV UTF-8(逗号分隔)(*.csv)
手动修改当前模板时工作正常。
自动化方法:
- 借助 Java 和 OpenCSV 复制文件以使用相同的模板
- 填充数据
- 刷新文件
- 将文件复制为 CSV 格式
但是当我上传文件时,申请不被接受。也没有收到任何错误消息。
请在下面找到我目前拥有的代码。任何帮助将不胜感激。
public void prepareCSVfile(int numberOfRecords) throws IOException {
String filePath = Constants.CONFIGURATION_PATH + "csv\" + Constants.RUNNAME + ".xls";
String fileName = Constants.CONFIGURATION_PATH + "csv\" + Constants.RUNNAME + ".csv";
File source=new File(Constants.CONFIGURATION_PATH + "csv\" +"Template.xls");
File destination=new File(filePath);
copyFile(source,destination);
CSVWriter writer = new CSVWriter(new OutputStreamWriter(new FileOutputStream(filePath, true), "UTF-8"), CSVWriter.DEFAULT_SEPARATOR, CSVWriter.NO_QUOTE_CHARACTER, CSVWriter.NO_ESCAPE_CHARACTER,CSVWriter.DEFAULT_LINE_END);
for (int i = 0; i < numberOfRecords; i++) {
String firstName = Commons.getRandomAlphabet(4).toUpperCase();
String lastName = Commons.getRandomAlphabet(4).toUpperCase();
String emailAddress = Commons.getRandomAlphabet(8).toUpperCase() + "@email.com";
String lines[] = { firstName, lastName, emailAddress };
writer.writeNext(lines);
System.out.println(lines);
}
// Flushing data from writer to file
writer.flush();
writer.close();
copyFile(new File(filePath),new File(fileName));
System.out.println("Data entered");
}
只是想让社区知道我找到了解决方案。在写入文件时,openCSV 不是一个好的选择。我正在使用 Apache commons csv,它工作正常。
这是更新后的工作代码。
String filePath = Constants.CONFIGURATION_PATH + "csv\" + Constants.RUNNAME + ".csv";
File source=new File(Constants.CONFIGURATION_PATH + "csv\" +"MyTemplate.csv");
CSVPrinter writer = new CSVPrinter(new FileWriter(filePath), CSVFormat.EXCEL);
writer.printRecord("First Name","Last Name","Email");
for (int i = 0; i < numberOfRecords; i++) {
List<String> nextLine = new ArrayList<>();
nextLine.add(Commons.getRandomAlphabet(6).toUpperCase());
nextLine.add(Commons.getRandomAlphabet(6).toUpperCase());
nextLine.add(Commons.getRandomAlphanumeric(10).toUpperCase() + "@email.com");
writer.printRecord(nextLine);
//data.add(new String[]{ firstName, lastName, emailAddress });
}
writer.flush();
writer.close();