MDA/MDD/TDD/DDD/DDDDDDD
          posts - 536, comments - 111, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          POI處理Excel公式的亂碼問題

          Posted on 2009-07-29 15:59 leekiang 閱讀(1378) 評論(0)  編輯  收藏 所屬分類: 文件處理
          前幾天做項目的過程中,利用到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
          主站蜘蛛池模板: 鹿邑县| 阿坝| 景洪市| 晋城| 鄂托克前旗| 连州市| 建湖县| 曲靖市| 义乌市| 武威市| 怀仁县| 阿城市| 漯河市| 甘孜县| 中西区| 阿鲁科尔沁旗| 祁东县| 蒲城县| 镇康县| 澄江县| 普格县| 桂平市| 五常市| 卓尼县| 平武县| 余姚市| 阿图什市| 扶绥县| 湘潭市| 沧州市| 桃源县| 连山| 新乡市| 旅游| 始兴县| 塘沽区| 厦门市| 清远市| 陆川县| 佛山市| 石渠县|