«

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

时间:2024-3-2 19:06     作者:韩俊     分类: Android


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

解析xls,要jxl.jar这个包下载jxl.jar

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

            return str;
    }</pre><br />




标签: android

热门推荐