解析xml文件并写入Excel表
类似于如下格式的xml数据,解析之后将数据写入Excel中
| <?xml version="1.0" encoding="UTF-8"?> |
| <DATASETS> |
| <DATASET> |
| <education>undergraduate</education> |
| <occupation>programmer</occupation> |
| <name>张三</name> |
| <weight>150</weight> |
| <age>23</age> |
| <height>173</height> |
| <hobby>basketball</hobby> |
| </DATASET> |
| <DATASET> |
| <education>undergraduate</education> |
| <occupation>programmer</occupation> |
| <name>王一</name> |
| <weight>120</weight> |
| <age>22</age> |
| <height>168</height> |
| <hobby>game</hobby> |
| </DATASET> |
| <DATASET> |
| <education>undergraduate</education> |
| <occupation>programmer</occupation> |
| <name>魏某</name> |
| <weight>125</weight> |
| <age>22</age> |
| <height>170</height> |
| <hobby>novel</hobby> |
| </DATASET> |
| </DATASETS> |
一. 构建springboot项目,导入依赖
| |
| <dependency> |
| <groupId>org.jdom</groupId> |
| <artifactId>jdom</artifactId> |
| <version>1.1.3</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.apache.poi</groupId> |
| <artifactId>poi</artifactId> |
| <version>4.1.0</version> |
| </dependency> |
| <dependency> |
| <groupId>org.apache.poi</groupId> |
| <artifactId>poi-ooxml</artifactId> |
| <version>4.1.0</version> |
| </dependency> |
二. 直接上方法
| |
| |
| |
| |
| |
| |
| |
| public static void xmlToExcel(String xmlPosition,String excelName){ |
| |
| SAXBuilder saxBuilder = new SAXBuilder(); |
| |
| InputStream is = null; |
| try { |
| is = new FileInputStream(new File(xmlPosition)); |
| }catch (Exception e){ |
| e.printStackTrace(); |
| System.out.println("文件位置未找到!"); |
| return; |
| } |
| |
| |
| Document document = null; |
| try { |
| document = saxBuilder.build(is); |
| }catch (Exception e){ |
| e.printStackTrace(); |
| System.out.println("文件内容构建失败!"); |
| System.out.println("请检查文件内容是否符合xml格式"); |
| } |
| |
| Element rootElement = document.getRootElement(); |
| |
| List<Element> children = rootElement.getChildren(); |
| ArrayList<HashMap<String, Object>> list = new ArrayList<>(); |
| for (Element child : children) { |
| |
| |
| |
| |
| |
| |
| |
| |
| List<Element> childrenList = child.getChildren(); |
| HashMap<String, Object> map = new HashMap<>(); |
| for (Element o : childrenList) { |
| |
| map.put(o.getName(), o.getValue()); |
| } |
| list.add(map); |
| } |
| |
| if (list.size() > 0){ |
| |
| File file = new File("C:\\Users\\Asus\\Desktop\\" + excelName + ".xls"); |
| if (file.exists()){ |
| System.out.println("该位置存在相同名字的文件,请重新命名!"); |
| return; |
| }else { |
| try { |
| boolean newFile = file.createNewFile(); |
| if (newFile){ |
| System.out.println("文件创建成功!"); |
| System.out.println("开始导入数据!"); |
| HSSFWorkbook wb = new HSSFWorkbook(); |
| HSSFSheet sheet = wb.createSheet("sheet1"); |
| for (int i = 0; i <= list.size(); i++) { |
| HSSFRow row = sheet.createRow(i); |
| if (i == 0){ |
| Set<Map.Entry<String, Object>> entries = list.get(i).entrySet(); |
| int j = 0; |
| for (Map.Entry<String, Object> entry : entries) { |
| HSSFCell cell = row.createCell(j); |
| cell.setCellValue(entry.getKey()); |
| j++; |
| } |
| }else { |
| Set<Map.Entry<String, Object>> entries = list.get(i-1).entrySet(); |
| int j = 0; |
| for (Map.Entry<String, Object> entry : entries) { |
| HSSFCell cell = row.createCell(j); |
| cell.setCellValue(entry.getValue().toString()); |
| j++; |
| } |
| } |
| } |
| ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| try { |
| wb.write(os); |
| }catch (IOException e){ |
| System.out.println("导入失败!"); |
| e.printStackTrace(); |
| } |
| byte[] content = os.toByteArray(); |
| OutputStream fos = null; |
| try { |
| fos = new FileOutputStream(file); |
| fos.write(content); |
| os.close(); |
| fos.close(); |
| System.out.println("数据导入成功!"); |
| }catch (IOException e){ |
| System.out.println("导入失败!"); |
| e.printStackTrace(); |
| } |
| }else { |
| System.out.println("文件创建失败!"); |
| } |
| } catch (IOException e) { |
| e.printStackTrace(); |
| System.out.println("excel文件创建失败!"); |
| } |
| } |
| }else { |
| System.out.println("源文件没有数据!"); |
| return; |
| } |
三. 最后excel文件

四. 扩展: 上面提到了写入excel,下面来试试读
| public void pushData() { |
| |
| File file = new File("excel文件的地址"); |
| Workbook wb = null; |
| try { |
| wb = readExcel(new FileInputStream(file), file.getName()); |
| Row row = null; |
| if(wb != null){ |
| |
| Sheet sheet = wb.getSheetAt(0); |
| |
| |
| ArrayList<HashMap<String, Object>> list = new ArrayList<>(); |
| |
| int rows = sheet.getPhysicalNumberOfRows(); |
| |
| int lastRowNum = sheet.getLastRowNum(); |
| |
| Row firstRow = sheet.getRow(0); |
| |
| short lastCellNum = firstRow.getLastCellNum(); |
| |
| Cell cell = firstRow.getCell(0); |
| |
| String value = cell.getStringCellValue(); |
| |
| |
| } |
| } catch (FileNotFoundException e) { |
| System.out.println("读取文件失败!"); |
| e.printStackTrace(); |
| } |
| } |
| |
| public Workbook readExcel(InputStream is, String extString){ |
| Workbook wb = null; |
| extString = extString.substring(extString.lastIndexOf(".")); |
| try { |
| if(".xls".equals(extString)){ |
| return new HSSFWorkbook(is); |
| }else if(".xlsx".equals(extString)){ |
| return new XSSFWorkbook(is); |
| }else { |
| return null; |
| } |
| } catch (FileNotFoundException e) { |
| e.printStackTrace(); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } |
| return wb; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!