本文实例讲述了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> mapList = ReadExcelFile();
System.out.println("excel size= " + mapList.size() + " ");
List namelists = readCol5Name();
System.out.println("namelists= " + namelists.size() + " ");
writeXmlFile(mapList, namelists);
}
private static List readSheetName() {
InputStream is = null;
Workbook wb = null;
java.util.List 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;
}
private static List readCol5Name() {
InputStream is = null;
Workbook wb = null;
java.util.List 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;
}
private static Map> ReadExcelFile() {
InputStream is = null;
Workbook wb = null;
Map> mapList = null;
Map maps = null;
java.util.List
提示:
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互相转换工具:
http://tools.jb51.net/code/xmljson
在线格式化XML/在线压缩XML:
http://tools.jb51.net/code/xmlformat
XML在线压缩/格式化工具:
http://tools.jb51.net/code/xml_format_compress
XML代码在线格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android操作XML数据技巧总结》、《Android编程之activity操作技巧总结》、《Android资源操作技巧汇总》、《Android文件操作技巧汇总》、《Android开发入门与进阶教程》、《Android视图View技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。



