<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    用poi生成鏈接

    Posted on 2008-10-21 22:11 leekiang 閱讀(4423) 評論(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
    謝謝,學習了
    主站蜘蛛池模板: 色妞www精品视频免费看| 亚洲色大成网站www| 国产精品免费看久久久香蕉 | 亚欧免费视频一区二区三区| 亚洲日本va在线视频观看| 久久99精品免费一区二区| 国产aⅴ无码专区亚洲av麻豆| 久久www免费人成看国产片| 亚洲精品成人片在线观看精品字幕| 99免费在线视频| 666精品国产精品亚洲| 在线观看成人免费视频不卡| 亚洲人配人种jizz| 日韩一品在线播放视频一品免费| 色噜噜的亚洲男人的天堂| 午夜亚洲av永久无码精品| 中文在线免费不卡视频| 亚洲一区二区三区日本久久九| 成人浮力影院免费看| 亚洲国产精品无码久久| 亚洲第一页综合图片自拍| 你懂的在线免费观看| 亚洲日韩乱码中文无码蜜桃| 在线a人片天堂免费观看高清| 久久精品国产亚洲AV| 亚洲国产一成人久久精品| 亚洲大片免费观看| 在线看片无码永久免费视频| 精品四虎免费观看国产高清午夜| 久久综合日韩亚洲精品色| 少妇太爽了在线观看免费视频| 亚洲宅男精品一区在线观看| 免费一级e一片在线播放| 最新亚洲成av人免费看| 亚洲一区欧洲一区| 亚洲午夜久久久久妓女影院 | 国产福利电影一区二区三区,免费久久久久久久精 | 日韩一卡2卡3卡4卡新区亚洲| 日本免费xxxx色视频| 国产精品亚洲专区一区| 亚洲A∨无码一区二区三区|