简体   繁体   English

使用apache元模型更新excel文件

[英]updating an excel file with apache metamodel

I'm trying to incorporate Apache MetaModel into a project and keep running into a weird problem. 我正在尝试将Apache MetaModel合并到一个项目中并继续遇到一个奇怪的问题。 I update an Excel spreadsheet row in code. 我在代码中更新了Excel电子表格行。 The code finds the right row, deletes it, then appends the row (with my update) to the bottom of the spreadsheet. 代码找到正确的行,删除它,然后将行(使用我的更新)附加到电子表格的底部。 I'd like the update to happen in-place, with the same data staying in the same row. 我希望更新能够就地发生,相同的数据保持在同一行。 I thought it was something I was doing wrong, then set up a stupid simple project to duplicate the behavior. 我认为这是我做错了,然后设置一个愚蠢的简单项目来复制行为。 Unfortunately, the problem remains. 不幸的是,问题仍然存在。

Here's the xlsx file: 这是xlsx文件:

Name    Address           City          State   Zip
Bob     123 Main St.      Norman        OK      11111
Fred    989 Elm Street    Chicago       IL      22222
Mary    555 First Street  San Francisco CA      33333

Now, I want to update Bob's Zip to "None". 现在,我想将Bob的Zip更新为“None”。

package MMTest;
import java.io.File;
import org.apache.metamodel.UpdateableDataContext;
import org.apache.metamodel.excel.ExcelDataContext;
import org.apache.metamodel.schema.Column;
import org.apache.metamodel.schema.Schema;
import org.apache.metamodel.schema.Table;
import org.apache.metamodel.update.Update;
public class MMTest {
    public static void main(String[] args) {
    UpdateableDataContext excel = new ExcelDataContext(new File("C:/test/test.xlsx"));
    Schema schema = excel.getDefaultSchema();
    Table[] tables = schema.getTables();
    assert tables.length == 1;
    Table table = schema.getTables()[0];
    Column Name = table.getColumnByName("Name");
    Column Zip = table.getColumnByName("Zip");
    excel.executeUpdate(new Update(table).where(Name).eq("Bob").value(Zip, "None"));
    }
}

Pretty simple right? 很简单吧? Nope. 不。 This is the result: 这是结果:

Name    Address           City          State   Zip
<blank line>                
Fred    989 Elm Street    Chicago       IL      22222
Mary    555 First Street  San Francisco CA      33333
Bob     123 Main St.      Norman        OK      None

Am I missing something simple? 我错过了一些简单的事吗? The documentation is pretty sparse, but I've read everything the internet has to offer on this package. 文档非常稀疏,但我已经阅读了互联网在此软件包上提供的所有内容。 I appreciate your time. 我很感激你的时间。

Late to the party, but I've recently bumped into this issue and haven't spotted an answer elsewhere yet. 晚了,但我最近碰到了这个问题,还没有在其他地方找到答案。 The actual deleting takes place in ExcelDeleteBuilder.java 实际删除发生在ExcelDeleteBuilder.java中

If you aren't concerned about maintaining row order, you could change 如果您不关心维护行顺序,则可以更改

    for (Row row : rowsToDelete) {
        sheet.removeRow(row);
    }

to

    for (Row row : rowsToDelete) {
        int rowNum = row.getRowNum() + 1;
        sheet.removeRow(row);
        sheet.shiftRows(rowNum, sheet.getLastRowNum(), -1);
    }

See Apache POI docs for a better understanding of shiftRows() . 有关shiftRows()的更好理解,请参阅Apache POI文档。 As Adi pointed out, you'll still end up with the "updated" row being moved to the bottom, but in my use case the empty row is successfully removed. 正如Adi指出的那样,你仍然会将“更新”行移到底部,但在我的用例中,成功删除了空行。

NB I'm working from Apache Metamodel 4.5.4 NB我正在使用Apache Metamodel 4.5.4

You are not missing anything. 你没有遗漏任何东西。 The ExcelDataContext is not providing it's own update behavior. ExcelDataContext没有提供自己的更新行为。 It is defaulting to use apache meta-model's default store agnostic implementation for updating the data. 使用apache元模型的默认存储不可知实现来更新数据是默认的。 That implementation of UpdateCallback uses DeleteAndInsertCallback which is causing the behavior you are observing. UpdateCallback的实现使用DeleteAndInsertCallback,这会导致您正在观察的行为。 It picks the row to be updated, updates it with a new value in memory, deletes the original row and inserts the updated row(which ends up in the bottom which is ExcelDataContext behavior). 它选择要更新的行,使用内存中的新值更新它,删除原始行并插入更新的行(最后一行是ExcelDataContext行为)。 You can open an issue at https://issues.apache.org/jira/browse/METAMODEL Attach your sample code and data. 您可以通过https://issues.apache.org/jira/browse/METAMODEL打开问题。附上示例代码和数据。 Best would be a failing unit test in https://git-wip-us.apache.org/repos/asf/metamodel.git 最佳将是https://git-wip-us.apache.org/repos/asf/metamodel.git中的失败单元测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM