Commit 78851199 authored by yanzg's avatar yanzg

导出Excel边框

parent 67c38588
...@@ -11,6 +11,7 @@ import com.yanzuoguang.util.table.TableHeadItem; ...@@ -11,6 +11,7 @@ import com.yanzuoguang.util.table.TableHeadItem;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.io.File; import java.io.File;
...@@ -402,9 +403,9 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -402,9 +403,9 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
} }
CellRangeAddress region = new CellRangeAddress(rowStart, rowEnd, columnPos, columnPos); CellRangeAddress region = new CellRangeAddress(rowStart, rowEnd, columnPos, columnPos);
setStyle(region);
sheet.addMergedRegion(region); sheet.addMergedRegion(region);
// 设置合并后的单元格样式
sheet.getRow(rowStart).getCell(columnPos).setCellStyle(style);
} }
/** /**
...@@ -520,22 +521,37 @@ public class ExcelConsole<T extends Object> implements DbRow<T> { ...@@ -520,22 +521,37 @@ public class ExcelConsole<T extends Object> implements DbRow<T> {
font.setFontName("宋体"); font.setFontName("宋体");
font.setFontHeightInPoints((short) 10); font.setFontHeightInPoints((short) 10);
cellStyle.setFont(font); cellStyle.setFont(font);
// 左右居中 // 左右居中
cellStyle.setAlignment(CellStyle.ALIGN_CENTER); cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
// 上下居中 // 上下居中
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
// 设置自动换行
cellStyle.setWrapText(true); cellStyle.setWrapText(true);
// 设置单元格的边框颜色.
cellStyle.setTopBorderColor(HSSFColor.BLACK.index);
cellStyle.setLeftBorderColor(HSSFColor.BLACK.index); cellStyle.setLeftBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderLeft((short) 1);
cellStyle.setRightBorderColor(HSSFColor.BLACK.index); cellStyle.setRightBorderColor(HSSFColor.BLACK.index);
cellStyle.setBorderRight((short) 1); cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
// 设置单元格的边框为粗体 // 设置单元格的边框为粗体
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderBottom(CellStyle.BORDER_THIN); cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
// 设置单元格的边框颜色.
cellStyle.setBottomBorderColor(HSSFColor.BLACK.index);
// 设置单元格的背景颜色. // 设置单元格的背景颜色.
cellStyle.setFillForegroundColor(HSSFColor.WHITE.index); cellStyle.setFillForegroundColor(HSSFColor.WHITE.index);
return cellStyle; return cellStyle;
} }
private void setStyle(CellRangeAddress region) {
// 使用RegionUtil类为合并后的单元格添加边框
RegionUtil.setBorderTop(style.getBorderTop(), region, sheet, this.workbook);
RegionUtil.setBorderLeft(style.getBorderLeft(), region, sheet, this.workbook);
RegionUtil.setBorderRight(style.getBorderRight(), region, sheet, this.workbook);
RegionUtil.setBorderBottom(style.getBorderBottom(), region, sheet, this.workbook);
RegionUtil.setTopBorderColor(style.getTopBorderColor(), region, sheet, this.workbook);
RegionUtil.setLeftBorderColor(style.getLeftBorderColor(), region, sheet, this.workbook);
RegionUtil.setRightBorderColor(style.getRightBorderColor(), region, sheet, this.workbook);
RegionUtil.setBottomBorderColor(style.getBottomBorderColor(), region, sheet, this.workbook);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment