excel文件转换成xml文件

package test;

import java.io.*;

import jxl.*;
import org.jdom.Element;
import org.jdom.Document;
import org.jdom.output.XMLOutputter;

/**
 * 将excel转换成xml文件
 *
 * @author ztt
 *
 */
public class EtoX {
    /**
     * 程序入口
     *
     * @param args
     */
    public static void main(String[] args) {

        File file = new File("d:/excel");
        String[] files = file.list();
        for (int i = 0; i < files.length; i++) {

            File file1 = new File(files[i]);
            String newFile = file1.getName();
            String[] names = newFile.split("//.");
            String outName = "d:/xml/" + names[0] + ".xml";
            String inName = "d:/excel/" + newFile;
            excel2Xml(inName, outName);

        }
    }

    /**
     * 转换函数
     *
     * @param filePath
     *            excel文件
     * @param out
     *            xml文件
     */
    public static void excel2Xml(String filePath, String out) {
        try {
            InputStream in = new FileInputStream(filePath);
            Workbook wb = Workbook.getWorkbook(in);
            // 工作薄(Workbook)中工作表(Sheet)的个数
            System.out.println(wb.getNumberOfSheets());

            Sheet sheet = wb.getSheet(0);
            int columns = sheet.getColumns(); // 返回表中的总列数
            int rows = sheet.getRows(); // 返回表中总行数
            System.out.println("文件总共" + rows + "行");
            Element root = new Element("allElement");
            Document doc = new Document(root);
            for (int i = 0; i < rows; i++) {
                Cell[] cell = sheet.getRow(i);
                // System.out.println(cell.length);

                Element instrument = new Element("element");

                Element e1 = new Element("name");
                e1.setText(cell[0].getContents());
                instrument.addContent(e1);

                Element e2 = new Element("address");
                e2.setText(cell[1].getContents());
                instrument.addContent(e2);

                Element e3 = new Element("tel");
                e3.setText(cell[2].getContents());
                instrument.addContent(e3);

                Element e4 = new Element("date");
                e4.setText(cell[3].getContents());
                instrument.addContent(e4);

                Element e5 = new Element("map");
                e5.setText(cell[4].getContents());
                instrument.addContent(e5);

                root.addContent(instrument);

            }
            XMLOutputter outputter = new XMLOutputter();
            FileOutputStream output = new FileOutputStream(out);
            outputter.output(doc, output);

            wb.close();
            in.close();
            outputter.clone();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值