直接上代码,里面有注释:
package com.rock.agricultural.controller;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author FastKing
* @version 1.0
* @date 2019/1/4 16:01
**/
public class POITest {
public static void main(String[] args) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("TestSheet");
XSSFSheet sheet2 = workbook.createSheet("TestSheet2");
//合并单元格,合并参数依次为--->行开始位置,行结束位置,列开始位置,列结束位置
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));
XSSFRow row = sheet.createRow(1);
XSSFCell cell = row.createCell(0);
//设置单元格函数,此处功能为添加超链接与其他sheet关联,如果需要其他功能请参考excel函数自行修改
cell.setCellFormula("HYPERLINK(\"#TestSheet2!A1\",\"TestSheet2\")");
workbook.write(new FileOutputStream("F:\\Test.xlsx"));
}
}