2

我是安卓新手。在我的应用程序中,我需要阅读 .doc 和 .docx 以及其他办公套装文件。这取决于他们的存在。我想在我的应用程序中不使用 Intent 来阅读它。我尝试了许多代码,但它们无法正常工作。我的代码如下所示,使用各种模块。我应该做哪些更改,或者是否有任何其他方法可以读取这些文件?

我的代码是:

/**
     *  Apache OPI Code.
     */
    if(Environment.getExternalStorageState().equalsIgnoreCase("y")){
        TextView tv = (TextView) findViewById(R.id.textview_data_rla);
        WordExtractor extractor = null;
        try {
           File file = new File(getExternalFilesDir(null),"n.doc");
           FileInputStream fis=new FileInputStream(file.getAbsolutePath());
           Log.d("File", fis.toString());
           HWPFDocument document=new HWPFDocument(fis);
           extractor = new WordExtractor(document);
           String[] fileData = extractor.getParagraphText();
           for(int i=0;i<fileData.length;i++){
             if(fileData[i] != null){
               tv.setText(fileData[i]);
               Log.d("file",fileData[i]);
             }
           }
        }
        catch(Exception exep){

        }
    }

    /**
     *  Using HWPFDocument.
     */

    try {
        TextView tv = (TextView) findViewById(R.id.textview_data_rla);

        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard,"n.doc");

        //InputStream fis = new FileInputStream(file);
        POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));  
        HWPFDocument doc = new HWPFDocument(fs);  

        Range range = doc.getRange();  
        CharacterRun run = null;
        for (int i=0; i<range.numCharacterRuns(); i++) {  
            run = range.getCharacterRun(i);  
            Log.d("character  run",String.valueOf(i+1));
            Log.d("Text",run.text().toString());
        }
        tv.setText(run.text().toString());
        OutputStream out = new FileOutputStream(new File("/mnt/sdcard/new.doc"));
        doc.write(out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  

    /**
     *  Apache POI using NPOIFSFileSystem.
     */
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,"n.doc");
    WordExtractor extractor1 = null;
    try{
        NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
        extractor1 = new WordExtractor(fs.getRoot(), null);
    } catch (Exception e){
        e.printStackTrace();
    }

    for(String rawText : extractor1.getParagraphText()) {
    String text = WordExtractor.stripFields(rawText);
    Log.d("text",text);
    }

    /**
     *  Apache Tika code.
     */

    TikaConfig tika = TikaConfig.getDefaultConfig();
    TikaInputStream stream = TikaInputStream.get(file);
    ContentHandler handler = new BodyContentHandler();
    Metadata metadata = new Metadata();
    tika.getParser().parse(file, handler, metadata, new ParseContext());
    String text = handler.toString();


    /**
     *  Code using jOpenDocument.jar
     */

    final OpenDocument doc = new OpenDocument();
    doc.loadFrom("invoice.ods");

    // Show time !
    final JFrame f = new JFrame("Invoice Viewer");
    ODSViewerPanel viewer = new ODSViewerPanel(doc);
    f.setContentPane(viewer);
    f.pack();
    f.setVisible(true);
}

这是我必须使用的正确方法。

请尽可能尽快回复。提前致谢。

4

0 回答 0