英文:
Java Selenium - CSV UTF-8 format file creation is not working for Salesforce
问题
我面临着一个独特的挑战。我的应用程序基于SalesForce,我需要创建一个列表(名字,姓氏,电子邮件),然后上传到应用程序中。我正在使用OpenCSV生成文件并上传,但应用程序无法识别。
手动操作步骤:
- 下载开发人员提供的模板,格式为.xls Unicode格式。
- 用Microsoft 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");
}
英文:
I am facing a unique challenge. My application is based on SalesForce, where I have to create a list (First Name, Last Name, Email) and upload in the application. I am using OpenCSV to generate the file and uploading but application is not recognizing.
Manually How it works:
- Download template provided by developers which is .xls unicode format
- Open with Microsoft Excel
- Populate data and save as CSV UTF-8 (Comma delimited)(*.csv)
It works perfectly fine while modifying current template manually.
Automation Approach:
- With help of Java and OpenCSV copying the file to use the same template
- Populate data
- Flush file
- Copy file to be in CSV format
But when I upload the file, application is not accepting. Not getting any error message as well.
Please find below code that I have currently. Any help will be appreciated.
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");
}
答案1
得分: 0
I have found a solution. While writing the file openCSV is not a good option. I am using Apache commons csv and it's working fine.
Here is updated working code.
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);
}
writer.flush();
writer.close();
英文:
Just wanted to let the community know that I have found a solution. While writing the file openCSV is not a good option. I am using Apache commons csv and it's working fine.
Here is updated working code.
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();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论