2017-09-15 7 views
0

私は、次のコードを試みたが、提案のproperly.Anyタイプが動作していないが以前のデータを消去せずにExcelシートにデータを書き込む方法は?

public static void main(String[] args) throws EncryptedDocumentException, InvalidFormatException, IOException { 
InputStream inp = new FileInputStream("C:\\Documents and Settings\\Administrator\\Desktop\\Read Data.xlsx"); 
Workbook wb = WorkbookFactory.create(inp); 
Sheet sheet = wb.getSheetAt(0); 
Row row = sheet.getRow(1); 
Cell cell = row.getCell(0); 
cell.setCellType(cell.CELL_TYPE_STRING); 
String cellContents = cell.getStringCellValue(); 
//Modify the cellContents here 
// Write the output to a file 
//cell.setCellValue(cellContents); 
FileOutputStream fileOut = new FileOutputStream("C:\\Documents and Settings\\Administrator\\Desktop\\Read Data.xlsx"); 
sheet.createRow(1).createCell(1).setCellValue(cellContents); 
wb.write(fileOut); 
fileOut.close(); 
System.out.println(cellContents); 
} 

答えて

0

を理解されるであろうあなたはどの、あなたの問題は、あなたがしている既存の行を再作成され、この

InputStream inp = new FileInputStream("wb.xls"); 
Workbook wb = WorkbookFactory.create(inp); 
Sheet sheet = wb.getSheetAt([sheet index]); 
Row row = sheet.getRow([row index]); 
Cell cell = row.getCell([cell index]); 
String cellContents = cell.getStringCellValue(); 
//Modify the cellContents here 
// Write the output to a file 
cell.setCellValue(cellContents); 
FileOutputStream fileOut = new FileOutputStream("wb.xls"); 
wb.write(fileOut); 
fileOut.close(); 
+0

いいえ、その動作していません。以前のデータが消去されました.. –

0

を試すことができます既にそこに値を一掃される:

Row row = sheet.getRow(1); 
.... 
sheet.createRow(1).createCell(1).setCellValue(cellContents); 

最初のコールが2行をフェッチし、第二の一つは、それを払拭し、新しいものを作成し、

あなたがする必要があることは、すでにフェッチされている既存の行を再利用するために2番目の行を変更することだけです。

(その他の修正と....)のようなもの:

DataFormatter formatter = new DataFormatter(); 
Workbook wb = WorkbookFactory.create(inp); 
Sheet sheet = wb.getSheetAt(0); 
Row row = sheet.getRow(1); 
Cell cell = row.getCell(0); 
String cellContents = formatter.formatCellValue(cell); 

//Modify the cellContents here 

row.createCell(1).setCellValue(cellContents); 
FileOutputStream fileOut = new FileOutputStream("C:\\Documents and Settings\\Administrator\\Desktop\\Read Data.xlsx"); 
wb.write(fileOut); 
fileOut.close(); 
関連する問題