java把string.xml转化成excel文件

来源:互联网 发布:阿里云免费二级域名 编辑:程序博客网 时间:2024/06/05 03:19

在此之前一下代码需导入dom4j.jar和jxl.jar包。因为工作需要经常要整理string出来翻译,为了偷懒所以写了这个功能。

public class ParseXml {    static int count=0;    public static void main(String[] args) {        parserXml("G:\\work\\translate\\allString\\");    }    public static void parserXml(String fileName) {        SAXReader saxReader = new SAXReader();        //生成excel文件路径        String excelPath = "G:\\translate\\String.xls";        //原string.xml文件目录        String path="G:\\translate\\allString\\";        File excelFile = new File(excelPath);        try {            if (!excelFile.exists()) {                excelFile.createNewFile();            }            WritableWorkbook book=null;            book = Workbook.createWorkbook(excelFile);                    File inputXml = new File(path);            String[] fileList = inputXml.list();            WritableSheet[] ws=new WritableSheet[fileList.length];            for (String name : fileList) {                // 新建一个sheet                ws[count] = book.createSheet(name, count);                 System.out.println(fileName+name);                Document document = saxReader.read(fileName+name);                Element employees = document.getRootElement();                int j = 0;                for (Iterator i = employees.elementIterator(); i.hasNext(); j++) {                    Element employee = (Element) i.next();                    WritableCell attribute = new jxl.write.Label(0, j,employee.attributeValue("name"));                    WritableCell values = new jxl.write.Label(1, j, employee.getStringValue());                    // 往表中添加内容                    ws[count].addCell(attribute);                    ws[count].addCell(values);                }                count++;            }            // 将数据写入所建的excel            book.write();            book.close();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (RowsExceededException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (WriteException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (DocumentException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}



0 0
原创粉丝点击