2015-12-14 139 views
0

我是新來的硒,仍在學習。我試圖用兩個測試在TestNG中編寫一個程序,以便從Excel中讀取數據並使用Apache POI編寫代碼。我在同一個文件夾中有一個XLS和一個XLSX文件,並試圖使用這兩個文件。我期待在testng.xml中配置excels的路徑和名稱。當我執行我的第一個測試來讀取數據時,表名正在被讀爲xml。以下是我的testng.xml用testNG讀取Excel中的excel數據

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="Suite" parallel="tests" thread-count="2"> 
<parameter name="filePath" value="C:\\Technologies\\Selenium\\WebDriver\\DataFiles\\ExcelFiles"></parameter> 
    <test name="Xlsx"> 
    <parameter name="fileName" value="12142015_sx.xlsx"></parameter> 
    <parameter name="sheetName" value="xlsx_1"></parameter> 
    <classes> 
     <class name="handlefiles.handleExcel"/> 
    </classes> 
    </test> 
    <test name="Xls"> 
    <parameter name="fileName" value="12142015_s.xls"></parameter> 
    <parameter name="sheetName" value="xls_1"></parameter> 
    <classes> 
     <class name="handlefiles.handleExcel"/> 
    </classes> 
    </test> <!-- Test --> 
</suite> <!-- Suite --> 

下面是java類代碼。

package handlefiles; 

import java.io.*; 

import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
import org.testng.annotations.*; 

public class handleExcel { 

    @BeforeTest 
    @Parameters({"filePath", "fileName", "sheetName"}) 
    public void readExcel(String filePath, String fileName, String sheetName) throws IOException{ 
     // set the file path 
     File eFile = new File(filePath+"\\"+fileName); 
     //print the file path 
     System.out.println("File is at: "+eFile); 

     FileInputStream inputstream = new FileInputStream(eFile); 
     Workbook wb = null; 

     // Read and publish extension 
     String extensionName = fileName.substring(fileName.indexOf(".")); 
     System.out.println("File type is: "+extensionName); 

     //Read the file 
     if(extensionName.equalsIgnoreCase(".xlsx")){ 
      wb = new XSSFWorkbook(inputstream); 
     }else if(extensionName.equalsIgnoreCase(".xls")){ 
      wb = new HSSFWorkbook(inputstream); 
     } 
     //Read the sheet name 
     Sheet wbSheet = wb.getSheet(sheetName); 
     System.out.println("Sheet Name is: "+wbSheet); 

    } 

    @Test(priority=1) 
    public void readData(Sheet wbSheet) { 

    // Get the row count 
      int rowCount = wbSheet.getLastRowNum() - wbSheet.getFirstRowNum(); 

      // print data from excel 
      for (int i=0; i<rowCount-1; i++){ 
       Row row = wbSheet.getRow(i); 
       for (int j=0; j<row.getLastCellNum(); j++){ 
        System.out.println(row.getCell(j).getStringCellValue()+"||"); 
       } 
      } 
    } 
} 

當我在Eclipse中運行這個,我看到下面exception-

org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF) 
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:128) 
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:112) 
    at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:300) 
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:400) 
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:381) 
    at handlefiles.handleExcel.readExcel(handleExcel.java:36) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86) 
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514) 
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215) 
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142) 
    at org.testng.TestRunner.beforeRun(TestRunner.java:656) 
    at org.testng.TestRunner.run(TestRunner.java:624) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366) 
    at org.testng.SuiteRunner.access$000(SuiteRunner.java:39) 
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:400) 
    at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64) 
    at java.util.concurrent.FutureTask.run(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
+0

您確定.xls文件包含2007年以前的excel內容嗎? – Sebastien

+0

這是屬性的文件類型 - Microsoft Excel 97-2003工作表(.xls) – Naresh

回答

2

根據堆棧跟蹤POI認爲您正嘗試使用POI的XLS代碼(而不是XLSX代碼)打開XLSX文件。也許文件擴展名是錯誤的,該文件是「.xls」,但應該是「.xlsx」或文件已損壞等(或者您可能找到了一個錯誤)。

我建議採用POI的WorkbookFactory代替:

廠用於創建適當的種類的工作簿(它是 HSSFWorkbookXSSFWorkbook),通過自動檢測從供給 輸入。

0

刪除以下IF條件和

if(extensionName.equalsIgnoreCase(".xlsx")){ 
      wb = new XSSFWorkbook(inputstream); 
     }else if(extensionName.equalsIgnoreCase(".xls")){ 
      wb = new HSSFWorkbook(inputstream); 
     } 

添加這段代碼

wb = new XSSFWorkbook(inputstream); 

我曾經用過,我從來沒有告訴我正在使用哪個擴展。

+0

我試過這個。異常已更改,但工作表名仍以XML格式讀取。下面是testNG trace - org.testng.TestNGException: 方法readData需要1個參數,但在@Test註釋中提供了0個參數。 控制檯顯示如下。 圖紙名稱爲:名稱:/xl/worksheets/sheet1.xml - 內容類型:application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet + xml 圖紙名稱爲:名稱:/xl/worksheets/sheet1.xml - 內容類型:application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet + xml – Naresh

+0

@Naresh,我看到你已經接受了這個答案,但是你的評論讓我覺得這並沒有回答你的問題。你偶然接受了錯誤的答案嗎?謝謝。 – mfulton26

+0

@ mfulton26感謝您的注意。它可能是偶然發生的,而不是故意的。我已糾正它。 – Naresh