Apache POI——使用 Java 将数据插入到特定的列/行和工作表 EXCEL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21977355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache POI -- inserting data into specific column/rows and sheets EXCEL w/ Java
提问by davex4k9
I have a document in excel that I want to edit or input a data on it using java..I have try it but all I can do is just delete the data in my excel and replace it the data which I just input.I'm using Netbeans for my compiler. My output is GUI/JFrame how to Insert data into a specific column, row and sheet?? example I want to insert it in C12, E11, Sheet1 etc.
我在 excel 中有一个文档,我想使用 java 在其上编辑或输入数据。我的编译器使用 Netbeans。我的输出是 GUI/JFrame 如何将数据插入特定的列、行和工作表??例如我想将它插入到 C12、E11、Sheet1 等中。
Thank you...
谢谢...
I'm doing self study only in Java Coding that's why I don't know much..
我只在 Java 编码方面进行自学,这就是为什么我知道的不多..
采纳答案by Nor Sakinah Abdullah
This is just example for xls not xlsx. You can refer here. if you want to edit/input data in xlsx (you can using XSSFWorkbook and XSSFSheet).
这只是 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();
}
回答by CKuharski
Here is snippet to help you:
这是可以帮助您的片段:
public void write() throws IOException, WriteException {
File file = new File("file_location");
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook wb= Workbook.createWorkbook(file, wbSettings);
wb.createSheet("My Spreadsheet", 0);
WritableSheet excel = wb.getSheet(0);
createLabel(excel);
createContent(excel);
wb.write();
wb.close();
}