Android开发实现读取excel数据并保存为xml的方法
本文实例讲述了android开发实现读取excel数据并保存为xml的方法。分享给大家供大家参考,具体如下:
前阵子,公司请外面人翻译了一些android中values中的一些strings,然而保存的都是excel格式,如果单纯的将excel中的数据粘贴到指定的xml中的话,工作量非常的大,于是,自己写了个简单的demo,将excel中的数据读取并保存为xml对应的数据,下面的demo和图片展示:
1、数据保存在beanvalue中,包括key和value,方便后续数据读取
package cn.excel.parser; public class beanvalue { private string key; private string value; public beanvalue() { } public string getkey() { return key; } public void setkey(string key) { this.key = key; } public string getvalue() { return value; } public void setvalue(string value) { value = value; } }
2、数据解析,包括测试,直接在main方法中进行
package cn.excel.parser; import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.ioexception; import java.io.inputstream; import java.util.arraylist; import java.util.hashmap; import java.util.iterator; import java.util.list; import java.util.map; import java.util.map.entry; import java.util.treemap; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception; import javax.xml.transform.outputkeys; import javax.xml.transform.transformer; import javax.xml.transform.transformerconfigurationexception; import javax.xml.transform.transformerexception; import javax.xml.transform.transformerfactory; import javax.xml.transform.dom.domsource; import javax.xml.transform.stream.streamresult; import jxl.cell; import jxl.sheet; import jxl.workbook; import jxl.workbooksettings; import jxl.read.biff.biffexception; import org.w3c.dom.document; import org.w3c.dom.element; public class readexcelfile { private static final string src_file = "d://exceldoc/original/test.xls"; public static void main(string[] args) { map<integer, map<integer, beanvalue>> maplist = readexcelfile(); system.out.println("excel size= " + maplist.size() + " "); list<string> namelists = readcol5name(); system.out.println("namelists= " + namelists.size() + " "); writexmlfile(maplist, namelists); } /** * 读取excel表名,并保存在list列表中 * @return */ private static list<string> readsheetname() { inputstream is = null; workbook wb = null; java.util.list<string> list = null; try { is = new fileinputstream(src_file); if (null != is) { list = new arraylist<>(); wb = workbook.getworkbook(is); sheet[] sheets = wb.getsheets(); int sheetlen = sheets.length; for (int j = 0; j < sheetlen; j++) { list.add(sheets[j].getname()); }// for }// if } catch (filenotfoundexception e) { e.printstacktrace(); } catch (biffexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } finally { if (null != wb) { wb.close(); } if (null != is) { try { is.close(); } catch (ioexception e) { } } } return list; } /** * 读取第五列的标题名,并保持在list中 * @return */ private static list<string> readcol5name() { inputstream is = null; workbook wb = null; java.util.list<string> list = null; try { is = new fileinputstream(src_file); if (null != is) { list = new arraylist<>(); wb = workbook.getworkbook(is); sheet[] sheets = wb.getsheets(); int sheetlen = sheets.length; for (int j = 0; j < sheetlen; j++) { sheet rs = wb.getsheet(j); cell[] cell = rs.getrow(0); string packagename = cell[5].getcontents(); list.add(packagename); // system.out.println(packagename); }// for }// if } catch (filenotfoundexception e) { e.printstacktrace(); } catch (biffexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } finally { if (null != wb) { wb.close(); } if (null != is) { try { is.close(); } catch (ioexception e) { } } } return list; } /** * map<integer, beanvalue>,保持单张表中第三行开始,第2列和第5列的值(treemap可以按顺序加载) * 返回map<integer, map<integer, beanvalue>>,保证integer和表的索引一一对应 * 也可保持为list<map<integer, beanvalue>> * @return */ private static map<integer, map<integer, beanvalue>> readexcelfile() { inputstream is = null; workbook wb = null; map<integer, map<integer, beanvalue>> maplist = null; map<integer, beanvalue> maps = null; java.util.list<map<integer, beanvalue>> list = null; workbooksettings wosettings = null; try { is = new fileinputstream(src_file); if (null != is) { maplist = new hashmap<integer, map<integer, beanvalue>>(); list = new arraylist<>(); wosettings = new workbooksettings(); wosettings.setencoding("iso-8859-1");//设置编码格式 wb = workbook.getworkbook(is, wosettings); sheet[] sheets = wb.getsheets(); int sheetlen = sheets.length; for (int j = 0; j < sheetlen; j++) { sheet rs = wb.getsheet(j); int rownum = rs.getrows(); int colnum = rs.getcolumns(); maps = new treemap<>(); for (int i = 2; i < rownum; i++) { cell[] cell = rs.getrow(i); if (cell[5].getcontents() == null || cell[5].getcontents().trim().equals("")) { } else { beanvalue beanvalue = new beanvalue(); beanvalue.setkey(cell[2].getcontents()); beanvalue.setvalue(cell[5].getcontents()); maps.put(i, beanvalue); } } if (maps.size() > 0) { maplist.put(j, maps); system.out.println(sheets[j].getname()); } // list.add(maps); }// for }// if } catch (filenotfoundexception e) { e.printstacktrace(); } catch (biffexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } finally { if (null != wb) { wb.close(); } if (null != is) { try { is.close(); } catch (ioexception e) { } } } return maplist; } /** * 返回documentbuilder * @return */ public static documentbuilder getdocumentbuilder() { documentbuilderfactory dbfactory = documentbuilderfactory.newinstance(); documentbuilder dbbuilder = null; try { dbbuilder = dbfactory.newdocumentbuilder(); } catch (parserconfigurationexception e) { e.printstacktrace(); } return dbbuilder; } /** * 将所读excel的数据写入xml中,并按<string></string>格式保存 * @param maplist * @param namelist */ private static void writexmlfile( map<integer, map<integer, beanvalue>> maplist, list<string> namelist) { documentbuilder db = getdocumentbuilder(); document document = null; iterator<entry<integer, map<integer, beanvalue>>> iteratormap = maplist .entryset().iterator(); // int i = 0; while (iteratormap.hasnext()) { entry<integer, map<integer, beanvalue>> entrymap = iteratormap .next(); int i = entrymap.getkey(); map<integer, beanvalue> map = entrymap.getvalue(); document = db.newdocument(); document.setxmlstandalone(true); element resource = document.createelement("resource");//创建元素节点 resource.setattribute("xmlns:xliff", "urn:oasis:names:tc:xliff:document:1.2"); document.appendchild(resource);//添加元素 iterator<entry<integer, beanvalue>> iterator = map.entryset() .iterator(); while (iterator.hasnext()) { entry<integer, beanvalue> entry = iterator.next(); beanvalue beanvalue = entry.getvalue(); string key = beanvalue.getkey(); string value = beanvalue.getvalue(); if (value == null || value.trim().equals("")) { } else { element string = document.createelement("string"); string.setattribute("name", key); string.appendchild(document.createtextnode(value));//添加值 resource.appendchild(string);//添加子元素 } }// while string namestr = namelist.get(i); string packstr = namestr.substring(0, namestr.lastindexof("/")); string filename = namestr.substring(namestr.lastindexof("/") + 1); file file = new file("d://exceldoc/" + packstr); if (!file.exists()) { file.mkdirs(); } savexmldata(document,packstr,filename); }// while } private static void savexmldata(document document, string packstr, string filename) { transformerfactory tfactory = transformerfactory.newinstance(); try { transformer tftransformer = tfactory.newtransformer(); tftransformer.setoutputproperty(outputkeys.indent, "yes"); tftransformer.transform(new domsource(document), new streamresult("d://exceldoc/" + packstr + "/" + filename)); } catch (transformerconfigurationexception e) { e.printstacktrace(); } catch (transformerexception e) { e.printstacktrace(); } } }
提示:
1、需要引入的包:excel(jxl.jar)xml(dom4j-1.6.1.jar),excel解析poi-3.11-20141221.jar也可以;
2、读取excel会出现乱码问题,可通过workbooksettings进行编码格式转换;
3、以上demo针对本人读取的excel表格测试是可以的,具体需要根据你excel中的内容做相应变更即可,
但大体解析流程是一样的!
excel源数据表格:
保存为xml表格:
ps:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:
在线xml/json互相转换工具:
在线格式化xml/在线压缩xml:
xml在线压缩/格式化工具:
xml代码在线格式化美化工具:
更多关于android相关内容感兴趣的读者可查看本站专题:《android操作xml数据技巧总结》、《android编程之activity操作技巧总结》、《android资源操作技巧汇总》、《android文件操作技巧汇总》、《android开发入门与进阶教程》、《android视图view技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。
上一篇: php实现多城市切换特效