poi导出excel时url太长

public ByteArrayOutputStream export(ExcelData data) throws IOException {
		Workbook  workbook = new  XSSFWorkbook();//xlsx
//		Workbook  workbook = new  HSSFWorkbook();//xls
		Sheet  sheet = workbook.createSheet(ExcelConst.EXCEL_SHEET);

		// 标头样式
		CellStyle style = workbook.createCellStyle();
		style.setAlignment(HorizontalAlignment.CENTER);

		// 填充表头
		Row row = sheet.createRow(0);
		Cell cell;
		for (int i = 0; i < data.column(); i++) {
			cell = row.createCell(i);
			cell.setCellValue(data.getHead(i));
			cell.setCellStyle(style);
		}

		// 超链接样式
		CellStyle linkStyle = workbook.createCellStyle();
		Font cellFont = workbook.createFont();
		cellFont.setUnderline((byte) 1);
		cellFont.setColor(HSSFColor.BLUE.index);
		linkStyle.setFont(cellFont);

		// 填充数据
		Row newRow;
		for (int i = 0; i < data.row(); i++) {
			List<DataRow> dataRow = data.getRow(i);
			newRow = sheet.createRow(i + 1);
			for (int j = 0; j < dataRow.size(); j++) {
				Object value = dataRow.get(j).getValue();
				if (value instanceof Date) {
					newRow.createCell(j).setCellValue(
							new SimpleDateFormat(DateUtil.yyyyMMdd).format((Date) dataRow.get(j).getValue()));
				} else if (StringUtil.isNotEmpty(dataRow.get(j).getLink())) {
					Cell hyperCell = newRow.createCell(j);
					String url = dataRow.get(j).getLink();
					url = url.startsWith("http://") ? url : "http://" + url;
					hyperCell.setCellType(CellType.STRING);
					hyperCell.setCellStyle(linkStyle);
//						hyperCell.setCellFormula("HYPERLINK(\"" + url + "\",\"" + url + "\")");
					Hyperlink hyperlink =  workbook.getCreationHelper().createHyperlink(HyperlinkType.URL);
					hyperlink.setAddress(url);
					hyperCell.setCellValue(url);
					hyperCell.setHyperlink(hyperlink);
					hyperCell.setCellValue(url);					
				} else {
					Object dataValue = dataRow.get(j).getValue();
					if(ObjectUtil.isNotEmpty(dataValue)){
						newRow.createCell(j).setCellValue(dataValue.toString());
					}else{
						newRow.createCell(j).setCellValue("");
					}
					
				}
			}
		}
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		workbook.write(os);
		return os;
	}

url超过255个字符会报不能设置为url格式  

参考博客 

Apache POI(6):超链接(HyperLink)_个人知识管理布道师的博客-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值