/**
*
* 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:\\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<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();
}
/*
* å»æéæ³å符
*/
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());
}
温馨提示:内容为网友见解,仅供参考