前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何将xml转换成excel_java 解析xml

如何将xml转换成excel_java 解析xml

作者头像
全栈程序员站长
发布2022-09-23 10:44:57
8760
发布2022-09-23 10:44:57
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

展开全部

/**

*

* 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==
2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档