2016-12-09 5 views
0

私は更新したい特定のセルの値をjava excel APIを使用して、私は蛇のコードスニペットを使用していますが、セルの値が更新されていません。JavaのsetCellValue APIを使用してセルの値を更新できません

public void update_excel() throws IOException 
    { 
     FileInputStream excel_file = new FileInputStream(Test.excelfilepath); 
     XSSFWorkbook workbook = new XSSFWorkbook(excel_file); 
     XSSFSheet excel_sheet = workbook.getSheetAt(1); 
     XSSFCell cell1=excel_sheet.createRow(1).createCell(2); 
     cell1.setCellValue("abc"); 
     excel_file.close(); 
    } 
+2

;'と 'workbook.close();' – XtremeBaumer

+0

私は見ミスを犯しました。 'OutputStream out = Files.newOutputStream(path、StandardOpenOption.CREATE_NEW) ' – XtremeBaumer

+0

excel_fileは入力ストリームであり、書き込みAPIは出力ストリームを必要とするため、これを行うことはできません。 –

答えて

0

以下のコードを使用してセル値を更新します。

あなたは `workbook.write(excel_file)を呼び出す必要はあり価値保存するための
FileInputStream excel_file= new FileInputStream(new File("Test.excelfilepath)); //Read the spreadsheet that needs to be updated 
HSSFWorkbook wb = new HSSFWorkbook(excel_file); //Access the workbook 
HSSFSheet worksheet = wb.getSheetAt(1); //Access the worksheet, so that we can update/modify it. 
Cell cell = null; // declare a Cell object 
cell = worksheet.getRow(1).getCell(2); // Access the second cell in second row to update the value 
cell.setCellValue("OverRide Value"); // Get current cell value value and overwrite the value 
excel_file.close(); //Close the InputStream 
FileOutputStream output_file =new FileOutputStream(new File("Test.excelfilepath)); //Open FileOutputStream to write updates 
wb.write(output_file); //write changes 
output_file.close(); //close the stream 
+0

wbは閉じていません – XtremeBaumer

関連する問題