如何使用 Java 8 作为数据库访问 Excel

Uda*_*day 5 java excel ojdbc

我正在使用 Java 8。

当我尝试通过 jdbc-odbc 访问 Excel 数据(基本上这是我的测试数据)时,我收到“java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver”

而且我还试图以非 DSN 的身份访问数据。

我上网冲浪,发现 Oracle 弃用了对 jdbc-odbc 的支持。

那么使用 Java 访问这些 Excel 数据的最简单方法是什么?

Connection con=null;
    Statement stmt=null;
    ResultSet rs=null;
    String query = "select TestScript from [TS 360 Scripts$]";

    try
    {
        Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
        con = DriverManager.getConnection("jdbc:odbc:;Driver={Microsoft Excel Driver(*.xlsx)};DBQ=D://TS 360 Script with Count.xlsx");


        stmt=con.createStatement();
        rs=stmt.executeQuery(query);
        while(rs.next())
        {
            System.out.println(rs.getString("TestScript"));
        }

        con.close();
        rs.close();
        stmt.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

Din*_*tha 2

不要将 Excel 文件作为数据库访问。而是使用 jar,例如 Apache POI For Microsoft Documents

下载链接:Apache POI For MS Docs-Jar

使用此 API 的示例:

注意:在运行之前,您必须将 apache poi jar 添加到构建路径

package com.dd.selenium;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;


public class PerformDDTest {

    private static HSSFWorkbook xlWBook;

    private static HSSFSheet xlSheet;

    private static HSSFRow xlRow;

    private static HSSFCell xlCell;

    private static String filePath = "/home/dinesh/";

    private static String fileName = "test.xls";

    private static String url = "http://store.demoqa.com/"; 

    private static String result = "Pass";


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

        try {
            FileInputStream xlFile = 
                    new FileInputStream(filePath+fileName);

            //Access the required test data sheet

            xlWBook =  new HSSFWorkbook(xlFile);

            xlSheet = xlWBook.getSheet("Sheet1");

            xlRow = xlSheet.getRow(1);

            String username = xlRow.getCell(1).getStringCellValue();
            String password = xlRow.getCell(2).getStringCellValue();

            FirefoxDriver driver = new FirefoxDriver();

            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

            driver.manage().window().maximize();


            driver.get(url);

            driver.findElement(By.xpath(".//*[@id='account']/a")).click();

            driver.findElement(By.id("log")).sendKeys(username);
            driver.findElement(By.id("pwd")).sendKeys(password);

            driver.findElement(By.id("login")).click();



            driver.findElement(By.xpath(".//*[@id='account_logout']/a")).click();

            Thread.sleep(5000);

            driver.quit();

            setResultCell();


            FileOutputStream fout = new FileOutputStream(filePath+fileName);

            xlWBook.write(fout);

            fout.flush();
            fout.close();



        } catch (IOException e) {
            // TODO Auto-generated catch block
            result = "Failed";
            setResultCell();
            e.printStackTrace();
        }



    }


    private static void setResultCell() {
        xlCell = xlRow.getCell(3, xlRow.RETURN_BLANK_AS_NULL);

        if(xlCell == null ){
            xlCell = xlRow.createCell(3);
            xlCell.setCellValue(result);
        }else{
            xlCell.setCellValue(result);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)