support Hyperlink

This commit is contained in:
Looly 2021-09-17 21:05:39 +08:00
parent 545e2d4774
commit 9db1d175e0
8 changed files with 136 additions and 22 deletions

View File

@ -12,6 +12,7 @@
* 【core 】 BitStatusUtil添加来源声明issue#1824@Github
* 【core 】 UrlQuery.build增加重载支持可选是否转义issue#I4AIX1@Gitee
* 【core 】 ListUtil增加swapTo和swapElement方法pr#416@Gitee
* 【poi 】 ExcelWriter支持Hyperlinkissue#I49QAL@Gitee
*
### 🐞Bug修复
* 【core 】 修复FuncKey函数无效问题

View File

@ -5,8 +5,10 @@ import cn.hutool.core.lang.Assert;
import cn.hutool.poi.excel.cell.CellLocation;
import cn.hutool.poi.excel.cell.CellUtil;
import cn.hutool.poi.excel.style.StyleUtil;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
@ -390,6 +392,32 @@ public class ExcelBase<T extends ExcelBase<T>> implements Closeable {
return columnStyle;
}
/**
* 创建 {@link Hyperlink}默认内容标签为链接地址本身
* @param type 链接类型
* @param address 链接地址
* @return 链接
* @since 5.7.13
*/
public Hyperlink createHyperlink(HyperlinkType type, String address){
return createHyperlink(type, address, address);
}
/**
* 创建 {@link Hyperlink}默认内容
* @param type 链接类型
* @param address 链接地址
* @param label 标签即单元格中显示的内容
* @return 链接
* @since 5.7.13
*/
public Hyperlink createHyperlink(HyperlinkType type, String address, String label){
final Hyperlink hyperlink = this.workbook.getCreationHelper().createHyperlink(type);
hyperlink.setAddress(address);
hyperlink.setLabel(label);
return hyperlink;
}
/**
* 获取总行数计算方法为
*

View File

@ -16,6 +16,7 @@ import cn.hutool.core.util.URLUtil;
import cn.hutool.poi.excel.cell.CellLocation;
import cn.hutool.poi.excel.cell.CellUtil;
import cn.hutool.poi.excel.style.Align;
import org.apache.poi.common.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataValidation;
@ -947,6 +948,9 @@ public class ExcelWriter extends ExcelBase<ExcelWriter> {
} else {
rowMap = (Map) rowBean;
}
} else if (rowBean instanceof Hyperlink) {
// Hyperlink当成一个值
return writeRow(CollUtil.newArrayList(rowBean), isWriteKeyAsHead);
} else if (BeanUtil.isBean(rowBean.getClass())) {
if (MapUtil.isEmpty(this.headerAlias)) {
rowMap = BeanUtil.beanToMap(rowBean, new LinkedHashMap<>(), false, false);

View File

@ -1,6 +1,7 @@
package cn.hutool.poi.excel;
import cn.hutool.poi.excel.style.StyleUtil;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
@ -31,6 +32,8 @@ public class StyleSet implements Serializable{
protected CellStyle cellStyleForNumber;
/** 默认日期样式 */
protected CellStyle cellStyleForDate;
/** 默认链接样式 */
protected CellStyle cellStyleForHyperlink;
/**
* 构造
@ -42,15 +45,23 @@ public class StyleSet implements Serializable{
this.headCellStyle = StyleUtil.createHeadCellStyle(workbook);
this.cellStyle = StyleUtil.createDefaultCellStyle(workbook);
// 默认数字格式
cellStyleForNumber = StyleUtil.cloneCellStyle(workbook, this.cellStyle);
// 2表示0.00
cellStyleForNumber.setDataFormat((short) 2);
// 默认日期格式
this.cellStyleForDate = StyleUtil.cloneCellStyle(workbook, this.cellStyle);
// 22表示m/d/yy h:mm
this.cellStyleForDate.setDataFormat((short) 22);
// 默认数字格式
cellStyleForNumber = StyleUtil.cloneCellStyle(workbook, this.cellStyle);
// 2表示0.00
cellStyleForNumber.setDataFormat((short) 2);
// 默认链接样式
this.cellStyleForHyperlink = StyleUtil.cloneCellStyle(workbook, this.cellStyle);
final Font font = this.workbook.createFont();
font.setUnderline((byte) 1);
font.setColor(HSSFColor.HSSFColorPredefined.BLUE.getIndex());
this.cellStyleForHyperlink.setFont(font);
}
/**
@ -59,7 +70,7 @@ public class StyleSet implements Serializable{
* @return 头部样式
*/
public CellStyle getHeadCellStyle() {
return headCellStyle;
return this.headCellStyle;
}
/**
@ -68,25 +79,35 @@ public class StyleSet implements Serializable{
* @return 常规单元格样式
*/
public CellStyle getCellStyle() {
return cellStyle;
return this.cellStyle;
}
/**
* 获取数字带小数点单元格样式获取后可以定义整体头部样式
* 获取数字带小数点单元格样式获取后可以定义整体数字样式
*
* @return 数字带小数点单元格样式
*/
public CellStyle getCellStyleForNumber() {
return cellStyleForNumber;
return this.cellStyleForNumber;
}
/**
* 获取日期单元格样式获取后可以定义整体头部样式
* 获取日期单元格样式获取后可以定义整体日期样式
*
* @return 日期单元格样式
*/
public CellStyle getCellStyleForDate() {
return cellStyleForDate;
return this.cellStyleForDate;
}
/**
* 获取链接单元格样式获取后可以定义整体链接样式
*
* @return 链接单元格样式
* @since 5.7.13
*/
public CellStyle getCellStyleForHyperlink() {
return this.cellStyleForHyperlink;
}
/**
@ -102,6 +123,7 @@ public class StyleSet implements Serializable{
StyleUtil.setBorder(this.cellStyle, borderSize, colorIndex);
StyleUtil.setBorder(this.cellStyleForNumber, borderSize, colorIndex);
StyleUtil.setBorder(this.cellStyleForDate, borderSize, colorIndex);
StyleUtil.setBorder(this.cellStyleForHyperlink, borderSize, colorIndex);
return this;
}
@ -118,6 +140,7 @@ public class StyleSet implements Serializable{
StyleUtil.setAlign(this.cellStyle, halign, valign);
StyleUtil.setAlign(this.cellStyleForNumber, halign, valign);
StyleUtil.setAlign(this.cellStyleForDate, halign, valign);
StyleUtil.setAlign(this.cellStyleForHyperlink, halign, valign);
return this;
}
@ -136,6 +159,7 @@ public class StyleSet implements Serializable{
StyleUtil.setColor(this.cellStyle, backgroundColor, FillPatternType.SOLID_FOREGROUND);
StyleUtil.setColor(this.cellStyleForNumber, backgroundColor, FillPatternType.SOLID_FOREGROUND);
StyleUtil.setColor(this.cellStyleForDate, backgroundColor, FillPatternType.SOLID_FOREGROUND);
StyleUtil.setColor(this.cellStyleForHyperlink, backgroundColor, FillPatternType.SOLID_FOREGROUND);
return this;
}
@ -168,6 +192,7 @@ public class StyleSet implements Serializable{
this.cellStyle.setFont(font);
this.cellStyleForNumber.setFont(font);
this.cellStyleForDate.setFont(font);
this.cellStyleForHyperlink.setFont(font);
return this;
}
@ -181,6 +206,8 @@ public class StyleSet implements Serializable{
this.cellStyle.setWrapText(true);
this.cellStyleForNumber.setWrapText(true);
this.cellStyleForDate.setWrapText(true);
this.cellStyleForHyperlink.setWrapText(true);
return this;
}
}

View File

@ -15,6 +15,7 @@ import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
@ -171,6 +172,11 @@ public class CellUtil {
if ((value instanceof Double || value instanceof Float || value instanceof BigDecimal) && null != styleSet && null != styleSet.getCellStyleForNumber()) {
cell.setCellStyle(styleSet.getCellStyleForNumber());
}
} else if(value instanceof Hyperlink){
// 自定义超链接样式
if (null != styleSet && null != styleSet.getCellStyleForHyperlink()) {
cell.setCellStyle(styleSet.getCellStyleForHyperlink());
}
}
setCellValue(cell, value);

View File

@ -1,6 +1,7 @@
package cn.hutool.poi.excel.cell.setters;
import cn.hutool.poi.excel.cell.CellSetter;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.RichTextString;
import java.time.temporal.TemporalAccessor;
@ -38,6 +39,8 @@ public class CellSetterFactory {
return new RichTextCellSetter((RichTextString) value);
} else if (value instanceof Number) {
return new NumberCellSetter((Number) value);
}else if (value instanceof Hyperlink) {
return new HyperlinkCellSetter((Hyperlink) value);
} else {
return new CharSequenceCellSetter(value.toString());
}

View File

@ -0,0 +1,32 @@
package cn.hutool.poi.excel.cell.setters;
import cn.hutool.core.lang.Console;
import cn.hutool.poi.excel.cell.CellSetter;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Hyperlink;
/**
* {@link Hyperlink} 值单元格设置器
*
* @author looly
* @since 5.7.13
*/
public class HyperlinkCellSetter implements CellSetter {
private final Hyperlink value;
/**
* 构造
*
* @param value
*/
HyperlinkCellSetter(Hyperlink value) {
this.value = value;
}
@Override
public void setValue(Cell cell) {
cell.setHyperlink(this.value);
cell.setCellValue(this.value.getLabel());
}
}

View File

@ -10,12 +10,14 @@ import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.cell.setters.EscapeStrCellSetter;
import cn.hutool.poi.excel.style.StyleUtil;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddressList;
@ -770,4 +772,15 @@ public class ExcelWriteTest {
writer.write(ListUtil.of(1427545395336093698L));
writer.close();
}
@Test
@Ignore
public void writeHyperlinkTest(){
final ExcelWriter writer = ExcelUtil.getWriter("d:/test/hyperlink.xlsx");
final Hyperlink hyperlink = writer.createHyperlink(HyperlinkType.URL, "https://hutool.cn");
writer.write(ListUtil.of(hyperlink));
writer.close();
}
}