public static void main(String[] args) throws FileNotFoundException { ArrayList<Node> nodes = new ArrayList<Node>(); Node node0 = new Node(0, "1", null); Node node1 = new Node(1, "2", "0"); Node node2 = new Node(2, "3", "1"); Node node3 = new Node(3, "4", "1"); Node node4 = new Node(4, "5", "2"); Node node5 = new Node(5, "6", "2"); Node node6 = new Node(6, "7", "0"); nodes.add(node0); nodes.add(node1); nodes.add(node2); nodes.add(node3); nodes.add(node4); nodes.add(node5); nodes.add(node6); OutputStream outputStream = new FileOutputStream("D:\javaTools\project\TestProject-dd\tree_data.xlsx"); exportToExcel(nodes, outputStream); } public static void exportToExcel(List<Node> nodeList, OutputStream outputStream) { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet("Tree Data"); // 创建表头 Row headerRow = sheet.createRow(0); headerRow.createCell(0).setCellValue("ID"); headerRow.createCell(1).setCellValue("Name"); headerRow.createCell(2).setCellValue("Parent ID"); // 创建数据行 int rowIndex = 1; for (Node node : nodeList) { Row row = sheet.createRow(rowIndex++); row.createCell(0).setCellValue(node.getId()); row.createCell(1).setCellValue(node.getName()); row.createCell(2).setCellValue(node.getParentId()); } // 合并父级相同的行 Map<String, CellRangeAddress> mergedRegions = new HashMap<>(); for (int i = 1; i < rowIndex; i++) { Row row = sheet.getRow(i); String parentId = row.getCell(2).getStringCellValue(); if (mergedRegions.containsKey(parentId)) { CellRangeAddress mergedRegion = mergedRegions.get(parentId); int lastRow = mergedRegion.getLastRow(); if (lastRow == i - 1) { mergedRegions.put(parentId, new CellRangeAddress(lastRow, i, 2, 2)); } else { mergedRegions.put(parentId, new CellRangeAddress(lastRow + 1, i, 2, 2)); } } else { mergedRegions.put(parentId, new CellRangeAddress(i, i, 2, 2)); } } for (CellRangeAddress mergedRegion : mergedRegions.values()) { sheet.addMergedRegion(mergedRegion); } // 输出Excel try { workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); } } 这段代码有什么问题
时间: 2024-01-27 15:06:14 浏览: 23
这段代码存在以下问题:
1. 在导出到Excel中时,将所有数据都写入同一个Sheet中,如果数据量很大,可能会导致Excel文件过大或者导出失败。应该考虑将数据分成多个Sheet导出。
2. 在合并父级相同的行时,使用了HashMap来存储合并的区域,但是HashMap并不能保证顺序,可能会导致合并后的行顺序错乱。应该使用LinkedHashMap来保证顺序。
3. 导出时,将Excel文件写入OutputStream中,但是写入后没有关闭OutputStream。应该在写入完成后,及时关闭OutputStream。
相关问题
public class PlaylistApp { public static void Main(string[] args) { LinkedList<string> playlist = new LinkedList<string>(); Stack<string> history = new Stack<string>(); Scanner scanner = new Scanner(System.in); 修改
public class PlaylistApp {
public static void main(String[] args) {
LinkedList<String> playlist = new LinkedList<String>();
Stack<String> history = new Stack<String>();
Scanner scanner = new Scanner(System.in);
// 继续编写你的代码
}
}
public static void main(String[] args) { List<String> s = new ArrayList<>(); s.add("3"); List<String> s2 = Arrays.asList("1","2"); s2.addAll(s); System.out.println(s2); }
很抱歉,我之前的回答有误。根据您提供的修正后的代码,将元素 "3" 添加到了列表 s 中,并且通过 Arrays.asList 方法创建了一个包含元素 "1" 和 "2" 的不可变列表 s2。然后,使用 s2 的 addAll 方法尝试将列表 s 中的所有元素添加到 s2 中。然而,由于 Arrays.asList 返回的是一个固定大小的列表,不能进行添加或删除操作,所以会抛出 UnsupportedOperationException 异常。
正确的代码应该是使用 ArrayList 来创建 s2 列表,如下所示:
```java
public static void main(String[] args) {
List<String> s = new ArrayList<>();
s.add("3");
List<String> s2 = new ArrayList<>(Arrays.asList("1", "2"));
s2.addAll(s);
System.out.println(s2);
}
```
这样,最终输出的结果将会是 ["1", "2", "3"]。