在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Java语言基础 → java excel操作示例代码

java excel操作示例代码

Java语言基础

下载此实例
  • 开发语言:Java
  • 实例大小:0.07M
  • 下载次数:27
  • 浏览次数:367
  • 发布时间:2015-04-09
  • 实例类别:Java语言基础
  • 发 布 人:liuchangxin
  • 文件格式:.rar
  • 所需积分:2
 相关标签:

实例介绍

【实例简介】
【实例截图】

【核心代码】

package ExecutionEngine;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.apache.log4j.xml.DOMConfigurator;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.impl.xb.xmlconfig.ConfigDocument.Config;

















import Utils.ExcelUtils;
import Config.ActionKeywords;
import Config.Constants;




public class DriverScript {

	
   
	public static ActionKeywords actionKeywords;
    public static String sPageObject;
    public static String  method[];
    public static int iTestStep;
    public static int iTestLastStep;
    public static int iTestDataRow;
    public static String sTestCaseID;
    public static String sRunMode;
    public static String sData;
    public static boolean bResult=true;
    public static String queryresult;
    public static String sTestStep;
    public static String sDesctipt;
    public static String sTestName;
    public static String sFindByType;
    public static String sTestObject;
    public static String sOperation;
    public static String sexpected;
    public static String TestSuiteName;
    public static String TestAction_Value;
    

    
    public static void main(String[] args) throws Exception {
    	//results_statistical();
    	//Clean_Data ();
    	//Run_Test_Suite("配資1000审核成功");
    Task_scheduling();
    	//Record_TestResult() ;
	}
    
    /**
     * 备份文件并移除Sheet Reports
     * @throws IOException
     */
  public static void Clean_Data () throws IOException {
	  
	  SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
	  //生成时间
	  String  dateString = formatter.format(new Date());
	  String FileName_TestData=System.getProperty("user.dir") "\\src\\Reports\\Reports.xlsx";
	  String dst=System.getProperty("user.dir") "\\src\\Reports\\" dateString ".xlsx";
	  FileInputStream ExcelFile3;
	 
      ExcelFile3 = new FileInputStream(FileName_TestData);
	   XSSFWorkbook  ExcelWBook3 = new XSSFWorkbook(ExcelFile3);
	   
	   XSSFSheet ReportSheet=ExcelWBook3.getSheet("Report");
		   if (ReportSheet!=null){
			   ExcelUtils.copyFile(FileName_TestData,dst);
			    for (int i = 0; i < ExcelWBook3.getNumberOfSheets(); i  ) {
					   String sheetname=ExcelWBook3.getSheetAt(i).getSheetName();
						   if (sheetname.equals("Report")){
							   ExcelWBook3.removeSheetAt(i); 
							   FileOutputStream fileOut = new FileOutputStream(FileName_TestData);
							     ExcelWBook3.write(fileOut);
							     fileOut.close();
							     ExcelFile3.close();
						   }
				   }
			 }
	   
	  
		
	  
	  
    }
    
    public static void results_statistical2() throws IOException {
    	String FileName_TestData=System.getProperty("user.dir") "\\src\\Reports\\Reports.xlsx";
        FileInputStream ExcelFile3;
        ExcelFile3 = new FileInputStream(FileName_TestData);
        XSSFWorkbook  ExcelWBook3 = new XSSFWorkbook(ExcelFile3);
	    XSSFSheet ReportSheet=ExcelWBook3.getSheet("Report");
	    XSSFSheet ResultSheet=ExcelWBook3.getSheet("首页---数据统计");
	    XSSFRow Row;
	    boolean Result=true;
	    int  PASSNum=0;
	    int  FAILNum=0;
	    if (ReportSheet!=null){
	    	int Rows=ReportSheet.getLastRowNum();
	    	 for (int i=1;i<Rows;i  ){
	    		 String TestCaseName=ReportSheet.getRow(i).getCell(1).getStringCellValue();
	    		 String TestCaseName_Temp = "";
	    		 if(ReportSheet.getRow(i 1)!=null) {
	    			 TestCaseName_Temp=ReportSheet.getRow(i 1).getCell(1).getStringCellValue();
	    			 if (!TestCaseName.equals(TestCaseName_Temp)){
	    				 Result=ReportSheet.getRow(i).getCell(9).getBooleanCellValue();
		    			 if (Result==true){
		    				 PASSNum=PASSNum 1;
		    			 }
		    			 else {
		    				 FAILNum=FAILNum 1; 
		    			 }
	    		      }
	    			 
	    		 }
	    		
		    				
		    				
	    	 }
	    	 System.out.println("PASSNum:   " PASSNum "FAILNum:    " FAILNum);
	    	     Row=ResultSheet.getRow(1);
	    	      if (Row==null) {
	    	    	  Row=ResultSheet.createRow(1);  
	    	      }
	    		 if (Row.getCell(0)!=null){
	    			 Row.getCell(0).setCellValue(PASSNum);
		    		 Row.getCell(1).setCellValue(FAILNum); 
	    		 }
	    		 else {
	    			 Row.createCell(0).setCellValue(PASSNum);
	    			 Row.createCell(1).setCellValue(FAILNum);
	    		 }
	    }
	    System.out.println("PASSNum         :   " PASSNum "FAILNum              :    " FAILNum);
	     
	     FileOutputStream fileOut = new FileOutputStream(FileName_TestData);
	     ExcelWBook3.write(fileOut);
	     fileOut.close();
	     ExcelFile3.close();
    	
    }
    
    
    
    
    
    
    
    
    public static void results_statistical() throws IOException {
    	String FileName_TestData=System.getProperty("user.dir") "\\src\\Reports\\Reports.xlsx";
        FileInputStream ExcelFile3;
        ExcelFile3 = new FileInputStream(FileName_TestData);
        XSSFWorkbook  ExcelWBook3 = new XSSFWorkbook(ExcelFile3);
	    XSSFSheet ReportSheet=ExcelWBook3.getSheet("Report");
	    XSSFSheet ResultSheet=ExcelWBook3.getSheet("首页---数据统计");
	    XSSFRow Row;
	    int iTestCasePass=0;
	    int iTestCaseFail=0;
	    int iPass=0;
	    int iFail=0;
	    int iTestCase=0;
	    //取出最大行号
	    int Rows=ReportSheet.getLastRowNum();
				   for (int i=1;i<Rows 1;i  ){
					  boolean result= ReportSheet.getRow(i).getCell(9).getBooleanCellValue();
					  if (result==true){
						  iPass=iPass 1;
					  }
					  else {
						  iFail=iFail 1;
					  }
					 // System.out.println("iPass:   " iPass "iFail :     " iFail);
				   }
	   
		   for (int j=0;j<Rows;j  ){
			   String TestCaseName=ReportSheet.getRow(j).getCell(1).getStringCellValue();
			   String Temp=ReportSheet.getRow(j 1).getCell(1).getStringCellValue();
			    if (!TestCaseName.equals(Temp)){
			    	iTestCase=iTestCase 1;
			   // System.out.println("统计TestCase 数量" iTestCase);
			    }
		   }
	   iTestCasePass=iTestCase-iFail; 
	   iTestCaseFail=iFail;
	   Row=ResultSheet.getRow(1);
	   if (Row!=null){
		   Row.getCell(0).setCellValue(iTestCasePass);
		   Row.getCell(1).setCellValue(iTestCaseFail);
		   Row.createCell(10).setCellValue(iPass);
		   Row.createCell(11).setCellValue(iFail);
	   }
	   else {
		   Row=ResultSheet.createRow(1);
		   Row.createCell(0).setCellValue(iTestCasePass);
		   Row.createCell(1).setCellValue(iTestCaseFail);
		   
		   Row.createCell(10).setCellValue(iPass);
		   Row.createCell(11).setCellValue(iFail);
		   
		 
		   
	   }
	   
	   FileOutputStream fileOut = new FileOutputStream(FileName_TestData);
	   ExcelWBook3.write(fileOut);
	   fileOut.close();
	   ExcelFile3.close();
    }
    
    
    
    
    public static void Record_TestResult() throws IOException {
    	String FileName_TestData=System.getProperty("user.dir") "\\src\\Reports\\Reports.xlsx";
        FileInputStream ExcelFile3;
        ExcelFile3 = new FileInputStream(FileName_TestData);
		XSSFWorkbook  ExcelWBook3 = new XSSFWorkbook(ExcelFile3);
	    XSSFSheet ReportSheet=ExcelWBook3.getSheet("Report");
	    XSSFRow Row;
	    if (ReportSheet==null) {
	    	ReportSheet=ExcelWBook3.createSheet("Report");
	    	Row=ReportSheet.createRow(0);
	    	Row.createCell(0).setCellValue("测试套件名称");
	    	Row.createCell(1).setCellValue("测试用例");
	    	Row.createCell(2).setCellValue("测试步骤");
	    	Row.createCell(3).setCellValue("测试描述");
	    	Row.createCell(4).setCellValue("定位方式");
	    	Row.createCell(5).setCellValue("测试对象名称");
	    	Row.createCell(6).setCellValue("测试对象实休");
	    	Row.createCell(7).setCellValue("执行操作");
	    	Row.createCell(8).setCellValue("测试数据");
	    	Row.createCell(9).setCellValue("测试结果");
	    	Row.createCell(10).setCellValue("测试结际结果");
	    	FileOutputStream fileOut = new FileOutputStream(FileName_TestData);
	        ExcelWBook3.write(fileOut);
	        fileOut.close();
	    }
	 int LastNum2=ReportSheet.getLastRowNum();
	 Row=ReportSheet.createRow(LastNum2 1);
	 Row.createCell(0).setCellValue(TestSuiteName);
	 Row.createCell(1).setCellValue(sTestCaseID); 
	 Row.createCell(2).setCellValue(sTestStep); 
	 Row.createCell(3).setCellValue(sDesctipt); 
	 Row.createCell(4).setCellValue(sFindByType); 
	 Row.createCell(5).setCellValue(sTestName); 
	 Row.createCell(6).setCellValue(sTestObject); 
	 Row.createCell(7).setCellValue(sOperation); 
     if (sData!=null){
    	 Row.createCell(8).setCellValue(sData); 
     }
     Row.createCell(9).setCellValue(bResult); 
     
     if (sOperation!=null && sOperation.contains("check")){
    	 Row.createCell(10).setCellValue(TestAction_Value); 
     }
     FileOutputStream fileOut = new FileOutputStream(FileName_TestData);
     ExcelWBook3.write(fileOut);
     fileOut.close();
     ExcelFile3.close();
    }
    
    
    public static void Task_scheduling() throws Exception {
    	Clean_Data ();
    	String FileName_TestData=System.getProperty("user.dir") "\\src\\TestSuite.xlsx";
        FileInputStream ExcelFile2;
        ExcelFile2 = new FileInputStream(FileName_TestData);
		XSSFWorkbook  ExcelWBook2 = new XSSFWorkbook(ExcelFile2);
	    XSSFSheet ExcelWSheet2=ExcelWBook2.getSheet("任务执行计划");
	    int LastNum2=ExcelWSheet2.getLastRowNum();
	    for (int j=1;j<LastNum2 1;j  ){
	    	XSSFRow Row =ExcelWSheet2.getRow(j);
	    	String TestSuite=Row.getCell(0).getStringCellValue();
	    	String TestSuiteMode=Row.getCell(1).getStringCellValue();
	    	if (TestSuiteMode.equals("Yes")) {
	    		Run_Test_Suite(TestSuite);
	    	}
	    }
	    
	    results_statistical();

    	
    }
    
    public static void  Run_Test_Suite(String SuiteName) throws Exception {
    	
    String FileName_TestSuite=System.getProperty("user.dir") "\\src\\TestSuite.xlsx";
    FileInputStream ExcelFile;
	try {
		ExcelFile = new FileInputStream(FileName_TestSuite);
		XSSFWorkbook  ExcelWBook = new XSSFWorkbook(ExcelFile);
		
	    XSSFSheet TestSuiteSheet=ExcelWBook.getSheet("测试套件 ");
	    int LastNum=TestSuiteSheet.getLastRowNum();
	    for (int i=1;i<LastNum 1;i  ){
	    	//获取测试套件中最大行号
	    	 XSSFRow Row =TestSuiteSheet.getRow(i);
	    	 TestSuiteName=Row.getCell(0).getStringCellValue();
	    	 //获取测试数据索引值
	    	 String  TestData=Row.getCell(1).getStringCellValue();
          	 int TestDataIndex=Integer.parseInt(TestData);
          	 //获测试用例名称
          	sTestCaseID=Row.getCell(2).getStringCellValue();
          	 //运行测试用例(根据测试套件指定的用例名称和数据索引值执行)
          	  if (SuiteName.equals(TestSuiteName)){
          	  Run_Test_Case(sTestCaseID,TestDataIndex);
          	  System.out.println("测试用例名称" sTestCaseID "测试套件名称    ==" TestSuiteName);
	          }
	    }
	    ExcelFile.close();
	} catch (FileNotFoundException e) {
		
		e.printStackTrace();
	}
 	
 	
    }
    
    
   
    
    public static void Run_Test_Case (String SheetName,int SetDataIndex) throws Exception {
    	//设置用例文件名称 
    	String FileName_TestCase=System.getProperty("user.dir") "\\src\\TestCase\\TestCase.xlsx";
    	//打开用例
    	 Utils.ExcelUtils.setExcelFile(FileName_TestCase);
    	 //加载配置文件
    	 DOMConfigurator.configure("log4j.xml");
    	 //统计用例行数
    	int iTeseCaseRows=Utils.ExcelUtils.getRowCount(SheetName);
    	//依次读取各单元格中的内容
    	for (iTestStep=1;iTestStep<iTeseCaseRows;iTestStep  ) {
    		sTestStep=Utils.ExcelUtils.getCellData(iTestStep, Constants.Col_TestStep_ID, SheetName);
    		sDesctipt=Utils.ExcelUtils.getCellData(iTestStep, Constants.Col_TestStep_Descript, SheetName);
    		sTestName=Utils.ExcelUtils.getCellData(iTestStep, Constants.Col_TestStep_TestContrl, SheetName);
    		sFindByType=Utils.ExcelUtils.getCellData(iTestStep,Constants.Col_TestStep_FindByType, SheetName);
    		sTestObject=Utils.ExcelUtils.getCellData(iTestStep,Constants.Col_TestStep_TestObject, SheetName);
    		sOperation=Utils.ExcelUtils.getCellData(iTestStep, Constants.Col_TestStep_Operation, SheetName);
    		sData=Utils.ExcelUtils.getCellData(iTestStep, Constants.Col_TestStep_Data, SheetName);
    		
    		if (sData.contains("数化_")){
    		  //Split sData;
    		  String tem[]=sData.split("_");
          	  String temp_cell=tem[1];
          	  int cell= Integer.parseInt(temp_cell);
          	  String FileName_TestData=System.getProperty("user.dir") "\\src\\TestData\\TestData.xlsx";

          	 FileInputStream ExcelFile = new FileInputStream(FileName_TestData);
          	 XSSFWorkbook  ExcelWBook = new XSSFWorkbook(ExcelFile);
          	 XSSFSheet ExcelWSheet=ExcelWBook.getSheet(SheetName);
          	 XSSFRow Row =ExcelWSheet.getRow(SetDataIndex);
          	 sData=Row.getCell(cell).getStringCellValue();
          	 ExcelFile.close();
    	    }
    		System.out.println("测试用例名称==" sTestCaseID "测试步骤     =" sTestStep "测试对象    =" sTestName "操作方法   =" sOperation "测试数据  =" sData );
    		execute_Actions(sOperation);
    		
    		Record_TestResult();
    		if (bResult==false){
    			ActionKeywords.Screenshot("",TestSuiteName "--" sTestCaseID "-----" sTestStep );
    			
    			break;
    		}
    	}
    }

    
    
    
    private static void execute_Actions(String OperationName) throws Exception {
    	
   	    Thread.sleep(500);

    	   
    	   switch (OperationName) {
		   case "scrollIntoWebElement":
			ActionKeywords.scrollIntoWebElement(sFindByType, sTestObject, sData);
			break;
			
		   case "click":
				ActionKeywords.click(sFindByType, sTestObject, sData);
				break;
				
		   case "contextclick":
				ActionKeywords.contextclick(sFindByType, sTestObject, sData);
				break;	
				
		   case "doubleClick":
				ActionKeywords.doubleClick(sFindByType, sTestObject, sData);
				break;
				
		   case "openBrowser":
				ActionKeywords.openBrowser(sFindByType, sTestObject, sData);
				break;
				
		   case "switchtowindows":
				ActionKeywords.switchtowindows(sFindByType, sTestObject, sData);
				break;
				
		   case "navigate":
				ActionKeywords.navigate(sFindByType, sTestObject, sData);
				break;
				
		   case "alert":
				ActionKeywords.alert(sFindByType, sTestObject, sData);
				break;
				
		   case "click_table":
				ActionKeywords.click_table(sFindByType, sTestObject, sData);
				break;
				
		   case "select":
				ActionKeywords.select(sFindByType, sTestObject, sData);
				break;
				
		   case "input":
				ActionKeywords.input(sFindByType, sTestObject, sData);
				break;
				
		   case "closeBrowser":
				ActionKeywords.closeBrowser(sFindByType, sTestObject, sData);
				break;
		   case "checkText":
				ActionKeywords.checkText(sFindByType, sTestObject, sData);
				break;
		   case "check_table_text":
				ActionKeywords.check_table_text(sFindByType, sTestObject, sData);
				break;
		   case "querySQL":
				ActionKeywords.querySQL(sFindByType, sTestObject, sData);
				break;
		  
		default:
			break;
		}
  
    }
  
}

标签:

实例下载地址

java excel操作示例代码

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警