目录
1 Maven依赖
2 代码实现
3 调试代码
4 调试结果
注:
1 Maven依赖
com.alibaba
easyexcel
2.2.8
cn.hutool
hutool-all
5.5.7
2 代码实现
public static void insertLine(Workbook workbook, Sheet sheet, int beginRowIndex, int endRowIndex, int beginColIndex
, int endColIndex) {
insertLine(workbook, sheet, beginRowIndex, endRowIndex, beginColIndex, endColIndex, POIUtil.getXSSFRGBColor(0, 0, 0)
, LineStyleConstant.INT_SOLID, 0.5d);
}
public static void insertLine(Workbook workbook, Sheet sheet, int beginRowIndex, int endRowIndex, int beginColIndex, int endColIndex
, Color lineColor, int lineStyle, double lineWidth) {
insertShape(workbook, sheet, beginRowIndex, endRowIndex, beginColIndex, endColIndex, lineColor, lineStyle, lineWidth
, ShapeTypes.LINE);
}
public static void insertShape(Workbook workbook, Sheet sheet, int beginRowIndex, int endRowIndex, int beginColIndex, int endColIndex
, Color lineColor, int lineStyle, double lineWidth, int shapeType) {
CreationHelper helper = workbook.getCreationHelper();
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
Drawing drawing = sheet.getDrawingPatriarch();
if (drawing == null) {
drawing = sheet.createDrawingPatriarch();
}
ClientAnchor anchor = helper.createClientAnchor();
// 设置线条的开始位置
anchor.setCol1(beginColIndex);
anchor.setRow1(beginRowIndex);
// 设置线条的结束位置
anchor.setCol2(endColIndex);
anchor.setRow2(endRowIndex);
if (drawing instanceof XSSFDrawing) {
XSSFSimpleShape shape = ((XSSFDrawing) drawing).createSimpleShape((XSSFClientAnchor) anchor);
// 设置形状类型
shape.setShapeType(shapeType);
// 设置线宽
shape.setLineWidth(lineWidth);
// 设置线的风格
shape.setLineStyle(lineStyle - 1);
// 设置线的颜色
XSSFColor xssfColor = (XSSFColor) lineColor;
byte[] rgbBytes = xssfColor.getRGB();
if (rgbBytes == null || rgbBytes.length < 3) {
shape.setLineStyleColor(0, 0, 0);
} else {
shape.setLineStyleColor(rgbBytes[0], rgbBytes[1], rgbBytes[2]);
}
}
}
3 调试代码
@Test
public void testInsertLine(){
try {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
File file = new File("D:/easyexcel/testInsertLine.xlsx");
FileUtil.createNewFile(file);
//插入线条
POIExcelUtil.insertLine(workbook,sheet,1,2,0,4);
workbook.write(new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
4 调试结果
public static void insertLine(Workbook workbook, Sheet sheet, int beginRowIndex, int endRowIndex, int beginColIndex
, int endColIndex) {
insertLine(workbook, sheet, beginRowIndex, endRowIndex, beginColIndex, endColIndex, POIUtil.getXSSFRGBColor(0, 0, 0)
, LineStyleConstant.INT_SOLID, 0.5d);
}
public static void insertLine(Workbook workbook, Sheet sheet, int beginRowIndex, int endRowIndex, int beginColIndex, int endColIndex
, Color lineColor, int lineStyle, double lineWidth) {
insertShape(workbook, sheet, beginRowIndex, endRowIndex, beginColIndex, endColIndex, lineColor, lineStyle, lineWidth
, ShapeTypes.LINE);
}
public static void insertShape(Workbook workbook, Sheet sheet, int beginRowIndex, int endRowIndex, int beginColIndex, int endColIndex
, Color lineColor, int lineStyle, double lineWidth, int shapeType) {
CreationHelper helper = workbook.getCreationHelper();
//画图的顶级管理器,一个sheet只能获取一个(一定要注意这点)
Drawing drawing = sheet.getDrawingPatriarch();
if (drawing == null) {
drawing = sheet.createDrawingPatriarch();
}
ClientAnchor anchor = helper.createClientAnchor();
// 设置线条的开始位置
anchor.setCol1(beginColIndex);
anchor.setRow1(beginRowIndex);
// 设置线条的结束位置
anchor.setCol2(endColIndex);
anchor.setRow2(endRowIndex);
if (drawing instanceof XSSFDrawing) {
XSSFSimpleShape shape = ((XSSFDrawing) drawing).createSimpleShape((XSSFClientAnchor) anchor);
// 设置形状类型
shape.setShapeType(shapeType);
// 设置线宽
shape.setLineWidth(lineWidth);
// 设置线的风格
shape.setLineStyle(lineStyle - 1);
// 设置线的颜色
XSSFColor xssfColor = (XSSFColor) lineColor;
byte[] rgbBytes = xssfColor.getRGB();
if (rgbBytes == null || rgbBytes.length < 3) {
shape.setLineStyleColor(0, 0, 0);
} else {
shape.setLineStyleColor(rgbBytes[0], rgbBytes[1], rgbBytes[2]);
}
}
}
3 调试代码
@Test
public void testInsertLine(){
try {
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
File file = new File("D:/easyexcel/testInsertLine.xlsx");
FileUtil.createNewFile(file);
//插入线条
POIExcelUtil.insertLine(workbook,sheet,1,2,0,4);
workbook.write(new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
4 调试结果
注:
(1)只支持07版的Excel文档。
(2)源码请查看Gitee。
xudongbase: 主要是项目中可以用到的共通方法https://gitee.com/xudong_master/xudongbase



