在java中将自动值导出到excel文件

问题描述 投票:0回答:2

我在eclipse中使用selenium webdriver和Java一起工作。我需要将值导出到excel文件。我能够写入excel文件,但无法将特定值导出到excel文件。

以下是获取值的自动化过程:

public void AbbeyNational (String AbbeyNationalURL, String AN_AccCookiesButton, String AN_MortgageTabButton, String AN_ExistingCustomerButton, String AN_FollowRateButton, String AN_SVRButton, String AN_RateFld)throws InterruptedException {

    driver.get(AbbeyNationalURL);
    driver.findElement(By.xpath(AN_AccCookiesButton)).click();
    driver.findElement(By.linkText(AN_MortgageTabButton)).click();
    driver.findElement(By.xpath(AN_ExistingCustomerButton)).click();
    driver.findElement(By.xpath(AN_FollowRateButton)).click();
    driver.findElement(By.xpath(AN_SVRButton)).click();
    String a = driver.findElement(By.cssSelector(AN_RateFld)).getText();
    String AN_Rate = a.substring(54,58);
    System.out.println(AN_Rate);
}

“AN_Rate”变量在自动化后保存该值。该值打印到控制台,但我需要使用自动化将值导出到excel文件。谁能帮我这个?

另外,这是我写入excel文件的代码:

public void writeToExcel(String AN_Rate) throws IOException{
    File file = new File(filePath+"\\"+fileName);
    XSSFWorkbook IRWorkbook = new XSSFWorkbook();
    XSSFSheet Sheet0 = IRWorkbook.createSheet();
    Sheet0.createRow(0).createCell(0).setCellValue(AN_Rate);
    try {
        FileOutputStream fos = new FileOutputStream(file);
        IRWorkbook.write(fos);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
java selenium-webdriver automation
2个回答
0
投票

您可以将此代码用于大量数据:

public class ExcelWriter {

// LinkedHashMap to maintain the insertion order
public static Map<String, String> dataMap = new LinkedHashMap<>();
public static String filePath = "D:\\ECLIPSE-WORKSPACE\\playground\\src\\main\\resources";
public static String fileName = "demo-data";

public static void main(String[] args) throws IOException {

    String AN_Rate = "Data-1";
    String BN_Rate = "Data-2";
    String CN_Rate = "Data-3";

    dataMap.put("AN_Rate", AN_Rate);
    dataMap.put("BN_Rate", BN_Rate);
    dataMap.put("CN_Rate", CN_Rate);

    writeToExcel(dataMap, filePath, fileName);
}

public static void writeToExcel(Map<String, String> dataMap, String filePath, String fileName) throws IOException {
    File file = new File(filePath + "\\" + fileName + ".xlsx");
    if (file.exists()) {
        file.delete();
    }
    file.createNewFile();
    XSSFWorkbook IRWorkbook = new XSSFWorkbook();
    XSSFSheet sheet = IRWorkbook.createSheet();
    List<String> headers = dataMap.keySet().stream().collect(Collectors.toList()); // all key values in a list
    List<String> data = new ArrayList<>(dataMap.values()); // all data in a list

    setHeadersAndFillData(sheet, headers, data); // filling excel sheet with headers and corresponding data

    try {
        FileOutputStream fos = new FileOutputStream(file);
        IRWorkbook.write(fos);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

public static void setHeadersAndFillData(XSSFSheet sheet, List<String> headers, List<String> data) {
    int headersSize = headers.size();
    int dataSize = headers.size();
    Row headerRow = sheet.createRow(0);
    Row dataRow = sheet.createRow(1);
    setCells(headers, headersSize, headerRow);
    setCells(data, dataSize, dataRow);
}

private static void setCells(List<String> cellData, int headersSize, Row row) {
    for (int rn = 0; rn < headersSize; rn++) {
        row.createCell(rn).setCellValue(cellData.get(rn));
    }
}

}


0
投票

只需使用以下命令编辑方法writeToExcel:

替换:File file = new File(filePath+"\\"+fileName);并添加了文件扩展名

 File file = new File(filePath+"\\"+fileName + ".xlsx");

并且该值将导出到excel文件。

如果存在大量数据,则应将所有值存储在静态映射中,而不是一次性将所有值导出到excel。

© www.soinside.com 2019 - 2024. All rights reserved.