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

可以直接复制实验,
       解析doc,要tm-extractors-0.4.jar这个包
       解析xls,要jxl.jar这个包

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(new File(path));
08                        int a= 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                return text;
24        }
       解析xls
01public static String readXLS(String path) {
02                String str = "";
03                try {
04                        Workbook workbook = null;
05                        workbook = Workbook.getWorkbook(new File(path));
06                        Sheet sheet = workbook.getSheet(0);
07                        Cell cell = null;
08                        int columnCount = sheet.getColumns();
09                        int rowCount = 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                return str;
32        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值