@@ -254,6 +254,18 @@ | |||
<artifactId>velocity</artifactId> | |||
<version>${velocity.version}</version> | |||
</dependency> | |||
<!-- Excel处理 --> | |||
<dependency> | |||
<groupId>org.apache.poi</groupId> | |||
<artifactId>poi</artifactId> | |||
<version>3.17</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>org.apache.poi</groupId> | |||
<artifactId>poi-ooxml</artifactId> | |||
<version>3.17</version> | |||
</dependency> | |||
</dependencies> | |||
<profiles> |
@@ -0,0 +1,67 @@ | |||
package com.taauav.admin.controller; | |||
import com.taauav.admin.entity.TauvWaterData; | |||
import com.taauav.admin.query.TauvWaterDataQuery; | |||
import com.taauav.admin.service.ITauvWaterDataService; | |||
import com.taauav.common.bean.Response; | |||
import com.taauav.common.constant.PermissionConstants; | |||
import org.apache.shiro.authz.annotation.RequiresPermissions; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.web.bind.annotation.*; | |||
import org.springframework.web.multipart.MultipartFile; | |||
import javax.annotation.Resource; | |||
/** | |||
* 水质数据 控制器 | |||
* | |||
* @author daixiantong | |||
* @date 2020-05-14 | |||
*/ | |||
@RestController | |||
@RequestMapping("/admin/waterData") | |||
public class TauvWaterDataController extends BaseController { | |||
private static final String controllerName = "/admin/waterData"; | |||
@Autowired | |||
private ITauvWaterDataService waterDataService; | |||
@Resource | |||
private Response response; | |||
/** | |||
* 水质数据列表 | |||
* @param waterDataQuery | |||
* @return | |||
*/ | |||
@GetMapping("/index") | |||
@RequiresPermissions(controllerName + ":" + PermissionConstants.LIST_PERMISSION) | |||
public Response dataList(TauvWaterDataQuery waterDataQuery) { | |||
return waterDataService.selectPageList(waterDataQuery); | |||
} | |||
/** | |||
* 编辑 | |||
* @param waterData | |||
* @return | |||
*/ | |||
@PostMapping("/edit") | |||
@RequiresPermissions(controllerName + ":" + PermissionConstants.EDIT_PERMISSION) | |||
public Response editData(TauvWaterData waterData) { | |||
Boolean res = waterDataService.editData(waterData); | |||
if (!res) { | |||
return response.failure("操作失敗"); | |||
} | |||
return response.success("操作成功"); | |||
} | |||
/** | |||
* 导入数据 | |||
* @param file | |||
* @param id | |||
* @return | |||
*/ | |||
@PostMapping("/import") | |||
@RequiresPermissions(controllerName + ":" + PermissionConstants.IMPORT_PERMISSION) | |||
public Response importData(@RequestParam("file") MultipartFile file, @RequestParam("id") Integer id) { | |||
return waterDataService.importData(file, id); | |||
} | |||
} |
@@ -0,0 +1,60 @@ | |||
package com.taauav.admin.entity; | |||
import com.baomidou.mybatisplus.annotation.TableName; | |||
import com.taauav.common.domain.Entity; | |||
import io.swagger.annotations.ApiModelProperty; | |||
import lombok.Data; | |||
import lombok.EqualsAndHashCode; | |||
import lombok.experimental.Accessors; | |||
/** | |||
* 水质数据实体类 | |||
* | |||
* @author daixiantong | |||
* @date 2020-05-13 | |||
*/ | |||
@Data | |||
@EqualsAndHashCode(callSuper = false) | |||
@Accessors(chain = true) | |||
@TableName("tauv_water_data") | |||
public class TauvWaterData extends Entity { | |||
@ApiModelProperty(value = "巡检河道id") | |||
private Integer inspectDriverId; | |||
@ApiModelProperty(value = "化学需氧量") | |||
private String waterCod; | |||
@ApiModelProperty(value = "氨氮") | |||
private String waterNh3n; | |||
@ApiModelProperty(value = "总磷") | |||
private String waterTp; | |||
@ApiModelProperty(value = "总氮") | |||
private String waterTn; | |||
@ApiModelProperty(value = "溶解氧") | |||
private String waterDo; | |||
@ApiModelProperty(value = "浊度") | |||
private String waterTub; | |||
@ApiModelProperty(value = "化学需氧量多光谱图") | |||
private String waterCodPic; | |||
@ApiModelProperty(value = "氨氮多光谱图") | |||
private String waterNh3nPic; | |||
@ApiModelProperty(value = "总磷多光谱图") | |||
private String waterTpPic; | |||
@ApiModelProperty(value = "总氮多光谱图") | |||
private String waterTnPic; | |||
@ApiModelProperty(value = "溶解氧多光谱图") | |||
private String waterDoPic; | |||
@ApiModelProperty(value = "浊度多光谱图") | |||
private String waterTubPic; | |||
} |
@@ -8,7 +8,6 @@ import lombok.EqualsAndHashCode; | |||
import lombok.experimental.Accessors; | |||
import javax.validation.constraints.NotEmpty; | |||
import javax.validation.constraints.NotNull; | |||
import javax.validation.constraints.Size; | |||
/** |
@@ -0,0 +1,24 @@ | |||
package com.taauav.admin.mapper; | |||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |||
import com.baomidou.mybatisplus.core.metadata.IPage; | |||
import com.taauav.admin.entity.TauvWaterData; | |||
import com.taauav.admin.query.TauvWaterDataQuery; | |||
import com.taauav.admin.vo.TauvWaterDataVo; | |||
import org.apache.ibatis.annotations.Param; | |||
/** | |||
* 水质数据 | |||
* @author daixiantong | |||
* @date 2020-05-13 | |||
*/ | |||
public interface TauvWaterDataMapper extends BaseMapper<TauvWaterData> { | |||
/** | |||
* 自定义分页数据 | |||
* @param page | |||
* @param waterDataQuery | |||
* @return | |||
*/ | |||
IPage<TauvWaterDataVo> selectPageList(IPage page, @Param("param") TauvWaterDataQuery waterDataQuery); | |||
} |
@@ -0,0 +1,25 @@ | |||
<?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.taauav.admin.mapper.TauvWaterDataMapper"> | |||
<!--报告数据查询--> | |||
<select id="selectPageList" resultType="com.taauav.admin.vo.TauvWaterDataVo"> | |||
select d.*, v.inspect_no as `inspectNo`, v.driver_area as `driverArea`, v.driver_name as `driverName`, | |||
v.execution_time as `executionTime` | |||
from tauv_water_data as d left join tauv_inspect_driver as v on d.inspect_driver_id = v.id | |||
where d.mark = 1 and v.mark = 1 | |||
<if test="param != null and param.inspectNo != null and param.inspectNo != ''"> | |||
and v.inspect_no like concat('%', #{param.inspectNo}, '%') | |||
</if> | |||
<if test="param != null and param.driverArea != null and param.driverArea > 0"> | |||
and v.driver_area = #{param.driverArea} | |||
</if> | |||
<if test="param != null and param.driverName != null and param.driverName != ''"> | |||
and v.driver_name like concat('%', #{param.driverName}, '%') | |||
</if> | |||
<if test="param.startTime != null and param.endTime != null"> | |||
and (v.execution_time between #{param.startTime} and #{param.endTime}) | |||
</if> | |||
order by v.execution_time desc | |||
</select> | |||
</mapper> |
@@ -0,0 +1,48 @@ | |||
package com.taauav.admin.query; | |||
import com.fasterxml.jackson.annotation.JsonFormat; | |||
import com.taauav.common.core.mps.BaseQuery; | |||
import lombok.Data; | |||
import org.springframework.format.annotation.DateTimeFormat; | |||
import java.math.BigInteger; | |||
import java.util.Date; | |||
/** | |||
* 水质数据请求实体类 | |||
* | |||
* @author daixiantong | |||
* @date 2020-05-14 | |||
*/ | |||
@Data | |||
public class TauvWaterDataQuery extends BaseQuery { | |||
/** | |||
* 任务单号 | |||
*/ | |||
private String inspectNo; | |||
/** | |||
* 区属 | |||
*/ | |||
private BigInteger driverArea; | |||
/** | |||
* 河湖名称 | |||
*/ | |||
private String driverName; | |||
/** | |||
* 巡检开始时间 | |||
*/ | |||
@DateTimeFormat(pattern = "yyyy-MM-dd") | |||
@JsonFormat(pattern = "yyyy-MM-dd") | |||
private Date startTime; | |||
/** | |||
* 巡检结束时间 | |||
*/ | |||
@DateTimeFormat(pattern = "yyyy-MM-dd") | |||
@JsonFormat(pattern = "yyyy-MM-dd") | |||
private Date endTime; | |||
} |
@@ -0,0 +1,31 @@ | |||
package com.taauav.admin.service; | |||
import com.taauav.admin.entity.TauvWaterData; | |||
import com.taauav.admin.query.TauvWaterDataQuery; | |||
import com.taauav.common.bean.Response; | |||
import com.taauav.common.service.IBaseService; | |||
import org.springframework.web.multipart.MultipartFile; | |||
/** | |||
* 水质数据 服务类 | |||
* | |||
* @author daixiantong | |||
* @date 2020-05-14 | |||
*/ | |||
public interface ITauvWaterDataService extends IBaseService<TauvWaterData> { | |||
/** | |||
* 自定义初始化 | |||
* @param waterDataQuery | |||
* @return | |||
*/ | |||
Response selectPageList(TauvWaterDataQuery waterDataQuery); | |||
/** | |||
* 导入水质数据 | |||
* @param file | |||
* @param id | |||
* @return | |||
*/ | |||
Response importData(MultipartFile file, Integer id); | |||
} |
@@ -5,8 +5,6 @@ import com.taauav.admin.query.TauvWaterStandardQuery; | |||
import com.taauav.common.bean.Response; | |||
import com.taauav.common.service.IBaseService; | |||
import java.util.Map; | |||
/** | |||
* 地表水环境质量标准 服务类 | |||
* |
@@ -3,6 +3,7 @@ package com.taauav.admin.service.impl; | |||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |||
import com.baomidou.mybatisplus.core.metadata.IPage; | |||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |||
import com.taauav.admin.mapper.TauvWaterDataMapper; | |||
import com.taauav.common.bean.Response; | |||
import com.taauav.admin.dto.TauvReportDTO; | |||
import com.taauav.admin.dto.TauvReportInputDTO; | |||
@@ -65,6 +66,8 @@ public class TauvReportServiceImpl extends BaseServiceImpl<TauvReportMapper, Tau | |||
private ITauvReportCheckService iTauvReportCheckService; | |||
@Autowired | |||
private ITauvDriverService tauvDriverService; | |||
@Autowired | |||
private TauvWaterDataMapper waterDataMapper; | |||
/** | |||
* 添加报告 | |||
@@ -686,6 +689,16 @@ public class TauvReportServiceImpl extends BaseServiceImpl<TauvReportMapper, Tau | |||
if (status == 3) { | |||
// 审核通过 | |||
inspectDriver.setStatus(4); | |||
// 水质数据入库 | |||
QueryWrapper wrapper = new QueryWrapper(); | |||
wrapper.eq("inspect_driver_id", inspectDriverId); | |||
wrapper.eq("mark", 1); | |||
wrapper.last("limit 1"); | |||
TauvWaterData waterData = waterDataMapper.selectOne(wrapper); | |||
if (waterData == null) { | |||
waterData.setInspectDriverId(inspectDriverId); | |||
waterDataMapper.insert(waterData); | |||
} | |||
} else if (status == 4) { | |||
// 审核驳回 | |||
inspectDriver.setStatus(6); |
@@ -0,0 +1,62 @@ | |||
package com.taauav.admin.service.impl; | |||
import com.baomidou.mybatisplus.core.metadata.IPage; | |||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | |||
import com.taauav.admin.entity.SysCity; | |||
import com.taauav.admin.entity.TauvWaterData; | |||
import com.taauav.admin.mapper.SysCityMapper; | |||
import com.taauav.admin.mapper.TauvWaterDataMapper; | |||
import com.taauav.admin.query.TauvWaterDataQuery; | |||
import com.taauav.admin.service.ITauvWaterDataService; | |||
import com.taauav.admin.vo.TauvWaterDataVo; | |||
import com.taauav.common.bean.Response; | |||
import com.taauav.common.service.impl.BaseServiceImpl; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.stereotype.Service; | |||
import org.springframework.web.multipart.MultipartFile; | |||
import javax.annotation.Resource; | |||
import java.util.List; | |||
/** | |||
* 水质数据 服务类 | |||
* | |||
* @author daixiantong | |||
* @date 2020-05-14 | |||
*/ | |||
@Service | |||
public class TauvWaterDataServiceImpl extends BaseServiceImpl<TauvWaterDataMapper, TauvWaterData> implements ITauvWaterDataService { | |||
@Resource | |||
private Response response; | |||
@Autowired | |||
private SysCityMapper cityMapper; | |||
/** | |||
* 自定义分页 | |||
* @param waterDataQuery | |||
* @return | |||
*/ | |||
@Override | |||
public Response selectPageList(TauvWaterDataQuery waterDataQuery) { | |||
IPage<TauvWaterData> page = new Page<>(waterDataQuery.getPage(), waterDataQuery.getPageSize()); | |||
IPage<TauvWaterDataVo> list = baseMapper.selectPageList(page, waterDataQuery); | |||
if (list.getRecords().size() > 0) { | |||
for (TauvWaterDataVo data : list.getRecords()) { | |||
SysCity city = cityMapper.selectById(data.getDriverArea()); | |||
data.setAreaName(city == null ? "" : city.getName()); | |||
} | |||
} | |||
return response.success(list); | |||
} | |||
/** | |||
* 导入数据 | |||
* @param file | |||
* @param id | |||
* @return | |||
*/ | |||
@Override | |||
public Response importData(MultipartFile file, Integer id) { | |||
return null; | |||
} | |||
} |
@@ -12,7 +12,6 @@ import com.taauav.common.service.impl.BaseServiceImpl; | |||
import com.taauav.common.util.DateUtil; | |||
import com.taauav.common.util.ShiroUtils; | |||
import com.taauav.common.util.StringUtils; | |||
import org.springframework.beans.factory.annotation.Value; | |||
import org.springframework.stereotype.Service; | |||
import javax.annotation.Resource; | |||
@@ -26,8 +25,6 @@ import javax.annotation.Resource; | |||
@Service | |||
public class TauvWaterStandardServiceImpl extends BaseServiceImpl<TauvWaterStandardMapper, TauvWaterStandard> implements ITauvWaterStandardService { | |||
@Value("${spring.pageSize}") | |||
private Integer pageSize; | |||
@Resource | |||
private Response response; | |||
@@ -0,0 +1,44 @@ | |||
package com.taauav.admin.vo; | |||
import com.fasterxml.jackson.annotation.JsonFormat; | |||
import com.taauav.admin.entity.TauvWaterData; | |||
import lombok.Data; | |||
import java.math.BigInteger; | |||
import java.util.Date; | |||
/** | |||
* 水质数据 扩展实体类 | |||
* | |||
* @author daixiantong | |||
* @date 2020-5-14 | |||
*/ | |||
@Data | |||
public class TauvWaterDataVo extends TauvWaterData { | |||
/** | |||
* 任务单号 | |||
*/ | |||
private String inspectNo; | |||
/** | |||
* 区属id | |||
*/ | |||
private BigInteger driverArea; | |||
/** | |||
* 区属名称 | |||
*/ | |||
private String areaName; | |||
/** | |||
* 河湖名称 | |||
*/ | |||
private String driverName; | |||
/** | |||
* 巡检时间 | |||
*/ | |||
@JsonFormat(pattern = "yyyy-MM-dd") | |||
private Date executionTime; | |||
} |
@@ -0,0 +1,353 @@ | |||
package com.taauav.common.util; | |||
import org.apache.poi.hssf.usermodel.*; | |||
import org.apache.poi.poifs.filesystem.POIFSFileSystem; | |||
import org.apache.poi.ss.usermodel.*; | |||
import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |||
import org.springframework.util.StringUtils; | |||
import org.springframework.web.multipart.MultipartFile; | |||
import java.io.File; | |||
import java.io.FileOutputStream; | |||
import java.io.IOException; | |||
import java.nio.file.Files; | |||
import java.text.DecimalFormat; | |||
import java.util.ArrayList; | |||
import java.util.List; | |||
import java.util.Map; | |||
public class ExcelUtil { | |||
public static String exportExcelFile(List<List<String>> excelData, String path, String sheetName, int columnWidth) { | |||
HSSFWorkbook workbook = new HSSFWorkbook(); | |||
HSSFSheet sheet = workbook.createSheet(sheetName); | |||
if (columnWidth > 0) { | |||
sheet.setDefaultColumnWidth(columnWidth); | |||
} | |||
sheet.setDefaultRowHeightInPoints(18); | |||
int rowIndex = 0; | |||
for (List<String> data : excelData) { | |||
//创建一个row行,然后增值1 | |||
HSSFRow row = sheet.createRow(rowIndex++); | |||
//遍历添加本行数据 | |||
for (int i = 0; i < data.size(); i++) { | |||
HSSFCell cell = row.createCell(i); | |||
//创建一个内容对象 | |||
HSSFRichTextString text = new HSSFRichTextString(data.get(i)); | |||
//将内容对象的文字内容写入到单元格 | |||
cell.setCellValue(text); | |||
} | |||
} | |||
FileOutputStream outputStream; | |||
try { | |||
outputStream = new FileOutputStream(path); | |||
workbook.write(outputStream); | |||
} catch (IOException e) { | |||
e.printStackTrace(); | |||
} | |||
return path; | |||
} | |||
/** | |||
* 导入数据(单页) | |||
* | |||
* @param file 文件 | |||
* @param sheetIndex 页名的索引(从0开始,-1代表全部页) | |||
* @param headerIndex 表头的索引(用于获取共多少列以及第几行开始读数据) | |||
* @return | |||
* @throws IOException | |||
*/ | |||
public static List<List<Object>> importExcel(MultipartFile file, int sheetIndex, int headerIndex) throws Exception { | |||
Workbook workbook = null; | |||
//返回的data | |||
List<List<Object>> data = new ArrayList<>(); | |||
workbook = getWorkbook(file); | |||
//导入某一页 | |||
if (sheetIndex != -1 && sheetIndex > -1) { | |||
Sheet sheet = workbook.getSheetAt(sheetIndex); | |||
List<List<Object>> lists = importOneSheet(sheet, headerIndex); | |||
data.addAll(lists); | |||
} else { | |||
//导入全部 | |||
for (int i = 0; i < workbook.getNumberOfSheets(); i++) { | |||
Sheet sheet = workbook.getSheetAt(i); | |||
if (sheet == null) { | |||
continue; | |||
} | |||
List<List<Object>> lists = importOneSheet(sheet, headerIndex); | |||
data.addAll(lists); | |||
} | |||
} | |||
return data; | |||
} | |||
/** | |||
* 导入数据(所有页) | |||
* | |||
* @param file 文件 | |||
* @param headerIndex 表头的索引(用于获取共多少列以及第几行开始读数据) | |||
* @return | |||
* @throws IOException | |||
*/ | |||
public static List<List<Object>> importExcel(MultipartFile file, int headerIndex) throws Exception { | |||
return importExcel(file, -1, headerIndex); | |||
} | |||
/** | |||
* 获取表头 | |||
* | |||
* @param file | |||
* @param headerIndex | |||
* @return | |||
*/ | |||
public static List<String> getHeadList(MultipartFile file, Integer sheetIndex, Integer headerIndex) throws Exception { | |||
Workbook workbook = null; | |||
workbook = getWorkbook(file); | |||
Sheet sheet = workbook.getSheetAt(sheetIndex); | |||
Integer columnNum = sheet.getRow(headerIndex).getPhysicalNumberOfCells(); | |||
Row row = sheet.getRow(headerIndex); | |||
List<String> headList = new ArrayList<>(); | |||
for (Integer m = 0; m < columnNum; m++) { | |||
headList.add(getCellValue(row.getCell(m))); | |||
} | |||
return headList; | |||
} | |||
/** | |||
* 获取一个sheet里的数据 | |||
* | |||
* @param sheet | |||
* @param headerIndex | |||
* @return | |||
* @throws Exception | |||
*/ | |||
private static List<List<Object>> importOneSheet(Sheet sheet, int headerIndex) throws Exception { | |||
List<List<Object>> data = new ArrayList<>(); | |||
int row = sheet.getLastRowNum(); | |||
//row = -1 表格中没有数据 | |||
//row = headerIndex 表格中表头以下没有数据(指没有有用数据) | |||
if (row == -1 || row == headerIndex) { | |||
throw new Exception("表格中没有有用数据!"); | |||
} | |||
//通过表头获取共多少列 | |||
int coloumNum = sheet.getRow(headerIndex).getPhysicalNumberOfCells(); | |||
//从表头下一行开始取数据 | |||
for (int i = headerIndex + 1; i <= row; i++) { | |||
Row row1 = sheet.getRow(i); | |||
List<Object> list = new ArrayList<>(); | |||
if (row1 != null) { | |||
for (int j = 0; j < coloumNum; j++) { | |||
list.add(getCellValue(row1.getCell(j))); | |||
} | |||
} | |||
data.add(list); | |||
} | |||
return data; | |||
} | |||
/** | |||
* 获取workbook | |||
* | |||
* @return | |||
*/ | |||
private static Workbook getWorkbook(MultipartFile file) throws Exception { | |||
Workbook workbook = null; | |||
//获取文件名 | |||
String fileName = file.getOriginalFilename(); | |||
//判断文件格式 | |||
if (fileName.endsWith("XLS") || fileName.endsWith("xls")) { | |||
workbook = new HSSFWorkbook(file.getInputStream()); | |||
} else if (fileName.endsWith("XLSX") || fileName.endsWith("xlsx")) { | |||
workbook = new XSSFWorkbook(file.getInputStream()); | |||
} else { | |||
throw new Exception("文件格式有误!"); | |||
} | |||
return workbook; | |||
} | |||
/** | |||
* 获取单元格的值 | |||
* | |||
* @param cell | |||
* @return | |||
*/ | |||
private static String getCellValue(Cell cell) { | |||
if (StringUtils.isEmpty(cell)) { | |||
return ""; | |||
} | |||
CellType cellType = cell.getCellTypeEnum(); | |||
String cellValue = ""; | |||
switch (cellType) { | |||
// 数字 | |||
case NUMERIC: | |||
DecimalFormat df = new DecimalFormat("0.00"); | |||
cellValue = df.format(cell.getNumericCellValue()); | |||
break; | |||
// 字符串 | |||
case STRING: | |||
cellValue = cell.getStringCellValue(); | |||
break; | |||
// Boolean | |||
case BOOLEAN: | |||
cellValue = cell.getBooleanCellValue() + ""; | |||
break; | |||
// 公式 | |||
case FORMULA: | |||
cellValue = cell.getCellFormula() + ""; | |||
break; | |||
// 空值 | |||
case BLANK: | |||
cellValue = ""; | |||
break; | |||
// 故障 | |||
case ERROR: | |||
cellValue = "非法字符"; | |||
break; | |||
default: | |||
cellValue = "未知类型"; | |||
break; | |||
} | |||
return cellValue.trim(); | |||
} | |||
/** | |||
* 根据模板导出数据 | |||
* @param templatePath 模板路径(包括文件名) | |||
* @param filePath 导出文件路径(包括文件名) | |||
* @param map 直接渲染数据 | |||
* @param list 遍历渲染数组 | |||
* @return | |||
* @throws IOException | |||
*/ | |||
public static String exportExcelByTemplate(String templatePath, String filePath, Map<String, String> map, List<Map<String, String>> list) throws IOException { | |||
//复制模板到新文件 | |||
File template = new File(templatePath); | |||
File file = new File(filePath); | |||
if (!file.exists()) { | |||
Files.copy(template.toPath(), file.toPath()); | |||
} else { | |||
file.delete(); | |||
Files.copy(template.toPath(), file.toPath()); | |||
} | |||
FileOutputStream outputStream; | |||
try { | |||
POIFSFileSystem fileSystem = new POIFSFileSystem(file); | |||
HSSFWorkbook workbook = new HSSFWorkbook(fileSystem); | |||
Integer sheetNum = workbook.getNumberOfSheets(); | |||
if (sheetNum > 0) { | |||
for (int num = 0; num < sheetNum; num++) { | |||
HSSFSheet sheet = workbook.getSheetAt(num); | |||
//循环map中的键值对,替换excel中对应的键的值(注意,excel模板中的要替换的值必须跟map中的key值对应,不然替换不成功) | |||
if (!StringUtils.isEmpty(map)) { | |||
for (Object atr : map.keySet()) { | |||
//该sheet页里最多有几行内容 | |||
int rowNum = sheet.getLastRowNum(); | |||
String atrStr = "${" + atr + "}"; | |||
for (int i = 0; i < rowNum; i++) { | |||
HSSFRow row = sheet.getRow(i); | |||
int colNum = row.getLastCellNum(); | |||
for (int j = 0; j < colNum; j++) { | |||
HSSFCell cell = row.getCell((short) j); | |||
String str = cell.getStringCellValue(); | |||
if (str.startsWith("$") && atrStr.equals(str)) { | |||
//写入单元格内容 | |||
cell.setCellValue(map.get(atr)); | |||
} | |||
} | |||
} | |||
} | |||
} | |||
if (!StringUtils.isEmpty(list)) { | |||
Map<String, String> item = list.get(0); | |||
int m = -1; | |||
here: | |||
for (Object atr : item.keySet()) { | |||
int rowNum = sheet.getLastRowNum(); | |||
atr = "${item." + atr + "}"; | |||
for (int i = 0; i < rowNum; i++) { | |||
HSSFRow row = sheet.getRow(i); | |||
int colNum = row.getLastCellNum(); | |||
for (int j = 0; j < colNum; j++) { | |||
HSSFCell cell = row.getCell((short) j); | |||
String str = cell.getStringCellValue(); | |||
if (str.startsWith("$") && atr.equals(str)) { | |||
m = i; | |||
break here; | |||
} | |||
} | |||
} | |||
} | |||
if (m >= 0) { | |||
for (int n = 0; n < list.size(); n++) { | |||
if (n < list.size() - 1) { | |||
insertRow(sheet, m, 1); | |||
} | |||
HSSFRow row = sheet.getRow(m); | |||
int colNum = row.getLastCellNum(); | |||
Map<String, String> data = list.get(n); | |||
for (Object word : data.keySet()) { | |||
String wordStr = "${item." + word + "}"; | |||
for (int j = 0; j < colNum; j++) { | |||
HSSFCell cell = row.getCell((short) j); | |||
String str = cell.getStringCellValue(); | |||
if (str.startsWith("$") && wordStr.equals(str)) { | |||
//写入单元格内容 | |||
cell.setCellValue(data.get(word)); | |||
break; | |||
} | |||
} | |||
} | |||
m++; | |||
} | |||
} | |||
} | |||
} | |||
} | |||
// 输出文件 | |||
outputStream = new FileOutputStream(filePath); | |||
workbook.write(outputStream); | |||
outputStream.close(); | |||
} catch (Exception e) { | |||
e.printStackTrace(); | |||
} | |||
return filePath; | |||
} | |||
/** | |||
* 插入行 | |||
* insert row into the target sheet, the style of cell is the same as startRow | |||
* | |||
* @param sheet | |||
* @param starRow | |||
* @param rows | |||
*/ | |||
public static void insertRow(HSSFSheet sheet, int starRow, int rows) { | |||
sheet.shiftRows(starRow + 1, sheet.getLastRowNum(), rows, true, false); | |||
starRow = starRow - 1; | |||
for (int i = 0; i < rows; i++) { | |||
HSSFRow sourceRow = null; | |||
HSSFRow targetRow = null; | |||
HSSFCell sourceCell = null; | |||
HSSFCell targetCell = null; | |||
short m; | |||
starRow = starRow + 1; | |||
sourceRow = sheet.getRow(starRow); | |||
targetRow = sheet.createRow(starRow + 1); | |||
targetRow.setHeight(sourceRow.getHeight()); | |||
for (m = sourceRow.getFirstCellNum(); m < sourceRow.getLastCellNum(); m++) { | |||
sourceCell = sourceRow.getCell(m); | |||
targetCell = targetRow.createCell(m); | |||
targetCell.setCellStyle(sourceCell.getCellStyle()); | |||
targetCell.setCellValue(sourceCell.getStringCellValue()); | |||
} | |||
} | |||
} | |||
} |