栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java+jxl

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java+jxl

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class ZhuaShuJu {
public static WebDriver driver;
public static WebDriver instance = null;

@Test
    public void JDPrice(int row,String jd_sku) throws WriteException, IOException, BiffException {

    //driver=getWebDriver("webdriver.chrome.driver", "src/test/Driver/chromedriver.exe");
    System.setProperty("webdriver.chrome.driver", "src/test/Driver/chromedriver.exe");
        driver = new ChromeDriver();
       driver.manage().window().maximize();
        String url="https://i-item.com/"+sku+".html";
        driver.get(url);
        try {
            Thread.sleep(1000);
            String text=driver.findElement(By.xpath("/html/body/div[6]/div/div[2]/div[3]/div/div[1]/div[2]/span[1]/span[2]")).getText();
            WriteColumn(row, text);
        }catch(Exception e){
            System.out.println("有异常"+e);
        }
    driver.close();

    }
    //jxl读取excel数据,可以先用JXL创建Excel,再用这个EXCEL写入数据,中间出现过写入EXCEL为空的问题
    @Test
public  void readColumn() throws Exception {
    File file = new File("E:\javaproject\UiTest003\src\test\java\com\supplier\Date\jxlJD.xls");
    InputStream inputStream = new FileInputStream(file.getAbsoluteFile());
    Workbook workbook = Workbook.getWorkbook(inputStream);
    Sheet sheet = workbook.getSheet(0);
    int rows = sheet.getRows();
    int columns = sheet.getColumns();
    for (int row = 1; row < 4; row++) {
        Cell cell = sheet.getCell(0, row);
        String jd_sku= cell.getContents();
        System.out.println(jd_sku);
        JDPrice(row,jd_sku);

    }
        workbook.close();
        inputStream.close();


}
//jxl向已存在的EXCEL写入数据
@Test
public void WriteColumn(int row,String content) throws IOException, BiffException, WriteException {
    File file = new File("E:\javaproject\UiTest003\src\test\java\com\supplier\Date\jxlJD.xls");
    Workbook workbook = Workbook.getWorkbook(file);
    WritableWorkbook book= Workbook.createWorkbook(file, workbook);
    Sheet sheet = book.getSheet(0);
    Label label = new Label(1, row, content);
    ((WritableSheet) sheet).addCell(label);
    book.write();
    workbook.close();
    book.close();

}
//单例模式

public static WebDriver getWebDriver(String browserDriverType,String browserDriverPath) {
    if(instance == null) {

        if("webdriver.chrome.driver".equalsIgnoreCase(browserDriverType)) {
            System.setProperty(browserDriverType, browserDriverPath);
            instance = new ChromeDriver();

        }else if ("webdriver.ie.driver".equalsIgnoreCase(browserDriverType)) {
            System.setProperty(browserDriverType, browserDriverPath);
            instance = new InternetExplorerDriver();

        }else if ("webdriver.gecko.driver".equalsIgnoreCase(browserDriverType)) {
            System.setProperty(browserDriverType, browserDriverPath);
            instance = new FirefoxDriver();
        }
    }
    return instance;
}
//新建Excel并写入数据

@Test
public void JxiWrite() throws IOException, WriteException {
    File xlsFile = new File("C:\Users\win-10\Desktop\jxl.xls");
    // 创建一个工作簿
    WritableWorkbook workbook = Workbook.createWorkbook(xlsFile);
    // 创建一个工作表
    WritableSheet sheet = workbook.createSheet("sheet1", 0);
    for (int row = 0; row < 10; row++)
    {
        for (int col = 0; col < 10; col++)
        {
            // 向工作表中添加数据
            sheet.addCell(new Label(col, row, "zifeiy" + row + "," + col));
        }
    }
    workbook.write();
    workbook.close();
}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/583481.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号