2017-05-16 9 views
1

3.16 のEclipse IDEネオン3 セレン3.4(それはこの場合には重要ではないということ)のApache POI setCellData実際に設定していないセルデータ

私は、Excelスプレッドシートへの書き込み値との問題を抱えています値を読み取る。ここで

は私がハイレベルで何をしたいです:

  1. は、Excelは、1列1(私たちは0から始まるインデックスを使用している)
  2. 何をリードバックを行に
  3. 書き込みを提出開きその細胞に書き込まれました。

セルに値「B2」が含まれています。 setCellData()関数では、セルに "Hello World"を書き込んで、その関数がセルの内容を返すようにします。私はまた、指定されたセルの内容を読み込む別の関数を持っています。私は次のような出力を得る

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 

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; 

public class Udemy_Excel_Driven { 

    public static XSSFWorkbook wb; 
    public static XSSFSheet sheet; 
    public static XSSFRow row; 
    public static XSSFCell cell; 
    public static FileInputStream fis; 

    public static void main(String[] args) throws IOException, Exception 
    { 

     System.out.println("Before cell edit value is:"); 

     System.out.println(getCellData(1,1)); 



     String value = setCellData(1,1,"Hello World"); 

     System.out.println("What's the value after setting it with setCellData()?");     
     System.out.println(value); 
     System.out.println("What's the value using getCellData()?"); 
     System.out.println(getCellData(1,1)); 
    } 

    public static String getCellData(int rowNum, int colNum) throws IOException 
    { 

     /* 
     * Hierarchy of excel data: 
     * 
     * Workbook - take control of this 
     * Sheet - pick up the sheet of the workbook 
     * Row - pick the row 
     * Column - after picking the row, select the column 
     * Value - grab the value from the cell 
     * 
     */ 


     //0. = identify the path to the excel file in the system. 
     fis = new FileInputStream("C:\\data.xlsx"); 

     //1. Create a new XSSFWorkbook object. You need to pass in a FileInputStream object into it, which you created earlier. 
     wb = new XSSFWorkbook(fis); 

     //2. Get the sheet in the workbook. Create a new XSSFsheet object and set it to the sheet in the workbook 
     // Access the workbook method "getSheet" and pass in the name of the sheet 
     sheet = wb.getSheet("script"); 

     //3. Get the row and column. We are going to access the data from row 2 column 2. And remember the indices start at 0. 
     row = sheet.getRow(rowNum); 
     cell = row.getCell(colNum); 
     //get the value specified in the row and cell 
     return cell.getStringCellValue(); 
    } 



    public static String setCellData(int rowNum, int colNum, String data) throws IOException 
    { 

     fis = new FileInputStream("C:\\data.xlsx"); 
     wb = new XSSFWorkbook(fis); 
     sheet = wb.getSheet("script"); 
     row = sheet.getRow(rowNum); 
     cell = row.getCell(colNum); 

      cell.setCellValue(data); 
      String cellData = cell.getStringCellValue(); 
     return cellData; 

    } 

:私は、次のコードを実行すると

Before cell edit value is: 
B2 
What's the value after setting it with setCellData()? 
Hello World 
What's the value using getCellData()? 
B2 

Here's the contents of the data.xlsx file:

を私は私が開いてから書き込みが実際に発生したとは思いません指定されたセルに「Hello World」という文字列がありませんでした。この問題に対する回答はありますか?

+0

"Hello World"はExcelで書かれましたか?それとも、それを読んで問題を抱えていますか? –

+1

だから、 'wb = new XSSFWorkbook(fis);は何をしているのですか? 'fis 'のデータから' RAM'に新しい 'XSSFWorkbook'を作成します。そこで、あなたの 'getCellData'はファイル' C:\ data.xlsx'から 'fis'のデータから' RAM'に新しい 'XSSFWorkbook'を作成し、この' XSSFWorkbook'からデータを取得します。また、 'setCellData'は' C:\ data.xlsx'ファイルの 'fis'のデータから新しい' XSSFWorkbook'を 'RAM'に作成し、' RAM'の 'XSSFWorkbook'にデータをセットします。しかし、データはどこにでも 'C:\\ data.xlsx'というファイルに書き込まれます。 –

+0

今私はこの問題を認識しました。オブジェクトは作成され、操作され、実際にファイルに書き込むコードは一度もありません。新しいFileOutputStream関数を作成するコードをsetCellData関数に追加し、ファイルに書き戻しました。ファイルを読むと、特定のセルに「Hello World」と書かれています。 – user8022038

答えて

1

実際にファイルに書き込むコードの部分は表示されません。あなたはまた、この問題だけでなく、あなたが実装に興味があるかもしれない他の機能のために、このguideに相談することができます

FileOutputStream fileOut = new FileOutputStream("C:\\data.xlsx"); 
wb.write(fileOut); 
fileOut.close(); 

: それはこのような多かれ少なかれ見て何かをする必要があります。

+0

これはパズルの欠けた部分です(私はワークワークオブジェクトの情報を操作し、ファイル自体には書き込まないという説明とともに)。ありがとうございました! – user8022038

関連する問題