动态导出excel

hhw
杜函宇 1 month ago
parent 5410b8f729
commit 061445db0f

@ -3,17 +3,27 @@ package com.ruoyi.gysl.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ChangeExcelUtil;
import com.ruoyi.gysl.entity.BasicInformation; import com.ruoyi.gysl.entity.BasicInformation;
import com.ruoyi.gysl.entity.response.ProjectBuildingExcel;
import com.ruoyi.gysl.entity.response.ProjectExcelInfo;
import com.ruoyi.gysl.service.BasicInformationService; import com.ruoyi.gysl.service.BasicInformationService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.*;
/** /**
* (BasicInformation) * (BasicInformation)
@ -92,5 +102,167 @@ public class BasicInformationController extends BaseController {
public AjaxResult delete(@RequestParam("idList") List<Long> idList) { public AjaxResult delete(@RequestParam("idList") List<Long> idList) {
return success(this.basicInformationService.removeByIds(idList)); return success(this.basicInformationService.removeByIds(idList));
} }
/**
*
*/
@ApiOperation("动态导出基本信息")
@PostMapping("/exportInfo")
public void exportInfo(HttpServletResponse response, @RequestParam List<Long> idList) throws NoSuchFieldException, IOException, IllegalAccessException {
//获取对应的基本信息的列表
List<ProjectExcelInfo> info = basicInformationService.selectList(idList);
System.out.println(info);
//excel 第一层单元格
List<String> yc = new ArrayList<>();
//excel 第二层单元格
List<String> ec = new ArrayList<>();
//excel 第三层单元格
List<String> sc = new ArrayList<>();
//项目建筑信息数据所有字段数量
int sczdData = 0;
//一共有几组厂房,不包括重复
int cfsl = 0;
//动态存储每个分组对应的字段数量
Map<String, Integer> zdsl = new LinkedHashMap<>();
Class<?> clazz = ProjectExcelInfo.class;
Class<?> classBuilding = ProjectBuildingExcel.class;
// 遍历除了建筑获取其他字段注解
for (int i = 0; i < info.size(); i++) {
if (i == 0) {
for (Field field : clazz.getDeclaredFields()) {
Excel excelColumn = field.getAnnotation(Excel.class);
if (excelColumn != null) {
if (yc.contains(excelColumn.group())) {
yc.add("");
} else {
yc.add(excelColumn.group());
}
if ("id".equals(excelColumn.name())) {
sc.add("");
} else {
sc.add(excelColumn.name());
}
if (!Objects.equals(excelColumn.group(), "序号")) {
zdsl.merge(excelColumn.group(), 1, Integer::sum);
}
ec.add("");
}
}
}
for (ProjectBuildingExcel xmjzxx : info.get(i).getXmjzxx()) {
switch (xmjzxx.getFloor()) {
case 1:
if(!ec.contains("一层厂房")){
ec.add("一层厂房");
cfsl+=1;
}
break;
case 2:
if(!ec.contains("双层厂房")){
ec.add("双层厂房");
cfsl+=1;
}
break;
case 3:
if(!ec.contains("三层厂房")){
ec.add("三层厂房");
cfsl+=1;
}
break;
case 4:
if(!ec.contains("四层厂房")){
ec.add("四层厂房");
cfsl+=1;
}
break;
case 5:
if(!ec.contains("五层厂房")){
ec.add("五层厂房");
cfsl+=1;
}
break;
default:
if(!ec.contains("六层及以上厂房")){
ec.add("六层及以上厂房");
cfsl+=1;
}
}
for (Field field : classBuilding.getDeclaredFields()) {
Excel excelColumn = field.getAnnotation(Excel.class);
if (excelColumn != null) {
if (!yc.contains("项目建筑信息")) {
yc.add("项目建筑信息");
} else {
yc.add("");
}
ec.add("");
sc.add(excelColumn.name());
sczdData+=1;
}
}
ec.remove(ec.size()-1);
}
}
//=======================sheet处理
// 第一步创建一个Workbook对应一个Excel文件
XSSFWorkbook wb = new XSSFWorkbook();
// 第二步在Workbook中添加一个sheet,对应Excel文件中的sheet
XSSFSheet sheet = wb.createSheet("sheet");
// 第三步,设置样式以及字体样式
XSSFCellStyle titleStyle = ChangeExcelUtil.createTitleCellStyle(wb);
XSSFCellStyle headerStyle = ChangeExcelUtil.createHeadCellStyle(wb);
XSSFCellStyle contentStyle = ChangeExcelUtil.createContentCellStyle(wb);
// 行号
int rowNum = 0;
// 创建第一页的第一行索引从0开始
XSSFRow row0 = sheet.createRow(rowNum++);
row0.setHeight((short) 600);// 设置行高
sheet.setColumnWidth(1, 6000);
sheet.setColumnWidth(2, 6000);
//第二行
XSSFRow row1 = sheet.createRow(rowNum++);
row1.setHeight((short) 500);
//第三行
XSSFRow row2 = sheet.createRow(rowNum++);
row2.setHeight((short) 1000);
//合并单元格:excel,除了项目建筑信息的其余字段数量,项目建筑信息数据所有字段数量,一共有几组厂房,不包括重复
ChangeExcelUtil.extracted(sheet, zdsl,sczdData,cfsl);
// 创建第一行单元格
for (int i = 0; i < yc.size(); i++) {
XSSFCell c00 = row0.createCell(i);
c00.setCellValue(yc.get(i));
c00.setCellStyle(headerStyle);
}
// 创建第二行单元格
for (int i = 0; i < ec.size(); i++) {
XSSFCell tempCell = row1.createCell(i);
tempCell.setCellValue(ec.get(i));
tempCell.setCellStyle(headerStyle);
}
// 创建第三行单元格
for (int i = 0; i < sc.size(); i++) {
XSSFCell tempCell = row2.createCell(i);
tempCell.setCellValue(sc.get(i));
tempCell.setCellStyle(headerStyle);
}
//填充数据
basicInformationService.approvalMethodfillInData(sheet, contentStyle, rowNum, info,sc,ec,sczdData/cfsl);
// 1. 设置响应头(必须)
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("UTF-8");
// 设置文件名(解决中文乱码)
String fileName = URLEncoder.encode("项目信息.xlsx", "UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
try (ServletOutputStream out = response.getOutputStream()) {
// 3. 将 Workbook 写入响应流
wb.write(out);
out.flush();
} finally {
wb.close();
}
}
} }

@ -116,16 +116,20 @@ public class BasicInformation extends Model<BasicInformation> {
//施工许可证发放时间 //施工许可证发放时间
@ApiModelProperty("施工许可证发放时间") @ApiModelProperty("施工许可证发放时间")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") // @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") // @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date issuingTime; private String issuingTime;
//竣工验收时间 //竣工验收时间
@ApiModelProperty("竣工验收时间") @ApiModelProperty("竣工验收时间")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") // @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") // @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime acceptanceTime; private String acceptanceTime;
//建设进度
@ApiModelProperty("建设进度")
private String jsjd;
//项目法人单位简介 //项目法人单位简介
@NotBlank @NotBlank

@ -0,0 +1,36 @@
package com.ruoyi.gysl.entity.response;
import com.ruoyi.common.annotation.Excel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.math.BigDecimal;
/**
*
* @author du
* @since 2025/3/17 9:38
*/
@Data
public class ProjectBuildingExcel {
@Excel(name = "层数",group = "项目建筑信息")
@ApiModelProperty(value = "层数")
private Integer floor;
@Excel(name = "总建筑高度",group = "项目建筑信息")
@ApiModelProperty(value = "总建筑高度")
private BigDecimal totalBuildingHeight;
@Excel(name = "首层高度",group = "项目建筑信息")
@ApiModelProperty(value = "首层高度")
private BigDecimal scgd;
@Excel(name = "2至4层高",group = "项目建筑信息")
@ApiModelProperty(value = "2至4层高")
private BigDecimal twoAndFourCg;
@Excel(name = "4层以上层高",group = "项目建筑信息")
@ApiModelProperty(value = "4层以上层高")
private BigDecimal fourYscg;
}

@ -0,0 +1,54 @@
package com.ruoyi.gysl.entity.response;
import com.ruoyi.common.annotation.Excel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.math.BigDecimal;
import java.util.List;
/**
*
* @author du
* @since 2025/3/14 14:01
*/
@Data
public class ProjectExcelInfo {
//--基本信息
@Excel(name="id",group = "序号")
@ApiModelProperty("序号")
private String id;
@Excel(name = "项目名称",group = "项目基础信息")
@ApiModelProperty("项目名称")
private String name;
@Excel(name = "项目法人单位",group = "项目基础信息")
@ApiModelProperty("项目法人单位")
private String xmfrdwxz;
@Excel(name = "项目法人单位性质",group = "项目基础信息")
@ApiModelProperty("项目法人单位性质")
private Integer nature;
//--规划信息
@Excel(name = "总用地面积",group = "项目规划信息")
@ApiModelProperty(value = "总用地面积")
private BigDecimal zydmj;
@Excel(name = "容积率",group = "项目规划信息")
@ApiModelProperty(value = "容积率")
private BigDecimal rjl;
@Excel(name = "总建筑面积",group = "项目规划信息")
@ApiModelProperty(value = "总建筑面积")
private BigDecimal zjzmj;
@Excel(name = "标准层建筑面积",group = "项目规划信息")
@ApiModelProperty(value = "标准层建筑面积")
private BigDecimal bzcjzmj;
//--建筑信息
private List<ProjectBuildingExcel> xmjzxx;
}

@ -2,6 +2,12 @@ package com.ruoyi.gysl.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.gysl.entity.BasicInformation; import com.ruoyi.gysl.entity.BasicInformation;
import com.ruoyi.gysl.entity.BuildingInformation;
import com.ruoyi.gysl.entity.response.ProjectBuildingExcel;
import com.ruoyi.gysl.entity.response.ProjectExcelInfo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/** /**
* (BasicInformation)访 * (BasicInformation)访
@ -11,5 +17,9 @@ import com.ruoyi.gysl.entity.BasicInformation;
*/ */
public interface BasicInformationMapper extends BaseMapper<BasicInformation> { public interface BasicInformationMapper extends BaseMapper<BasicInformation> {
/**
* id
*/
List<ProjectExcelInfo> selectList(@Param("idList") List<Long> idList);
} }

@ -2,6 +2,13 @@ package com.ruoyi.gysl.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.gysl.entity.BasicInformation; import com.ruoyi.gysl.entity.BasicInformation;
import com.ruoyi.gysl.entity.response.ProjectBuildingExcel;
import com.ruoyi.gysl.entity.response.ProjectExcelInfo;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import java.util.List;
import java.util.Map;
/** /**
* (BasicInformation) * (BasicInformation)
@ -11,5 +18,26 @@ import com.ruoyi.gysl.entity.BasicInformation;
*/ */
public interface BasicInformationService extends IService<BasicInformation> { public interface BasicInformationService extends IService<BasicInformation> {
/**
* id
*/
List<ProjectExcelInfo> selectList(List<Long> idList);
/**
*
*/
void approvalMethodfillInData(XSSFSheet sheet, XSSFCellStyle contentStyle, int rowNum, List<ProjectExcelInfo> info,List<String> sc,List<String> ec,int a) ;
/**
*
*/
String getExcelData(ProjectExcelInfo item,String str);
/**
*
*/
String getBuildingData(ProjectBuildingExcel item,String str);
} }

@ -1,11 +1,22 @@
package com.ruoyi.gysl.service.impl; package com.ruoyi.gysl.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.gysl.mapper.BasicInformationMapper; import com.ruoyi.common.annotation.Excel;
import com.ruoyi.gysl.entity.BasicInformation; import com.ruoyi.gysl.entity.BasicInformation;
import com.ruoyi.gysl.entity.response.ProjectBuildingExcel;
import com.ruoyi.gysl.entity.response.ProjectExcelInfo;
import com.ruoyi.gysl.mapper.BasicInformationMapper;
import com.ruoyi.gysl.service.BasicInformationService; import com.ruoyi.gysl.service.BasicInformationService;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;
/** /**
* (BasicInformation) * (BasicInformation)
* *
@ -15,5 +26,128 @@ import org.springframework.stereotype.Service;
@Service("basicInformationService") @Service("basicInformationService")
public class BasicInformationServiceImpl extends ServiceImpl<BasicInformationMapper, BasicInformation> implements BasicInformationService { public class BasicInformationServiceImpl extends ServiceImpl<BasicInformationMapper, BasicInformation> implements BasicInformationService {
/**
* id
*/
@Override
public List<ProjectExcelInfo> selectList(List<Long> idList) {
return baseMapper.selectList(idList);
}
/**
*
*/
@Override
public void approvalMethodfillInData(XSSFSheet sheet, XSSFCellStyle contentStyle, int rowNum, List<ProjectExcelInfo> info, List<String> sc, List<String> ec, int num) {
Class<?> classBuilding = ProjectBuildingExcel.class;
for (ProjectExcelInfo projectExcelInfo : info) {
XSSFRow tempRow = sheet.createRow(rowNum++);
tempRow.setHeight((short) 1400);
int i1;
for (i1 = 0; i1 < sc.size(); i1++) {
XSSFCell tempCell = tempRow.createCell(i1);
tempCell.setCellStyle(contentStyle);
if (!ec.get(i1).endsWith("厂房")) {
if (Objects.equals(sc.get(i1), "")) {
tempCell.setCellValue(projectExcelInfo.getId());
} else {
tempCell.setCellValue(getExcelData(projectExcelInfo, sc.get(i1)));
}
} }
}
// XSSFCell tempCell = tempRow.createCell(8);
// tempCell.setCellStyle(contentStyle);
// tempCell.setCellValue("12312321");
for (int i2 = 0; i2 < projectExcelInfo.getXmjzxx().size(); i2++) {
switch (projectExcelInfo.getXmjzxx().get(i2).getFloor()) {
case 1:
i1 = ec.indexOf("一层厂房");
break;
case 2:
i1 = ec.indexOf("双层厂房");
break;
case 3:
i1 = ec.indexOf("三层厂房");
break;
case 4:
i1 = ec.indexOf("四层厂房");
break;
case 5:
i1 = ec.indexOf("五层厂房");
break;
default:
i1 = ec.indexOf("六层及以上厂房");
}
for (Field field : classBuilding.getDeclaredFields()) {
XSSFCell tempCell = tempRow.createCell(i1);
tempCell.setCellStyle(contentStyle);
Excel excelColumn = field.getAnnotation(Excel.class);
if (excelColumn != null) {
tempCell.setCellValue(getBuildingData(projectExcelInfo.getXmjzxx().get(i2), sc.get(i1)));
i1++;
}
}
}
}
}
@Override
public String getExcelData(ProjectExcelInfo item, String str) {
String a = null;
switch (str) {
case "":
a = item.getId() == null ? "-" : item.getId();
break;
case "项目名称":
a = item.getName() == null ? "-" : item.getName();
break;
case "项目法人单位":
a = item.getXmfrdwxz() == null ? "-" : item.getXmfrdwxz();
break;
case "项目法人单位性质":
a = String.valueOf(item.getNature() == null ? "-" : item.getNature());
break;
case "总用地面积":
a = String.valueOf(item.getZydmj() == null ? "-" : item.getZydmj());
break;
case "容积率":
a = String.valueOf(item.getRjl() == null ? "-" : item.getRjl());
break;
case "总建筑面积":
a = String.valueOf(item.getZjzmj() == null ? "-" : item.getZjzmj());
break;
case "标准层建筑面积":
a = String.valueOf(item.getBzcjzmj() == null ? "-" : item.getBzcjzmj());
break;
}
return a;
}
/**
*
*/
@Override
public String getBuildingData(ProjectBuildingExcel item, String str) {
String a = null;
switch (str) {
case "层数":
a = item.getFloor() == null ? "-" : String.valueOf(item.getFloor());
break;
case "总建筑高度":
a = item.getTotalBuildingHeight() == null ? "-" : String.valueOf(item.getTotalBuildingHeight());
break;
case "首层高度":
a = String.valueOf(item.getScgd() == null ? "-" : item.getScgd());
break;
case "2至4层高":
a = String.valueOf(item.getTwoAndFourCg() == null ? "-" : item.getTwoAndFourCg());
break;
case "4层以上层高":
a = String.valueOf(item.getFourYscg() == null ? "-" : item.getFourYscg());
break;
}
return a;
}
}

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.gysl.mapper.BasicInformationMapper">
<resultMap id="ProjectExcelInfoResult" type="com.ruoyi.gysl.entity.response.ProjectExcelInfo">
<!-- 基础信息字段 -->
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="xmfrdwxz" column="xmfrdwxz"/>
<result property="nature" column="nature"/>
<!-- 规划信息字段 -->
<result property="zydmj" column="zydmj"/>
<result property="rjl" column="rjl"/>
<result property="zjzmj" column="zjzmj"/>
<result property="bzcjzmj" column="bzcjzmj"/>
<!-- 嵌套集合:建筑信息(一对多) -->
<collection
property="xmjzxx"
ofType="com.ruoyi.gysl.entity.response.ProjectBuildingExcel"
javaType="java.util.List">
<result property="floor" column="floor"/>
<result property="totalBuildingHeight" column="total_building_height"/>
<result property="scgd" column="scgd"/>
<result property="twoAndFourCg" column="two_and_four_cg"/>
<result property="fourYscg" column="four_yscg"/>
</collection>
</resultMap>
<select id="selectList" resultMap="ProjectExcelInfoResult">
SELECT
a.id,
a.name,
a.xmfrdwxz,
a.nature,
b.zydmj,
b.rjl,
b.zjzmj,
b.bzcjzmj,
<!-- 建筑信息字段添加 c_ 前缀 -->
c.floor,
c.total_building_height,
c.scgd,
c.two_and_four_cg,
c.four_yscg
FROM
basic_information a
LEFT JOIN plan_information b ON a.id = b.xm_id
LEFT JOIN building_information c ON a.id = c.xm_id
WHERE
a.id IN
<foreach collection="idList" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
</mapper>

@ -17,7 +17,11 @@
<dependencies> <dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version> <!-- 建议使用最新稳定版本 -->
</dependency>
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId> <artifactId>mybatis-plus-boot-starter</artifactId>

@ -28,6 +28,11 @@ public @interface Excel
*/ */
public String name() default ""; public String name() default "";
/**
* Excel.
*/
public String group() default "";
/** /**
* , : yyyy-MM-dd * , : yyyy-MM-dd
*/ */

@ -0,0 +1,124 @@
package com.ruoyi.common.utils.poi;
import com.ruoyi.common.annotation.Excel;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.lang.reflect.Field;
import java.util.Map;
/**
* Excel
*
* @author dhy
*/
public class ChangeExcelUtil<T> {
public static void extracted(XSSFSheet sheet, Map<String, Integer> s, int i,int jzxx) {
// todo: 合并参数分别为: 起始行,结束行,起始列,结束列
sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 0));
int startCol = 1; // 合并起始列
int endCol = 0;
for (String key : s.keySet()) {
endCol = startCol + s.get(key) - 1;
sheet.addMergedRegion(new CellRangeAddress(0, 1, startCol, endCol));
// 更新下一个开始列(当前结束列 + 1
startCol = endCol + 1;
}
endCol+=1;
sheet.addMergedRegion(new CellRangeAddress(0, 0, endCol, endCol+i-1));
int ehEndCol = 0;
for (int x = 0; x < jzxx; x++) {
ehEndCol = endCol + (i/jzxx) - 1;
sheet.addMergedRegion(new CellRangeAddress(1, 1, endCol, ehEndCol));
endCol = ehEndCol+1;
}
}
/**
*
*
* @param wb
* @return
*/
public static XSSFCellStyle createTitleCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(HorizontalAlignment.CENTER);//水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直对齐
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());//背景颜色
XSSFFont headerFont1 = (XSSFFont) wb.createFont(); // 创建字体样式
headerFont1.setBold(true); //字体加粗
headerFont1.setFontName("黑体"); // 设置字体类型
headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小
cellStyle.setFont(headerFont1); // 为标题样式设置字体样式
return cellStyle;
}
/**
*
*
* @param wb
* @return
*/
public static XSSFCellStyle createHeadCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setWrapText(true);// 设置自动换行
// cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());//背景颜色
cellStyle.setAlignment(HorizontalAlignment.CENTER); //水平居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); //垂直对齐
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// cellStyle.setBottomBorderColor(IndexedColors.BLACK.index);
cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN); //左边框
cellStyle.setBorderRight(BorderStyle.THIN); //右边框
cellStyle.setBorderTop(BorderStyle.THIN); //上边框
XSSFFont headerFont = (XSSFFont) wb.createFont(); // 创建字体样式
headerFont.setBold(true); //字体加粗
headerFont.setFontName("黑体"); // 设置字体类型
headerFont.setFontHeightInPoints((short) 12); // 设置字体大小
cellStyle.setFont(headerFont); // 为标题样式设置字体样式
return cellStyle;
}
/**
*
*
* @param wb
* @return
*/
public static XSSFCellStyle createContentCellStyle(XSSFWorkbook wb) {
XSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);// 水平居中
cellStyle.setWrapText(true);// 设置自动换行
cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
cellStyle.setBorderLeft(BorderStyle.THIN); //左边框
cellStyle.setBorderRight(BorderStyle.THIN); //右边框
cellStyle.setBorderTop(BorderStyle.THIN); //上边框
// 生成12号字体
XSSFFont font = wb.createFont();
font.setColor((short) 8);
font.setFontHeightInPoints((short) 12);
cellStyle.setFont(font);
return cellStyle;
}
}
Loading…
Cancel
Save