在Android讀取Word文件時,在網上查看時可以用tm-extractors,但好像沒有提到怎么讀取Word文檔中字體的顏色,字體,上下標等相關的屬性。但由於需要,要把doc文檔中的內容(字體,下划線,顏色等)讀取應用到android中(不包括圖片和圖表)。
后面采用的是poi三方jar包(原包太大,可以從源代碼里自己抽取有用的一些代碼減少包的大小)。
我的想法是:把doc中的內容解析出來后,加上html對應的標簽,在android中通過Html.fromHtml在TextView中進行顯示,或者通過WebView.loadData進行加載顯示
但測試后,發現如果加載太多內容的話,在Android中效率不行。
效果(該圖的效果是在TextView中的效果,在WebView中效果會更好些):
doc圖:

android圖:

做法1:(解析為span樣式的,這種做法只能用WebView方式加載,Html.fromHtml無效)
- /**Span樣式
- * 通過字體的樣式進行加載
- * @param inputStream
- * @return
- */
- 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的設置無效果)
- /**
- * Html樣式
- * 通過字體樣式解析
- * @param inputStream
- * @return
- */
- 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 CharacterRunUtils {
- private static final short ENTER_ASCII = 13;
- private static final short SPACE_ASCII = 32;
- private static final short TABULATION_ASCII = 9;
- /**
- * 比對字體是否相同
- * 可以繼續加其它屬性
- * @param cr1
- * @param cr2
- * @return
- */
- public static boolean compareCharStyleForSpan(CharacterRun cr1,
- CharacterRun cr2) {
- return cr1.isBold() == cr2.isBold()
- && cr1.getFontName().equals(cr2.getFontName())
- && cr1.getFontSize() == cr2.getFontSize()
- && cr1.isItalic() == cr2.isItalic()
- && cr1.getColor() == cr2.getColor()
- && cr1.getUnderlineCode() == cr2.getUnderlineCode()
- && cr1.isStrikeThrough() == cr2.isStrikeThrough()
- && cr1.getColor() == cr2.getColor();
- }
- public static boolean compareCharColor(CharacterRun cr1, CharacterRun cr2) {
- return cr1.getFontName().equals(cr2.getFontName())
- && cr1.getFontSize() == cr2.getFontSize()
- && cr1.getColor() == cr2.getColor();
- }
- public static String getSpicalSysbom(char currentChar) {
- String tempStr = "";
- if (currentChar == ENTER_ASCII) {
- tempStr += "<br/>";
- } else if (currentChar == SPACE_ASCII) {
- tempStr += " ";
- } else if (currentChar == TABULATION_ASCII) {
- tempStr += " ";
- } else {
- tempStr += currentChar;
- }
- return tempStr;
- }
- public static String getSpicalSysbomSpan(char currentChar) {
- String tempStr = "";
- if (currentChar == ENTER_ASCII) {
- tempStr += "<br/>";
- } else if (currentChar == SPACE_ASCII) {
- tempStr += " ";
- } else if (currentChar == TABULATION_ASCII) {
- tempStr += " ";
- }
- return tempStr;
- }
- /**
- * 特殊字符的取代
- * @param currentChar
- * @return
- */
- public static String getSpicalSysbomByRun(String currentChar) {
- StringBuffer tempStr = new StringBuffer();
- int length = currentChar.length();
- for (int i = 0; i < length; i++) {
- tempStr.append(getSpicalSysbom(currentChar.charAt(i)));
- }
- return tempStr.toString();
- }
- /**
- * span方式前綴
- * @param cr
- * @return
- */
- public static String toSpanType(CharacterRun cr) {
- StringBuffer spanStyle = new StringBuffer("<span style='font-family:");
- spanStyle.append(cr.getFontName()).append("; font-size:")
- .append(cr.getFontSize() / 2).append("pt;");
- if (cr.isBold())
- spanStyle.append("font-weight:bold;");
- if (cr.isItalic())
- spanStyle.append("font-style:italic;");
- if (cr.isStrikeThrough())
- spanStyle.append("text-decoration:line-through;");
- if (cr.getUnderlineCode() != 0)
- spanStyle.append("text-decoration:underline;");
- spanStyle.append("color:")
- .append(ColorUtils.getHexColor(cr.getIco24())).append(";")
- .append("'>");
- return spanStyle.toString();
- }
- /**
- * 為font方式提供<font前綴
- * @param cr
- * @return
- */
- public static String fontFaceColorSizeToHtml(CharacterRun cr) {
- StringBuffer htmlType = new StringBuffer("<font ");
- htmlType.append("size='").append(cr.getFontSize() / 2).append("' ")
- .append("face='").append(cr.getFontName()).append("' ")
- .append("color='")
- .append(ColorUtils.getHexColor(cr.getIco24())).append("'>");
- return htmlType.toString();
- }
- /**
- * 處理上下標
- * @param cr
- * @param currentChar
- * @return
- */
- public static String toSupOrSub(CharacterRun cr, String currentChar) {
- int sub = cr.getSubSuperScriptIndex();
- if (sub != 0) {
- if (sub == 1)
- // 上標
- return "<sup>" + currentChar + "</sup>";
- else
- // 下標
- return "<sub>" + currentChar + "</sub>";
- } else
- return currentChar;
- }
- public static String toSupOrSub(CharacterRun cr, char currentChar) {
- return toSupOrSub(cr, new String(new char[]{currentChar}));
- }
- }
用到的顏色的轉換(進行簡單的顏色轉換)
- 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));
- }
- }

