简体   繁体   中英

How to display .docx file contents in a textview on Android Activity?

I am trying to display the contents of .doc/.docx file in the textview in Android Activity by using the code.

File sdcard = Environment.getExternalStorageDirectory();

    File file = new File(sdcard,"demo.docx");

    StringBuilder text = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) {
            text.append(line);
            text.append('\n');
        }
        br.close();
    }
    catch (IOException e) {
    }
    output.setText(text);

But, I am getting output like this,

http://i.stack.imgur.com/TAqCW.jpg

It works fine for .txt file but not for .doc/.docx file. Please Help me, how to display the contents of .doc/.docx file in textview in android activity?

Open Microsoft Word docx file with Java

Check out that question for a general solution for parsing text.

I had the same problem but I wanted to pass the text into a java textArea not an android textView. For and Android specific solutio check out

https://github.com/plutext/AndroidDocxToHtml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM