我正在编写一个程序来读取一个大的xml文件并从中创建一个excel文件。每个节点的属性将是excel文件中的列标题。我创建了一个Dom对象并获得了节点列表。我需要遍历它,对于每个节点,我需要在excel表中添加一行,并将节点的属性值作为列值。因此,在迭代时,我需要动态创建行。我该怎么做?我没有看到在apache POI中添加创建的行的功能,到目前为止我所看到的是每次都定义新的行。我无法做到,因为它有超过5000个条目。基本上我想做的是:
Node node = null;
HSSFRow datarow = null;
for (int i = 0; i < nodeList.getLength(); i++) {
node = nodeList.item(i);
datarow = spreadSheet.createRow(i);
//set values for data row here, and add it.
//so in the loop, next time the same variable will be assigned to spreadSheet.createRow(1) etc.
}
我知道createRow是从spreadSheet调用的,它会将行添加到它。但是在循环中,同样的变量也会分配给其他行,所以我想最后我只会得到1行。请就此向我提出建议。
请尝试以下方法
Node node = null;
HSSFRow datarow = null;
for (int i = 0; i < nodeList.getLength(); i++) {
// On each loop you get the value of node item
node = nodeList.item(i);
//For every new node list you will create a row
datarow = spreadSheet.createRow(i);
//Finally set the node value to the columns of the newly created Row
}
希望这可以帮助 !!