多餘的代碼網上都有就不多貼了 我這邊的sheet來自於writer 各位看官可自行改成自己的
path是地址,name是那一列存的附件名稱,i是行數 j是列數
private void createSuperLink(ExcelWriter writer,String path,String name,int i,int j){
Workbook workbook = writer.getWorkbook();
Sheet sheet = writer.getSheet();
// 獲取行
Row row = sheet.getRow(i+1);
if(row==null){
row = sheet.createRow(i+1);
}
// 獲取列
Cell cell = row.getCell(j+6);
if(cell==null){
cell = row.createCell(j+6);
}
CreationHelper createHelper = workbook.getCreationHelper();
CellStyle linkStyle = workbook.createCellStyle();
Font cellFont = workbook.createFont();
cellFont.setColor(IndexedColors.BLUE.index);
linkStyle.setFont(cellFont);
//底部邊框
linkStyle.setBorderTop(BorderStyle.THIN);
linkStyle.setTopBorderColor(IndexedColors.BLACK.index);
//底部邊框
linkStyle.setBorderBottom(BorderStyle.THIN);
linkStyle.setBottomBorderColor(IndexedColors.BLACK.index);
//左邊框顏色
linkStyle.setBorderRight(BorderStyle.THIN);
linkStyle.setRightBorderColor(IndexedColors.BLACK.index);
cell.setCellStyle(linkStyle);
// 地址存在時纔是超鏈接
if(path!=null){
Hyperlink link = createHelper.createHyperlink(HyperlinkType.URL);
link.setAddress(path);
cell.setHyperlink(link);
}
cell.setCellValue(name);
}
實現效果
還有由於我的附件頭部是後期合併 因爲要計算附件的最大數目進行合併,導致了右邊框沒有邊線
代碼如下max爲附件最大個數 其中本來我只用了獲取列 結果發現設置邊線失敗 後面用獲取尾列才ok的
private void createAppendHead(ExcelWriter writer,int max){
Sheet sheet = writer.getSheet();
// 合併
sheet.addMergedRegion(new CellRangeAddress(0,0,6,max+5));
// 獲取行
Cell headCell = sheet.getRow(0).getCell(0);
// 獲取列
Cell targetCell = sheet.getRow(0).createCell(6);
// 獲取合併列尾
Cell targetEndCell = sheet.getRow(0).createCell(5+max);
// 設置樣式
targetCell.setCellStyle(headCell.getCellStyle());
targetEndCell.setCellStyle(headCell.getCellStyle());
// 設置內容
targetCell.setCellValue("附件");
}