使用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. I update an Excel spreadsheet row in code. 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.

我試圖將Apache元模型合並到一個項目中,並繼續遇到一個奇怪的問題。我用代碼更新Excel電子表格行。代碼找到正確的行,刪除它,然后將行(與我的更新)追加到電子表格的底部。我希望更新發生在適當的位置,相同的數據保持在同一行。我認為這是我做錯了的事情,然后建立了一個愚蠢的簡單項目來復制這種行為。不幸的是,問題依然存在。

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.

我錯過了什么簡單的東西嗎?文檔非常少,但是我已經閱讀了internet提供的關於這個包的所有內容。我很欣賞你的時間。

2 个解决方案

#1


1  

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(). 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.

請參閱Apache POI文檔,了解shiftRows()。正如Adi所指出的,您最終仍將“更新”行移動到底部,但在我的用例中,空行被成功刪除。

N.B. I'm working from Apache Metamodel 4.5.4

我在Apache Metamodel 4.5.4工作。

#2


0  

You are not missing anything. The ExcelDataContext is not providing it's own update behavior. It is defaulting to use apache meta-model's default store agnostic implementation for updating the data. That implementation of UpdateCallback uses DeleteAndInsertCallback which is causing the behavior you are observing. 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). You can open an issue at https://issues.apache.org/jira/browse/METAMODEL Attach your sample code and data. Best would be a failing unit test in https://git-wip-us.apache.org/repos/asf/metamodel.git

你沒有錯過任何東西。ExcelDataContext沒有提供它自己的更新行為。默認使用apache元模型的默認存儲不可知實現來更新數據。UpdateCallback的實現使用DeleteAndInsertCallback,它會導致您正在觀察的行為。它選擇要更新的行,在內存中使用一個新值對其進行更新,刪除原始行並插入更新后的行(最后在底部出現ExcelDataContext行為)。您可以在https://issues. apache.org/jirae./browse/metamodel上打開一個問題,附加您的示例代碼和數據。在https://git-wip-us.apache.org/repos/asf/metamodel.git中,最好是失敗的單元測試。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/08/08/725847dc83d3a9bfb5c35d3c11584cb0.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com