首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apache POI --使用Java将数据插入到特定的列/行和工作表中

Apache POI --使用Java将数据插入到特定的列/行和工作表中
EN

Stack Overflow用户
提问于 2014-02-24 09:36:06
回答 2查看 20.4K关注 0票数 5

我有一个excel文档,我想用java在它上面编辑或输入数据。我已经试过了,但我所能做的就是删除excel中的数据,并用我刚刚输入的数据替换它。我正在使用Netbeans作为我的编译器。我的输出是GUI/JFrame如何将数据插入到特定的列、行和表中??例如,我想把它插入到C12,E11,Sheet1等中。

谢谢..。

我只是在做Java代码的自学,这就是为什么我知道的不多。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-24 11:25:11

这只是xls而不是xlsx的示例。你可以参考here。如果您想在xlsx中编辑/输入数据(您可以使用XSSFWorkbook和XSSFSheet)。

代码语言:javascript
复制
    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();
    }
票数 7
EN

Stack Overflow用户

发布于 2014-02-24 10:13:46

以下是帮助您的代码片段:

代码语言:javascript
复制
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();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21977355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档