怎样在Android中解析doc、docx、xls、xlsx格式文件?

来源:互联网 发布:three.js全景源码特效 编辑:程序博客网 时间:2024/04/27 20:41
可以直接复制实验,
       解析doc,要tm-extractors-0.4.jar这个包
       解析xls,要jxl.jar这个包

view source
print?
01public static String readDOC(String path) {
02                // 创建输入流读取doc文件
03                FileInputStream in;
04                String text =null;
05//                Environment.getExternalStorageDirectory().getAbsolutePath()+ "/aa.doc")
06                try{
07                        in =new FileInputStream(newFile(path));
08                        inta= in.available();
09                        WordExtractor extractor =null;
10                        // 创建WordExtractor
11                        extractor =new WordExtractor();
12                        // 对doc文件进行提取
13                        text = extractor.extractText(in);
14                        System.out.println("解析得到的东西"+text);
15                }catch (FileNotFoundException e) {
16                        e.printStackTrace();
17                }catch (Exception e) {
18                        e.printStackTrace();
19                }
20                if(text == null) {
21                        text ="解析文件出现问题";
22                }
23                returntext;
24        }
       解析xls
view source
print?
01public static String readXLS(String path) {
02                String str ="";
03                try{
04                        Workbook workbook =null;
05                        workbook = Workbook.getWorkbook(newFile(path));
06                        Sheet sheet = workbook.getSheet(0);
07                        Cell cell =null;
08                        intcolumnCount = sheet.getColumns();
09                        introwCount = sheet.getRows();
10                        for(int i = 0; i < rowCount; i++) {
11                                for(int j = 0; j < columnCount; j++) {
12                                        cell = sheet.getCell(j, i);
13                                        String temp2 ="";
14                                        if(cell.getType() == CellType.NUMBER) {
15                                                temp2 = ((NumberCell) cell).getValue() +"";
16                                        }else if(cell.getType() == CellType.DATE) {
17                                                temp2 ="" + ((DateCell) cell).getDate();
18                                        }else {
19                                                temp2 ="" + cell.getContents();
20                                        }
21                                        str = str +"  " + temp2;
22                                }
23                                str +="\n";
24                        }
25                        workbook.close();
26                }catch (Exception e) {
27                }
28                if(str == null) {
29                        str ="解析文件出现问题";
30                }
31                returnstr;
32        }