可以直接复制实验,
解析doc,要tm-extractors-0.4.jar这个包
解析xls,要jxl.jar这个包
解析xls
解析doc,要tm-extractors-0.4.jar这个包
解析xls,要jxl.jar这个包
01 | public 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 | } |
01 | public 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 | } |