在Android讀取Word文件時,在網上查看時能夠用tm-extractors,但好像沒有提到怎麼讀取Word文檔中字體的顏色,字體,上下標等相關的屬性。但因爲須要,要把doc文檔中的內容(字體,下劃線,顏色等)讀取應用到android中(不包括圖片和圖表)。html
後面採用的是poi三方jar包(原包太大,能夠從源代碼裏本身抽取有用的一些代碼減小包的大小)。android
個人想法是:把doc中的內容解析出來後,加上html對應的標籤,在android中經過Html.fromHtml在TextView中進行顯示,或者經過WebView.loadData進行加載顯示app
但測試後,發現若是加載太多內容的話,在Android中效率不行。測試
效果(該圖的效果是在TextView中的效果,在WebView中效果會更好些):字體
doc圖:spa
![](http://static.javashuo.com/static/loading.gif)
android圖:htm
![](http://static.javashuo.com/static/loading.gif)
作法1:(解析爲span樣式的,這種作法只能用WebView方式加載,Html.fromHtml無效)圖片
- public static String readDocToSpanByRun(InputStream inputStream) {
- HWPFDocument hwpfDocument = null;
- if(inputStream == null)
- throw new RuntimeException("inputStream is null ...");
- try{
- hwpfDocument = new HWPFDocument(inputStream);
- }catch(Exception e) {
- throw new RuntimeException("HWPFDocment Exception", e);
- }
- Range allRange = hwpfDocument.getRange();
- int length = allRange.numCharacterRuns();
- StringBuffer sb = new StringBuffer();
- CharacterRun cur;
- String text = "";
- for (int i = 0; i < length; i++) {
- cur = allRange.getCharacterRun(i);
- sb.append(CharacterRunUtils.toSpanType(cur));
- text = CharacterRunUtils.getSpicalSysbomByRun(cur.text());
- if(cur.getSubSuperScriptIndex() == 1)
- sb.append("<sup>").append(text).append("</sup>");
- else if(cur.getSubSuperScriptIndex() == 2)
- sb.append("<sub>").append(text).append("</sub>");
- else
- sb.append(text);
- sb.append("</span>");
- }
- return sb.toString();
- }
-
作法2:(解析爲font樣式的,Html.fromHtml有效,但對應size的設置無效果)ip
- public static String readDocToHtml(InputStream inputStream) {
- HWPFDocument hwpfDocument = null;
- if(inputStream == null)
- throw new RuntimeException("inputStream is null ...");
- try{
- hwpfDocument = new HWPFDocument(inputStream);
- }catch(Exception e) {
- throw new RuntimeException("HWPFDocment Exception", e);
- }
- CharacterRun cur = null;
- StringBuffer sb = new StringBuffer();
- StringBuffer charStr = new StringBuffer();
- Range allRange = hwpfDocument.getRange();
- for(int i = 0; i < allRange.numCharacterRuns(); i++) {
- cur = allRange.getCharacterRun(i);
- sb.append(CharacterRunUtils.fontFaceColorSizeToHtml(cur));
- charStr.append(CharacterRunUtils.toSupOrSub(cur, CharacterRunUtils.getSpicalSysbomByRun(cur.text())));
- if(cur.isBold()) {
- charStr.insert(0, "<b>");
- charStr.insert(charStr.length(), "</b>");
- }
- if(cur.getUnderlineCode() != 0) {
- charStr.insert(0, "<u>");
- charStr.insert(charStr.length(), "</u>");
- }
- if(cur.isItalic()) {
- charStr.insert(0, "<i>");
- charStr.insert(charStr.length(), "</i>");
- }
- if(cur.isStrikeThrough()) {
- charStr.insert(0, "<s>");
- charStr.insert(charStr.length(), "</s>");
- }
- sb.append(charStr).append("</font>");
- charStr.setLength(0);
- }
- hwpfDocument = null;
- return sb.toString();
- }
如下是會用到的方法:文檔
用到的顏色的轉換(進行簡單的顏色轉換)
- public class ColorUtils {
-
- public static int red(int c) {
- return c & 0XFF;
- }
-
- public static int green(int c) {
- return (c >> 8) & 0XFF;
- }
-
- public static int blue(int c) {
- return (c >> 16) & 0XFF;
- }
-
- public static int rgb(int c) {
- return (red(c) << 16) | (green(c) <<8) | blue(c);
- }
-
- public static String rgbToSix(String rgb) {
- int length = 6 - rgb.length();
- String str = "";
- while(length > 0){
- str += "0";
- length--;
- }
- return str + rgb;
- }
-
- public static String getHexColor(int color) {
- color = color == -1 ? 0 : color;
- int rgb = rgb(color);
- return "#" + rgbToSix(Integer.toHexString(rgb));
- }
- }