poi版本依赖如下
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
话不多说代码如下
HSSFWorkbook wb = new HSSFWorkbook();
/* 建立新的sheet对象*/
HSSFSheet sheet = wb.createSheet("汇总页面");
HSSFRow row = sheet.createRow((short)0);
/* 连接跳转*/
CreationHelper createHelper = wb.getCreationHelper();
HSSFCell likeCell = row.createCell((short)0);
Hyperlink hyperlink = createHelper.createHyperlink(HyperlinkType.DOCUMENT);
// "#"表示本文档 "明细页面"表示sheet页名称 "A10"表示第几列第几行
hyperlink.setAddress("#明细页面!A10");
likeCell.setHyperlink(hyperlink);
// 点击进行跳转
likeCell.setCellValue("dddd");
/* 设置为超链接的样式*/
HSSFCellStyle linkStyle = wb.createCellStyle();
HSSFFont cellFont= wb.createFont();
cellFont.setUnderline((byte) 1);
linkStyle.setFont(cellFont);
likeCell.setCellStyle(linkStyle);
/* 建立第二个sheet对象*/
HSSFSheet sheet2 = wb.createSheet("明细页面"); //建立新的sheet对象
for (int i = 0; i < 30; i++) {
HSSFRow row2 = sheet2.createRow((short)i);
HSSFCell cell2 = row2.createCell((short)0);
cell2.setCellValue("测试第"+(i+1)+"行");
}
/* 输出文件*/
FileOutputStream fileOut = new FileOutputStream("D:\\汇总和明细.xls");
wb.write(fileOut);
fileOut.close();