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

          用poi生成鏈接

          Posted on 2008-10-21 22:11 leekiang 閱讀(4428) 評論(1)  編輯  收藏 所屬分類: 文件處理
          1,
          一個需求, 要求報表生成的Excel表格支持超鏈接。例如點擊Excel內的公司名, 自動打開瀏覽器并連到該公司的網站上去。在Excel里面選中所需的單元格, 右鍵彈出屬性, 選超鏈接就能輸入相應的地址了,既然Excel支持超鏈接。那就沒有什么借口說不能實現了。:).

          翻了翻POI的文檔, 很容易就找到了解決方案。在POI中讓單元格實現超鏈接功能, 可以用Hyperlink 函數。HYPERLINK函數包含兩個參數,第一個參數是指向的地址,第二個參數是顯示的字符串

          cell = row.createCell(colNumber)。
          cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
          cell.setCellFormula("HYPERLINK(\"" + "Http://www.google.ca"+ "\",\"" + "Google Canada"+ "\")");
          或cell.setCellFormula("HYPERLINK(\"[workbook.xls]'sheet2'!A1\",\"homepage\")"); //HYPERLINK("#明細!A1","homepage"),#代表本工作簿,來源http://club.excelhome.net/thread-54081-1-1.html

          現在超鏈接單元格看起來和一般的單元格沒有分別, 除非你把鼠標放上去才會變成手行光標。 為了和一般的習慣相符, 還需要把字符顏色變成藍色和加上下劃線。 這就要用到 style了、

          HSSFCellStyle linkStyle = workbook.createCellStyle();
          HSSFFont cellFont= workbook.createFont();
          cellFont.setUnderline((byte) 1);
          cellFont.setColor(HSSFColor.BLUE.index);
          linkStyle.setFont(cellFont);

          最后把style應用到cell上去就大功告成了。
          cell.setCellStyle(linkStyle);

          以上修改自http://sunnylei2008.blogspot.com/2007/07/poihssf.html
          和http://diystyle.javaeye.com/blog/132093
          還有http://blog.csdn.net/xunyiren/archive/2007/03/08/1524533.aspx

          2,
          以下來自http://www.javaeye.com/topic/25569,是用jxl解決的。
          看了POI文檔,找到一個LinkedDataFormulaField 和LinkedDataRecord,jxl文檔里有 Hyperlink,現在想對一個excel中的一組sheet做一個索引,方便查找每張sheet,
          String outputFile="D:/導出接口.xls";
          try
          {
          Workbook wb=Workbook.getWorkbook(new File(outputFile)); //Excel獲得文件
          //打開一個文件的副本,并且指定數據寫回到原文件
          WritableWorkbook book=Workbook.createWorkbook(new File(outputFile),wb);
          WritableSheet sheet=book.createSheet("導出目標",0); //添加一個工作表
          String[] oriSheetNames=wb.getSheetNames();? //獲得源excel文件中的所有sheet名稱
          ?for(int i=0;i<book.getNumberOfSheets();i++)
          ?{
          ??? sheet.addCell(new Label(0,i+1,String.valueOf(i+1)));? //第一列寫入編號
          /**
          * public WritableHyperlink(int col,int row,java.lang.String desc,WritableSheet sheet,int destcol,int destrow)
          * Constructs a hyperlink to some cells within this workbook
          * col - the column containing this hyperlink
          * row - the row containing this hyperlink
          * desc - the cell contents for this hyperlink
          * sheet - the sheet containing the cells to be linked to
          * destcol - the column number of the first destination linked cell
          * destrow - the row number of the first destination linked cell
          * */
          sheet.addHyperlink(new WritableHyperlink(1,i+1,oriSheetNames[i],book.getSheet(oriSheetNames[i]),0,0));
          book.write();
          book.close();
          wb.close();
          }catch(IOException e)
          {
          ??? System.out.println("異常: "+e);
          }
          catch(BiffException e)
          {
          ??? System.out.println("異常: "+e);
          }
          catch(RowsExceededException e)
          {
          ??? System.out.println("異常: "+e);
          }
          catch(WriteException e)
          {
          ??? System.out.println("異常: "+e);
          }

          3,公式里的亂碼如何解決,來源:http://topic.csdn.net/t/20060309/10/4602637.html,作者阿水
          前幾天做項目的過程中,利用到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的描述進行編譯輸出。 ?

          后注:按上面的辦法,中文問題是解決了,但點擊鏈接還是報"引用無效",觀察了一會,原來是sheet名帶有括號造成的。
          ?


          評論

          # re: 用poi生成鏈接  回復  更多評論   

          2012-01-05 10:46 by caoshl
          謝謝,學習了
          主站蜘蛛池模板: 拜城县| 怀宁县| 文成县| 靖远县| 舒兰市| 米脂县| 乐昌市| 保靖县| 汝州市| 夏河县| 鲁甸县| 专栏| 南溪县| 区。| 呼玛县| 墨竹工卡县| 满城县| 罗平县| 上杭县| 淅川县| 盈江县| 澄迈县| 高青县| 新沂市| 莱西市| 井冈山市| 葫芦岛市| 鄯善县| 西华县| 鄱阳县| 嘉兴市| 阿坝县| 东台市| 南阳市| 科技| 江山市| 广元市| 舞钢市| 惠州市| 霍城县| 西峡县|