2016-12-19 5 views
0

私は、POIを使ってExcelにデータを書き込む関数を書いています。しかし、現時点では、パラメータにデータが書き込まれる行と列を指定する必要があります。この関数を一般的にして、特定のテストケースの次の列に自動的にデータを書き込む必要があり、テストケースが変更されたときにその行を変更する必要があります。Excelで書く機能

public void WritetoExcel(String filepath,String DatatoWrite,int RowNum, int ColNum, int sheetnumber) throws Exception 
    { 
     FileInputStream ExcelFile = new FileInputStream(filepath); 
     ExcelWBook = new XSSFWorkbook(ExcelFile); 
     ExcelWSheet = ExcelWBook.getSheetAt(sheetnumber); 

     try{ 
      Row = ExcelWSheet.getRow(RowNum); 
      Cell = Row.getCell(ColNum, org.apache.poi.ss.usermodel.Row.RETURN_BLANK_AS_NULL); 
      if (Cell == null) { 
       Cell = Row.createCell(ColNum); 
       Cell.setCellValue(DatatoWrite); 
       } else { 
        Cell.setCellValue(DatatoWrite); 
       } 
      FileOutputStream fileOut = new FileOutputStream(filepath); 
       ExcelWBook.write(fileOut); 
       fileOut.flush(); 
       fileOut.close(); 
      }catch(Exception e){ 
       throw (e); 
      } 
     } 

どうすればいいですか?

+0

テストケースを指定できますか? – XtremeBaumer

+0

テストケースは、トランザクションが実行されるたびにorderIDとそのステータスを書き込むことです。テストケースが再度実行されると、新しいオーダーIDとそれに対応するステータスが生成され、Excelに再度書き込むことになります –

+0

質問は私にはあまり明確ではありません。変更されたテストケースをどのように認識していますか? – JDC

答えて

0
public void WritetoExcel(String filepath, String DatatoWrite, int sheetnumber, boolean newTest) throws Exception { 
     FileInputStream ExcelFile = new FileInputStream(filepath); 
     ExcelWBook = new XSSFWorkbook(ExcelFile); 
     ExcelWSheet = ExcelWBook.getSheetAt(sheetnumber); 

     try { 
      if (newTest) { 
       Row = ExcelWSheet.createRow(RowNum++); 
       cellnum=0; 
      } 
      Cell = Row.creatCell(cellnum++); 
      Cell.setCellValue(DatatoWrite); 
      FileOutputStream fileOut = new FileOutputStream(filepath); 
      ExcelWBook.write(fileOut); 
      fileOut.flush(); 
      fileOut.close(); 
     } catch (Exception e) { 
      throw (e); 
     } 
    } 

私はそれを理解しているので、データを入力したい空白のExcelシートがあります。したがって、ブール値が真となるたびに新しい行を作成し、常に新しいセルを作成する必要があります。ブール値は、新しいテストケースが実行された場合にのみ真です。 rownumとcellnumをメンバー変数にし、起動時の値を0に設定します。このコードでは、空のExcelファイルが必要です。

関連する問題