java xml excel文件_java代码中读写xml文件、读excel文件

本文展示了如何使用Java从Excel文件中读取数据,并将这些数据写入XML文件。提供了创建XML文档、读取Excel文件以及解析XML文件的相关代码示例。
摘要由CSDN通过智能技术生成

一、需要读excel文件中的数据写入xml文件中

class="java" name="code">public interface XmlDocument {

/**

* 建立XML文档

* @param fileName 文件全路径名称

*/

public void createXml(String fileName);

/**

* 解析XML文档

* @param fileName 文件全路径名称

*/

public void parserXml(String fileName);

}

二、读excel文件并写入xml文件的test类

@Test

public void createXml() throws IOException {

String fileName = "d://test.xml";

Document document = DocumentHelper.createDocument();

//读取excel

String excelFilename = "d://1.xls";

Element employees = document.addElement("keywords");

Element employee = employees.addElement("fistkeywords");

Element name = employee.addElement("keywords");

name.setText("机械及行业设备");

Element code = employee.addElement("code");

code.setText("113");

Element thisrdkeywords = employee.addElement("thirdkeywords");

InputStream is;

is = new FileInputStream(excelFilename);

HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);

for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {

HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);

if (hssfSheet == null) {

continue;

}

for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {

HSSFRow hssfRow = hssfSheet.getRow(rowNum);

if (hssfRow == null) {

continue;

}

// 循环列Cell

// 0关键词 1code

HSSFCell keycell0 = hssfRow.getCell(0);

keycell0.setCellType(HSSFCell.CELL_TYPE_STRING);

String ssss = keycell0.getStringCellValue();

String keyexcel = getValue(keycell0);

//xml文件中插入值

Element namethird = thisrdkeywords.addElement("keywords");

namethird.setText(keyexcel);

HSSFCell codecell2 = hssfRow.getCell(1);

codecell2.setCellType(HSSFCell.CELL_TYPE_STRING);

String str=hssfRow.getCell(1).toString();

Element codethiird = thisrdkeywords.addElement("code");

codethiird.setText(getValue(codecell2));

}

}

try {

OutputFormat format = OutputFormat.createCompactFormat();

format.setEncoding("utf-8");

format.setNewlines(true);

Writer fileWriter = new FileWriter(fileName);

XMLWriter xmlWriter = new XMLWriter(fileWriter, format);

xmlWriter.write(document);

xmlWriter.close();

} catch (IOException e) {

System.out.println(e.getMessage());

}

}

三、读xml文件的类

public void parserXml() {

String fileName = "d://test.xml";

File inputXml = new File(fileName);

SAXReader saxReader = new SAXReader();

try {

Document document = saxReader.read(inputXml);

Element employees = document.getRootElement();

for (Iterator i = employees.elementIterator(); i.hasNext();) {

Element employee = (Element) i.next();

for (Iterator j = employee.elementIterator(); j.hasNext();) {

Element node = (Element) j.next();

System.out.println(node.getName() + ":" + node.getText());

}

}

} catch (DocumentException e) {

System.out.println(e.getMessage());

}

System.out.println("dom4j parserXml");

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值