android 解析xls文件,Android中解析doc、docx、xls、xlsx格式文件

解析doc,要tm-extractors-0.4.jar这个包

解析xls,要jxl.jar这个包

----------------------------------

1.  解析doc

public static String readDOC(String path) {

// 创建输入流读取doc文件

FileInputStream in;

String text = null;

//                Environment.getExternalStorageDirectory().getAbsolutePath()+ "/aa.doc")

try {

in = new FileInputStream(new File(path));

int a= in.available();

WordExtractor extractor = null;

// 创建WordExtractor

extractor = new WordExtractor();

// 对doc文件进行提取

text = extractor.extractText(in);

System.out.println("解析得到的东西"+text);

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

if (text == null) {

text = "解析文件出现问题";

}

return text;

}

2. 解析xls

public static String readXLS(String path) {

String str = "";

try {

Workbook workbook = null;

workbook = Workbook.getWorkbook(new File(path));

Sheet sheet = workbook.getSheet(0);

Cell cell = null;

int columnCount = sheet.getColumns();

int rowCount = sheet.getRows();

for (int i = 0; i < rowCount; i++) {

for (int j = 0; j < columnCount; j++) {

cell = sheet.getCell(j, i);

String temp2 = "";

if (cell.getType() == CellType.NUMBER) {

temp2 = ((NumberCell) cell).getValue() + "";

} else if (cell.getType() == CellType.DATE) {

temp2 = "" + ((DateCell) cell).getDate();

} else {

temp2 = "" + cell.getContents();

}

str = str + "  " + temp2;

}

str += "\n";

}

workbook.close();

} catch (Exception e) {

}

if (str == null) {

str = "解析文件出现问题";

}

return str;

}

3. 解析docx

public static String readDOCX(String path) {

String river = "";

try {

ZipFile xlsxFile = new ZipFile(new File(path));

ZipEntry sharedStringXML = xlsxFile.getEntry("word/document.xml");

InputStream inputStream = xlsxFile.getInputStream(sharedStringXML);

XmlPullParser xmlParser = Xml.newPullParser();

xmlParser.setInput(inputStream, "utf-8");

int evtType = xmlParser.getEventType();

while (evtType != XmlPullParser.END_DOCUMENT) {

switch (evtType) {

case XmlPullParser.START_TAG:

String tag = xmlParser.getName();

System.out.println(tag);

if (tag.equalsIgnoreCase("t")) {

river += xmlParser.nextText() + "\n";

}

break;

case XmlPullParser.END_TAG:

break;

default:

break;

}

evtType = xmlParser.next();

}

} catch (ZipException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (XmlPullParserException e) {

e.printStackTrace();

}

if (river == null) {

river = "解析文件出现问题";

}

return river;

}

4. 解析xlsx public static String readXLSX(String path) {                 String str = "";                 String v = null;                 boolean flat = false;                 List ls = new ArrayList();                 try {                         ZipFile xlsxFile = new ZipFile(new File(path));                         ZipEntry sharedStringXML = xlsxFile                                         .getEntry("xl/sharedStrings.xml");                         InputStream inputStream = xlsxFile.getInputStream(sharedStringXML);                         XmlPullParser xmlParser = Xml.newPullParser();                         xmlParser.setInput(inputStream, "utf-8");                         int evtType = xmlParser.getEventType();                         while (evtType != XmlPullParser.END_DOCUMENT) {                                 switch (evtType) {                                 case XmlPullParser.START_TAG:                                         String tag = xmlParser.getName();                                         if (tag.equalsIgnoreCase("t")) {                                                 ls.add(xmlParser.nextText());                                         }                                         break;                                 case XmlPullParser.END_TAG:                                         break;                                 default:                                         break;                                 }                                 evtType = xmlParser.next();                         }                         ZipEntry sheetXML = xlsxFile.getEntry("xl/worksheets/sheet1.xml");                         InputStream inputStreamsheet = xlsxFile.getInputStream(sheetXML);                         XmlPullParser xmlParsersheet = Xml.newPullParser();                         xmlParsersheet.setInput(inputStreamsheet, "utf-8");                         int evtTypesheet = xmlParsersheet.getEventType();                         while (evtTypesheet != XmlPullParser.END_DOCUMENT) {                                 switch (evtTypesheet) {                                 case XmlPullParser.START_TAG:                                         String tag = xmlParsersheet.getName();                                         if (tag.equalsIgnoreCase("row")) {                                         } else if (tag.equalsIgnoreCase("c")) {                                                 String t = xmlParsersheet.getAttributeValue(null, "t");                                                 if (t != null) {                                                         flat = true;                                                         System.out.println(flat + "有");                                                 } else {                                                         System.out.println(flat + "没有");                                                         flat = false;                                                 }                                         } else if (tag.equalsIgnoreCase("v")) {                                                 v = xmlParsersheet.nextText();                                                 if (v != null) {                                                         if (flat) {                                                                 str += ls.get(Integer.parseInt(v)) + "  ";                                                         } else {                                                                 str += v + "  ";                                                         }                                                 }                                         }                                         break;                                 case XmlPullParser.END_TAG:                                         if (xmlParsersheet.getName().equalsIgnoreCase("row")                                                         && v != null) {                                                 str += "\n";                                         }                                         break;                                 }                                 evtTypesheet = xmlParsersheet.next();                         }                         System.out.println(str);                 } catch (ZipException e) {                         e.printStackTrace();                 } catch (IOException e) {                         e.printStackTrace();                 } catch (XmlPullParserException e) {                         e.printStackTrace();                 }                 if (str == null) {                         str = "解析文件出现问题";                 }                 return str;         }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值