我在excel中有一个文档,想使用java编辑或输入数据。我已经尝试过,但是我所能做的就是删除excel中的数据并替换我刚刚输入的数据。我将Netbeans用于我的编译器。我的输出是GUI / JFrame如何将数据插入到特定的列,行和表中?示例我想将其插入C12,E11,Sheet1等。
谢谢...
我只在Java Coding中进行自学,这就是为什么我了解不多的原因。
这只是xls的示例,而不是xlsx。你可以在这里参考。如果要在xlsx中编辑/输入数据(可以使用XSSFWorkbook和XSSFSheet)。
try {
//Get the excel file.
FileInputStream file = new FileInputStream(new File("(which sheet you want to modify or edit(put the path here)"));
//Get workbook for XLS file.
HSSFWorkbook yourworkbook = new HSSFWorkbook(file);
//Get first sheet from the workbook.
//If there have >1 sheet in your workbook, you can change it here IF you want to edit other sheets.
HSSFSheet sheet1 = yourworkbook.getSheetAt(0);
// Get the row of your desired cell.
// Let's say that your desired cell is at row 2.
Row row = sheet1.getRow(1);
// Get the column of your desired cell in your selected row.
// Let's say that your desired cell is at column 2.
Cell column = row.getCell(1);
// If the cell is String type.If double or else you can change it.
String updatename = column.getStringCellValue();
//New content for desired cell.
updatename="Lala";
//Print out the updated content.
System.out.println(updatename);
//Set the new content to your desired cell(column).
column.setCellValue(updatename);
//Close the excel file.
file.close();
//Where you want to save the updated sheet.
FileOutputStream out =
new FileOutputStream(new File("(where you want to save?.Put the path here)"));
yourworkbook.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句