如何将xml转换成excel_java 解析xml

如何将xml转换成excel_java 解析xml展开全部/****ExcelXML.java*IBM_Developer_POI(Excel,Word)*/packagecom.wds.excelxml;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.I…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

展开全部

/**

*

* ExcelXML.java

* IBM_Developer_POI(Excel,Word) */

package com.wds.excelxml;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.text.NumberFormat;

import java.text.ParseException;

import org.apache.poi.hssf.usermodel.HSSFCell;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;

import org.apache.poi.hssf.usermodel.HSSFDataFormat;

import org.apache.poi.hssf.usermodel.HSSFDataFormatter;

import org.apache.poi.hssf.usermodel.HSSFHyperlink;

import org.apache.poi.hssf.usermodel.HSSFRow;

import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import nu.xom.Attribute;

import nu.xom.Document;

import nu.xom.Element;

import nu.xom.Elements;

import nu.xom.Serializer;

public class ExcelXML {

public static void main(String[] args) {

excelXML();

}

/**

* 从Excel到XML

* 从XML到Excel

*/

private static void excelXML(){

/*

* 首先创建一个32313133353236313431303231363533e59b9ee7ad9431333337373665XML文档

* 要创建XML文档,首先创建一个根元素

*/

Element reportRoot=new Element(“sheet”);

Document xmlReport=new Document(reportRoot);

try {

//读取Excel文件

FileInputStream excelFIS=new FileInputStream(“D:\\JavaTest\\Employee_List.xls”);

//创建Excel工作表

HSSFWorkbook excelWB=new HSSFWorkbook(excelFIS);

//获得Excel工作簿

HSSFSheet excelSheet=excelWB.getSheetAt(0);

//获得工作簿的行数

int rows=excelSheet.getPhysicalNumberOfRows();

//遍历工作簿的行

for(int rowIndex=0; rowIndex

HSSFRow oneRow=excelSheet.getRow(rowIndex);

if(oneRow==null){

continue;

}

//在迭代每一行的时候,创建xml的行元素

Element rowElement=new Element(“row”);

//获得当前行的单元格数

int cells=oneRow.getPhysicalNumberOfCells();

//遍历行中的每一个单元格

for(int cellIndex=0;cellIndex

HSSFCell oneCell=oneRow.getCell(cellIndex);

if(oneCell==null){

continue;

}

//设置元素的默认名称

String elementName=”header”;

//获得单元格所在列位置

int cellColumnIndex=oneCell.getColumnIndex();

if(rowIndex>0){

elementName=reportRoot.getFirstChildElement(“row”).getChild(cellColumnIndex).getValue();

}

/*

* 去掉非法字符

*/

elementName = elementName.replaceAll(“[\\P{ASCII}]”,””);

elementName = elementName.replaceAll(” “, “”);

Element cellElement = new Element(elementName);

//添加属性和元素

//String attributeValue=oneCell.getCellStyle().getDataFormatString();

//Attribute dataFormatAttribute=new Attribute(“dataFormat”, attributeValue);

//cellElement.addAttribute(dataFormatAttribute);

/*

* 根据不同的属性添加

*/

Attribute strTypeAttribute=null;

switch (oneCell.getCellType()) {

case HSSFCell.CELL_TYPE_STRING:

strTypeAttribute=new Attribute(“dataType”,”String”);

cellElement.addAttribute(strTypeAttribute);

cellElement.appendChild(oneCell.getStringCellValue());

rowElement.appendChild(cellElement);

break;

case HSSFCell.CELL_TYPE_NUMERIC:

strTypeAttribute=new Attribute(“dataType”,”Numeric”);

cellElement.addAttribute(strTypeAttribute);

HSSFDataFormatter dataFormatter=new HSSFDataFormatter();

String cellFormatted=dataFormatter.formatCellValue(oneCell);

cellElement.appendChild(cellFormatted);

rowElement.appendChild(cellElement);

break;

}

}

if(rowElement.getChildCount()>0){

reportRoot.appendChild(rowElement);

}

//System.out.println(xmlReport.toXML());

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/171931.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 请简述list,set,map类型的集合的各自特点_list与set的区别

    请简述list,set,map类型的集合的各自特点_list与set的区别List、Map、Set的区别与联系一、结构特点List和Set是存储单列数据的集合,Map是存储键值对这样的双列数据的集合;List中存储的数据是有顺序的,并且值允许重复;Map中存储的数据是无序的,它的键是不允许重复的,但是值是允许重复的;Set中存储的数据是无顺序的,并且不允许重复,但元素在集合中的位置是由元素的hashcode决定,即位置是固定的(Set集合是根据hashcod…

    2025年9月25日
    4
  • JS数组合并(5种)

    JS数组合并(5种)前言项目过程中,经常会遇到JS数组合并的情况,时常为这个纠结。这里整理一下。简单而实用的for最容易想到的莫过于for了。会变更原数组,当然也可以写成生成新数组的形式。letarr=[1,2]letarr2=[3,4]for(letiinarr2){arr.push(arr2[i])}console.log(arr)//[1,2,3,4]arr.concat(arr2)会生成新的数组。letarr=[1,2]let

    2022年6月30日
    68
  • 03_Hadoop架构

    03_Hadoop架构

    2021年8月22日
    56
  • 关于python中可迭代对象和迭代器的一些理解

    关于python中可迭代对象和迭代器的一些理解

    2021年6月17日
    102
  • windows 7 开机错误 未能连接到一个Windows服务

    windows 7 开机错误 未能连接到一个Windows服务Windows无法连接到systemeventnotificationservice服务。此问题阻止标准用户登录系统。作为管理员用户,您可以复查系统事件日志,以获得有关此服务未响应原因的详细信息。如下图:莫名其妙突然电脑死机了,重启后就出现了上述问题,并且电脑无法连网,开机速度慢到需要4分钟多,刚开机还黑屏。查看systemeventnotificationserv…

    2022年5月15日
    43
  • 华三交换机配置access命令_H3C交换机配置基本命令详解

    华三交换机配置access命令_H3C交换机配置基本命令详解H3C交换机配置基本命令详解随着移动互联网趋势加快以及智能终端的快速普及,WLAN应用需求在全球保持高速增长态势。下面是小编整理的关于H3C交换机配置基本命令详解,希望大家认真阅读!1、配置主机名[H3C]systemnameH3C2、配置console口密码#进入系统视图。system-view#进入AUX用户界面视图。[H3C]user-interfaceaux0#设置通过Con…

    2022年6月20日
    198

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号