有时候我门需要把EXCEL表格中的数据转换成XML格式 这需要用到JXL(分析EXCEL)包和JDOM包(构成XML)
import java.io.*;import jxl.*;import org.jdom.Element;import org.jdom.Document;import org.jdom.output.XMLOutputter;
/** * * @author guo */public class EtoX { /** Creates a new instance of EtoX */ public EtoX() { } /** * @param args the command line arguments */ public static void main(String[] args) { try{ InputStream is= new FileInputStream("C://Documents and Settings//guo//EXCELtoXML//src//exceltoxml//ningxia.xls"); Workbook rwb=Workbook.getWorkbook(is); //System.out.println(rwb.getNumberOfSheets()); //获得工作薄(Workbook)中工作表(Sheet)的个数 Sheet rs=rwb.getSheet(0); int l=rs.getColumns(); //返回表中的总列数 int w=rs.getRows(); //返回表中包含总行数 System.out.println("文件总共"+w+"行"); Element root=new Element("allmetadata"); Document doc=new Document(root); for(int i=0;i<w;i++) { Cell[] cell = rs.getRow(i); //System.out.println(cell.length);
Element instrument=new Element("metadata"); Element e0=new Element("DBOperation"); e0.setText("insert"); instrument.addContent(e0); Element e1=new Element("EquipClassficationNum"); e1.setText(cell[0].getContents()); instrument.addContent(e1); Element e2=new Element("EuipNum"); //e2.setText(rs.getString()); e2.setText("null"); instrument.addContent(e2); Element e3=new Element("EquipCHNName"); e3.setText(cell[1].getContents()); //e3.setText("null"); instrument.addContent(e3); Element e4=new Element("EquipENGName"); //e3.setText(rs.getString(2)); e4.setText("null"); instrument.addContent(e4); Element e5=new Element("EquipModel"); e5.setText(cell[2].getContents()); //e5.setText("null"); instrument.addContent(e5); Element e6=new Element("EquipTechParameter"); e6.setText(cell[3].getContents()); //e5.setText("null"); instrument.addContent(e6); Element e7=new Element("EquipApplicationArea"); e7.setText(cell[4].getContents()); //e7.setText("null"); instrument.addContent(e7);
Element e8=new Element("EquipAccessory"); //e8.setText(rs.getString(5)); e8.setText("null"); instrument.addContent(e8); Element e9=new Element("EquipCertification"); e9.setText(cell[5].getContents()); //e9.setText("null"); instrument.addContent(e9);
Element e10=new Element("EquipManufacturer"); e10.setText(cell[6].getContents()); //e9.setText("null"); instrument.addContent(e10); Element e11=new Element("EquipManuCountryCode"); e11.setText(cell[7].getContents()); //e11.setText("null"); instrument.addContent(e11); Element e12=new Element("EquipPriceRMB"); e12.setText(cell[8].getContents()); //System.out.println(cell[8].getContents()); //e11.setText("null"); instrument.addContent(e12); Element e13=new Element("EquipServiceDate"); e13.setText(cell[9].getContents()); //e11.setText("null"); instrument.addContent(e13); Element e14=new Element("Equip"); e14.setText(cell[10].getContents()); //e11.setText("null"); instrument.addContent(e14); Element e15=new Element("EquipLocation"); e15.setText(cell[11].getContents()); //e11.setText("null"); instrument.addContent(e15); Element e16=new Element("EquipZIPCode"); e16.setText(cell[12].getContents()); //e11.setText("null"); instrument.addContent(e16); Element e17=new Element("EquipContactPerson"); e17.setText(cell[13].getContents()); //e11.setText("null"); instrument.addContent(e17); Element e18=new Element("EquipTelephone"); e18.setText(cell[14].getContents()); System.out.println(cell[14].getContents()); //e11.setText("null"); instrument.addContent(e18); Element e19=new Element("EquipEmail"); //e19.setText(rs.getString(15)); e19.setText("null"); instrument.addContent(e19); Element e20=new Element("EquipOpenCalendar"); e20.setText(cell[15].getContents()); //e20.setText("null"); instrument.addContent(e20); Element e21=new Element("EquipServiceCharge"); e21.setText(cell[16].getContents()); //e11.setText("null"); instrument.addContent(e21); Element e22=new Element("EquipTotalServiceTime"); //e22.setText(rs.getString(16)); e22.setText("null"); instrument.addContent(e22); Element e23=new Element("EquipTotalServiceFee"); //e22.setText(rs.getString(16)); e23.setText("null"); instrument.addContent(e23); Element e24=new Element("EquipServiceUsers"); //e22.setText(rs.getString(16)); e24.setText("null"); instrument.addContent(e24); Element e25=new Element("EquipServiceAchivements"); //e22.setText(rs.getString(16)); e25.setText("null"); instrument.addContent(e25); Element e26=new Element("EquipOtherInfo"); //e22.setText(rs.getString(16)); e26.setText("null"); instrument.addContent(e26); root.addContent(instrument); //System.out.println("第"+i+"行生成完成!"); } XMLOutputter outputter=new XMLOutputter(); FileOutputStream output=new FileOutputStream("ningxia.xml"); outputter.output(doc,output); rwb.close(); is.close(); outputter.clone(); //String strc00=c00.getContents(); // System.out.println(strc00); //System.out.println(rs.getName()); //System.out.println(); //返回表中的总列数 //System.out.println(); //返回表中包含总行数 }catch(Exception e) { e.printStackTrace(); } } }
