网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
10月30日漏签0天
编程吧 关注:373,327贴子:1,636,757
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 5回复贴,共1页
<<返回编程吧
>0< 加载中...

如何将EXCEL表的内容读取到xml中

  • 只看楼主
  • 收藏

  • 回复
  • qyubinfeng
  • 中级粉丝
    2
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
题目是要求吧 名字是input.xls 的EXCEL表中 提取所有的PO NUMBER放到xml中


  • qyubinfeng
  • 中级粉丝
    2
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
代码如下:
/**
*
* 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(){
/*
* 首先创建一个XML文档
* 要创建XML文档,首先创建一个根元素
*/
Element reportRoot=new Element("sheet");
Document xmlReport=new Document(reportRoot);
try {
//读取Excel文件
FileInputStream excelFIS=new FileInputStream("D:\\sample_data\\input1.xls");
//创建Excel工作表
HSSFWorkbook excelWB=new HSSFWorkbook(excelFIS);
//获得Excel工作簿
HSSFSheet excelSheet=excelWB.getSheetAt(0);
//获得工作簿的行数
int rows=excelSheet.getPhysicalNumberOfRows();
//遍历工作簿的行
for(int rowIndex=0; rowIndex<rows;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<cells;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();



2025-10-30 03:20:42
广告
不感兴趣
开通SVIP免广告
  • qyubinfeng
  • 中级粉丝
    2
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
}
/*
* 去掉非法字符
*/
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("PO NUMBER","String");
cellElement.addAttribute(strTypeAttribute);
cellElement.appendChild(oneCell.getStringCellValue());
rowElement.appendChild(cellElement);
break;
case HSSFCell.CELL_TYPE_NUMERIC:
strTypeAttribute=new Attribute("PO NUMBER","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());
}
/*
* 计算薪水的 1% 并存储到一个 donation 元素中
*/
Elements rowElements=reportRoot.getChildElements("row");
//遍历每一个行元素
for(int i=0;i<rowElements.size();i++){
/*
* 第一行元素是头,不计算
* 计算薪水的1%存储到donation的节点上
*/
if(i==0){
Element donationElement=new Element("header");
donationElement.appendChild("Donation");
Attribute dataType=new Attribute("PO NUMBER", "String");
donationElement.addAttribute(dataType);
Attribute dataFormat=new Attribute("dataFormat", "General");



  • qyubinfeng
  • 中级粉丝
    2
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
donationElement.addAttribute(dataFormat);
rowElements.get(i).appendChild(donationElement);
}else{
Element donationElement=new Element("donation");
//添加数据类型的属性
Attribute dataType=new Attribute("PO NUMBER","Numeric");
donationElement.addAttribute(dataType);
//添加数据格式的属性
Attribute dataFormat=new Attribute("PO NUMBER", "#,##0");
donationElement.addAttribute(dataFormat);
//获得当前行的薪水
Element salaryElement=rowElements.get(i).getFirstChildElement("PO NUMBER");
String salaryString=salaryElement.getValue();
//转换薪水格式
NumberFormat numberFormat=NumberFormat.getInstance();
Number salaryNumber=numberFormat.parse(salaryString);
//计算薪水的1%
double donationSalary=salaryNumber.doubleValue()*0.01;
//添加薪水节点
donationElement.appendChild(Double.toString(donationSalary));
rowElements.get(i).appendChild(donationElement);
}//End else
}//End for(int i=0;i<rowElements.size();i++)
//屏幕输出XML文件
System.out.println(xmlReport.toXML());
/*
* 输出到文件
*/
FileOutputStream xmlFOS=new FileOutputStream("D:\\sample_data\\output_schema.xml");
//创建Serializer,输出文件
Serializer saveXMLSerializer=new Serializer(xmlFOS);
//saveXMLSerializer.setIndent(5);
saveXMLSerializer.write(xmlReport);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
}



  • qyubinfeng
  • 中级粉丝
    2
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
报错如下:
Exception in thread "main" java.lang.NullPointerException
at com.wds.excelxml.ExcelXML.excelXML(ExcelXML.java:81)
at com.wds.excelxml.ExcelXML.main(ExcelXML.java:36)


  • qyubinfeng
  • 中级粉丝
    2
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这段代码是copy的 但是我只理解前面的部分,建一个模型差不多的,然后把数据放进去,然后我应该怎么拿出内容是PO NUMBER的?


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 5回复贴,共1页
<<返回编程吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示