前幾天做項目的過程中,利用到Apache項目中的POI來實現基于Excel的數據模板輸出,其中利用公式的方式嵌入超鏈接進行網頁鏈接訪問。 ?
? 自己做了一些處理EXCEL單元格的方法,但在進行公式處理時,由于POI的問題,顯示的公式信息一直都是亂碼,后來在網上找到一些朋友關于這些問題的解 決方法,感覺幫助很大。因此,結合自己的實踐經驗,把修改POI內部源碼的過程寫出來,以其對資料做一整理,希望對后來的朋友也有所幫助。 ?
? ?
? 1、首先,上網找到POI的發布版本的源碼,我下的是poi-src-2.5.1-final-20040804.zip這個版本。 ?
? 2、找到StringPtg.java這個文件,在解壓后的\src\java\org\apache\poi\hssf\record\formula文件夾下面 ?
? 3、利用文本編輯工具對StringPtg.java進行編輯 ?
? 4、找到public ? StringPtg(byte ? [] ? data, ? int ? offset)這個方法, ?
? 對其修改如下 ?
? /** ? Create ? a ? StringPtg ? from ? a ? byte ? array ? read ? from ? disk ? */ ?
? ? ? ? ? public ? StringPtg(byte ? [] ? data, ? int ? offset) ?
? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? offset++; ?
? ? ? ? ? ? ? ? ? field_1_length ? = ? data[offset]; ?
? ? ? ? ? ? ? ? ? field_2_options ? = ? data[offset+1]; ?
? ? ? ? ? ? ? ? ? if ? (fHighByte.isSet(field_2_options)) ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? // ? modified ? by ? rainsoft ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? // ? in ? excel ? chinese ? is ? stored ? two ? bytes ? HIGH ? bytes,LOW ? bytes ?
? ? ? ? ? ? ? ? ? ? ? ? ? // ? field_3_string= ? StringUtil.getFromUnicode(data,offset+2,field_1_length); ?
? ? ? ? ? ? ? ? ? ? ? ? ? field_3_string= ? StringUtil.getFromUnicodeHigh(data,offset+2,field_1_length); ?
? ? ? ? ? ? ? ? ? }else ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? field_3_string=StringUtil.getFromCompressedUnicode(data,offset+2,field_1_length); ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? //setValue(new ? String(data, ? offset+3, ? data[offset+1] ? + ? 256*data[offset+2])); ?
? ? ? ? ? } ?
? 其中主要利用getFromUnicodeHigh方法替換原有的方法進行處理。 ?
? 5、再查找StringPtg(String ? value),做如下的修改, ?
? ?
? public ? StringPtg(String ? value) ? { ?
? ? ? ? ? ? ? ? ? if ? (value.length() ? >255) ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? throw ? new ? IllegalArgumentException("String ? literals ? in ? formulas ? cant ? be ? bigger ? than ? 255 ? characters ? ASCII"); ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? ? this.field_2_options=0; ?
? ? ? ? ? ? ? ? ? // ? add ? by ? rainsoft ?
? ? ? ? ? ? ? ? ? // ? two ? bytes ? char ? options ? must ? be ? "1" ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? try ? { ?
? ? ? ? ? ? ? ? ? ? ? if ? (value.length()!=value.getBytes("GBK").length) ?
? ? ? ? ? ? ? ? ? ? ? ? ? this.field_2_options=1; ?
? ? ? ? ? ? ? ? ? } ? catch ? (Exception ? e) ? { ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? ? // ? end ? add ?
? ? ? ? ? ? ? ? ? this.fHighByte.setBoolean(field_2_options, ? false); ?
? ? ? ? ? ? ? ? ? this.field_3_string=value; ?
? ? ? ? ? ? ? ? ? this.field_1_length=(byte)value.length(); ? //for ? the ? moment, ? we ? support ? only ? ASCII ? strings ? in ? formulas ? we ? create ?
? ? ? ? ? } ?
? ?
? 6、至此對源文件的修改就結束了,下一步則需要對其進行編譯輸出。 ?
? 7、利用docs/howtobuild.html的描述進行編譯輸出。 ?
? 8、成功編譯輸出的POI???????.jar文件,復制到原有的編譯路徑,替換到原有的文件即可,最好刪除原有的 ? POI文件。?
http://topic.csdn.net/t/20060309/10/4602637.html
? 自己做了一些處理EXCEL單元格的方法,但在進行公式處理時,由于POI的問題,顯示的公式信息一直都是亂碼,后來在網上找到一些朋友關于這些問題的解 決方法,感覺幫助很大。因此,結合自己的實踐經驗,把修改POI內部源碼的過程寫出來,以其對資料做一整理,希望對后來的朋友也有所幫助。 ?
? ?
? 1、首先,上網找到POI的發布版本的源碼,我下的是poi-src-2.5.1-final-20040804.zip這個版本。 ?
? 2、找到StringPtg.java這個文件,在解壓后的\src\java\org\apache\poi\hssf\record\formula文件夾下面 ?
? 3、利用文本編輯工具對StringPtg.java進行編輯 ?
? 4、找到public ? StringPtg(byte ? [] ? data, ? int ? offset)這個方法, ?
? 對其修改如下 ?
? /** ? Create ? a ? StringPtg ? from ? a ? byte ? array ? read ? from ? disk ? */ ?
? ? ? ? ? public ? StringPtg(byte ? [] ? data, ? int ? offset) ?
? ? ? ? ? { ?
? ? ? ? ? ? ? ? ? offset++; ?
? ? ? ? ? ? ? ? ? field_1_length ? = ? data[offset]; ?
? ? ? ? ? ? ? ? ? field_2_options ? = ? data[offset+1]; ?
? ? ? ? ? ? ? ? ? if ? (fHighByte.isSet(field_2_options)) ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? // ? modified ? by ? rainsoft ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? // ? in ? excel ? chinese ? is ? stored ? two ? bytes ? HIGH ? bytes,LOW ? bytes ?
? ? ? ? ? ? ? ? ? ? ? ? ? // ? field_3_string= ? StringUtil.getFromUnicode(data,offset+2,field_1_length); ?
? ? ? ? ? ? ? ? ? ? ? ? ? field_3_string= ? StringUtil.getFromUnicodeHigh(data,offset+2,field_1_length); ?
? ? ? ? ? ? ? ? ? }else ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? field_3_string=StringUtil.getFromCompressedUnicode(data,offset+2,field_1_length); ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? //setValue(new ? String(data, ? offset+3, ? data[offset+1] ? + ? 256*data[offset+2])); ?
? ? ? ? ? } ?
? 其中主要利用getFromUnicodeHigh方法替換原有的方法進行處理。 ?
? 5、再查找StringPtg(String ? value),做如下的修改, ?
? ?
? public ? StringPtg(String ? value) ? { ?
? ? ? ? ? ? ? ? ? if ? (value.length() ? >255) ? { ?
? ? ? ? ? ? ? ? ? ? ? ? ? throw ? new ? IllegalArgumentException("String ? literals ? in ? formulas ? cant ? be ? bigger ? than ? 255 ? characters ? ASCII"); ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? ? this.field_2_options=0; ?
? ? ? ? ? ? ? ? ? // ? add ? by ? rainsoft ?
? ? ? ? ? ? ? ? ? // ? two ? bytes ? char ? options ? must ? be ? "1" ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? try ? { ?
? ? ? ? ? ? ? ? ? ? ? if ? (value.length()!=value.getBytes("GBK").length) ?
? ? ? ? ? ? ? ? ? ? ? ? ? this.field_2_options=1; ?
? ? ? ? ? ? ? ? ? } ? catch ? (Exception ? e) ? { ?
? ? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? ? ? ? // ? end ? add ?
? ? ? ? ? ? ? ? ? this.fHighByte.setBoolean(field_2_options, ? false); ?
? ? ? ? ? ? ? ? ? this.field_3_string=value; ?
? ? ? ? ? ? ? ? ? this.field_1_length=(byte)value.length(); ? //for ? the ? moment, ? we ? support ? only ? ASCII ? strings ? in ? formulas ? we ? create ?
? ? ? ? ? } ?
? ?
? 6、至此對源文件的修改就結束了,下一步則需要對其進行編譯輸出。 ?
? 7、利用docs/howtobuild.html的描述進行編譯輸出。 ?
? 8、成功編譯輸出的POI???????.jar文件,復制到原有的編譯路徑,替換到原有的文件即可,最好刪除原有的 ? POI文件。?
http://topic.csdn.net/t/20060309/10/4602637.html