Commit bea3477f authored by shulidong's avatar shulidong

委托单产值导出方法2

parent a5b58c15
...@@ -81,10 +81,12 @@ public class EntrustController { ...@@ -81,10 +81,12 @@ public class EntrustController {
public void export(String startDate, String endDate, Integer status, public void export(String startDate, String endDate, Integer status,
Integer clientId, String projectName, String projectCode, Integer clientId, String projectName, String projectCode,
String fileName, HttpServletResponse response) { String fileName, HttpServletResponse response) {
long a = System.currentTimeMillis();
try { try {
entrustService.export(startDate, endDate, status, entrustService.exportConsignation(startDate, endDate, status,
clientId, projectName, projectCode, clientId, projectName, projectCode,
fileName, response); fileName, response);
System.out.println(System.currentTimeMillis()-a);
} catch (Exception e) { } catch (Exception e) {
log.debug("委托列表导出{}", e); log.debug("委托列表导出{}", e);
} }
......
...@@ -144,6 +144,10 @@ public interface IEntrustService extends IService<Entrust> { ...@@ -144,6 +144,10 @@ public interface IEntrustService extends IService<Entrust> {
Integer clientId, String projectName, String projectCode, Integer clientId, String projectName, String projectCode,
String fileName, HttpServletResponse response); String fileName, HttpServletResponse response);
void exportConsignation(String startDate, String endDate, Integer status,
Integer clientId, String projectName, String projectCode,
String fileName, HttpServletResponse response);
BaseResponse<EntrustVo> getBaseDtail(Integer id); BaseResponse<EntrustVo> getBaseDtail(Integer id);
BaseResponse<EntrustVo> getBaseDtailClient(Integer id); BaseResponse<EntrustVo> getBaseDtailClient(Integer id);
......
...@@ -23,6 +23,15 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; ...@@ -23,6 +23,15 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.Data; import lombok.Data;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
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.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -5332,6 +5341,181 @@ public class EntrustServiceImpl extends ServiceImpl<EntrustMapper, Entrust> impl ...@@ -5332,6 +5341,181 @@ public class EntrustServiceImpl extends ServiceImpl<EntrustMapper, Entrust> impl
} }
} }
@Resource
private ITeamService teamService;
@Resource
private ISampleDistributionService sampleDistributionService;
@Override
public void exportConsignation(String startDate, String endDate, Integer status,
Integer clientId, String projectName, String projectCode,
String fileName, HttpServletResponse response) {
//1.首先查询所有可见的检测项
QueryWrapper<Team> teamQueryWrapper = new QueryWrapper<>();
teamQueryWrapper.eq("is_display", 1);
List<Team> teamList = teamService.list(teamQueryWrapper);
//转化成id为key的map
Map<Integer, Team> teamMap = teamList.stream().collect(Collectors.toMap(Team::getId, a -> a, (k1, k2) -> k1));
//2.条件查询委托单项
Map<String, Object> params = new HashMap<>();
params.put("startDate", startDate);
params.put("endDate", endDate);
params.put("status", status);
params.put("clientId", clientId);
params.put("projectName", projectName);
params.put("projectCode", projectCode);
List<Map<String, Object>> entrustList = entrustMapper.exportList(params);
//获取所有的委托单号
List<Integer> queryList = entrustList.stream().map(x -> Integer.parseInt(x.get("entrustId").toString())).collect(Collectors.toList());
//3.根据委托单号获取样品
QueryWrapper<Sample> queryWrapper = new QueryWrapper<>();
queryWrapper.in("entrust_id", queryList);
List<Sample> sampleList = sampleService.list(queryWrapper);
//4.查询所有的派发检测项
QueryWrapper<SampleDistribution> sdqueryWrapper = new QueryWrapper<>();
sdqueryWrapper.in("entrust_id", queryList);
sdqueryWrapper.in("sample_id", sampleList.stream().filter(x -> x.getCementCode().equals(x.getParallelCode())).map(Sample::getId).collect(Collectors.toList()));
List<SampleDistribution> sampleDistributions = sampleDistributionService.list(sdqueryWrapper);
//5.分组并统计
Map<Integer, List<SampleDistribution>> groupByEntrustId = sampleDistributions.stream().collect(Collectors.groupingBy(SampleDistribution::getEntrustId, Collectors.toList()));
//6.开始填充数据。首先填充表头
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet();
XSSFCellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
//水平居中
style.setAlignment(HorizontalAlignment.CENTER);
//垂直居中
style.setVerticalAlignment(VerticalAlignment.CENTER);
//生成表头
String[] in_fo = {
"委托单号",
"委托单位",
"项目编号",
"项目名称",
"委托人",
"样品数量",
"发样日期",
"是否加急",
"实际产值",
};
List<String> infoHeader = Arrays.asList(in_fo);
generatoronsignationWorkbook(font,style, sheet, infoHeader, teamList.stream().map(Team::getName).collect(Collectors.toList()));
//填充数据,从第二行开始
int rownum = 2;
for (int j = 0; j < entrustList.size(); j++) {
Map<String, Object> entrust = entrustList.get(j);
int entrustId = Integer.parseInt(entrust.get("entrustId").toString());
if (groupByEntrustId.get(entrustId) != null) {
XSSFRow row0 = sheet.createRow(rownum++);
Map<Integer, List<SampleDistribution>> sampleDistribuGroup = groupByEntrustId.get(entrustId).stream().collect(Collectors.groupingBy(SampleDistribution::getTeamId, Collectors.toList()));
//处理委托人信息表头
for (int i = 0; i < infoHeader.size(); i++) {
XSSFCell tempCell = row0.createCell(i);
tempCell.setCellValue(entrust.get(infoHeader.get(i)) == null ? "" : entrust.get(infoHeader.get(i)).toString());
tempCell.setCellStyle(style);
}
//处理检测项总产值表头
for (int i = infoHeader.size(); i < teamList.size() + infoHeader.size(); i++) {
XSSFCell tempCell = row0.createCell(i);
Team team = teamList.get(i - infoHeader.size());
tempCell.setCellValue(sampleDistribuGroup.get(team.getId()) == null ? "" : team.getCharge().multiply(new BigDecimal(sampleDistribuGroup.get(team.getId()).size())).toString());
tempCell.setCellStyle(style);
}
//增加1空列
{
XSSFCell tempCell = row0.createCell(teamList.size() + infoHeader.size());
tempCell.setCellValue("");
tempCell.setCellStyle(style);
}
//处理检测项信息表头
for (int i = teamList.size() + infoHeader.size() + 1; i < infoHeader.size() + 1 + teamList.size() * 3; i++) {
Team team = teamList.get((i - teamList.size() - infoHeader.size() - 1) / 2);
XSSFCell tempCell1 = row0.createCell(i);
tempCell1.setCellStyle(style);
tempCell1.setCellValue(sampleDistribuGroup.get(team.getId()) == null ? "" : team.getCharge().toString());
XSSFCell tempCell2 = row0.createCell(++i);
tempCell2.setCellValue(sampleDistribuGroup.get(team.getId()) == null ? "" : sampleDistribuGroup.get(team.getId()).size() + "");
tempCell2.setCellStyle(style);
}
}
}
ExcelUtil.excelExportNew(fileName == null || fileName.trim().length() <= 0 ? "委托列表" : fileName, wb, response);
}
private static void generatoronsignationWorkbook(XSSFFont font ,XSSFCellStyle style, XSSFSheet sheet, List<String> infoHeader, List<String> teamsHeader) {
//委托项的表头
int rownum = 0;
//创建第一行
XSSFRow row0 = sheet.createRow(rownum++);
//处理委托人信息表头
for (int i = 0; i < infoHeader.size(); i++) {
XSSFCell tempCell = row0.createCell(i);
tempCell.setCellValue(infoHeader.get(i));
tempCell.setCellStyle(style);
}
//处理检测项总产值表头
for (int i = infoHeader.size(); i < teamsHeader.size() + infoHeader.size(); i++) {
XSSFCell tempCell = row0.createCell(i);
tempCell.setCellValue(ExcelUtil.applyFontsub(teamsHeader.get(i - infoHeader.size()),font));
tempCell.setCellStyle(style);
}
//增加1空列
{
XSSFCell tempCell = row0.createCell(teamsHeader.size() + infoHeader.size());
tempCell.setCellValue("");
tempCell.setCellStyle(style);
}
//处理检测项信息表头
for (int i = teamsHeader.size() + infoHeader.size() + 1; i < infoHeader.size() + 1 + teamsHeader.size() * 3; i++) {
String header = teamsHeader.get((i - teamsHeader.size() - infoHeader.size() - 1) / 2);
XSSFCell tempCell1 = row0.createCell(i);
tempCell1.setCellStyle(style);
tempCell1.setCellValue(ExcelUtil.applyFontsub(header,font));
XSSFCell tempCell2 = row0.createCell(++i);
tempCell2.setCellValue(ExcelUtil.applyFontsub(header,font));
tempCell2.setCellStyle(style);
//合并表头
sheet.addMergedRegion(new CellRangeAddress(0, 0, i - 1, i));
}
//创建第二行
XSSFRow row1 = sheet.createRow(rownum++);
//处理委托人信息表头
for (int i = 0; i < infoHeader.size(); i++) {
XSSFCell tempCell = row1.createCell(i);
tempCell.setCellValue("");
tempCell.setCellStyle(style);
//合并表头
sheet.addMergedRegion(new CellRangeAddress(0, 1, i, i));
}
//处理检测项总产值表头
for (int i = infoHeader.size(); i < teamsHeader.size() + infoHeader.size(); i++) {
XSSFCell tempCell = row1.createCell(i);
tempCell.setCellValue("计算产值");
tempCell.setCellStyle(style);
}
//增加1空列
{
XSSFCell tempCell = row0.createCell(teamsHeader.size() + infoHeader.size());
tempCell.setCellValue("");
tempCell.setCellStyle(style);
}
//处理检测项信息表头
for (int i = teamsHeader.size() + 1 + infoHeader.size(); i < infoHeader.size() + 1 + teamsHeader.size() * 3; i++) {
XSSFCell tempCell1 = row1.createCell(i);
tempCell1.setCellValue("单价");
tempCell1.setCellStyle(style);
XSSFCell tempCell2 = row1.createCell(++i);
tempCell2.setCellValue("数量");
tempCell2.setCellStyle(style);
}
}
/** /**
* 查看样品流转信息 * 查看样品流转信息
......
package cn.wise.sc.cement.business.util; package cn.wise.sc.cement.business.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import cn.afterturn.easypoi.excel.annotation.Excel;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.FontFormatting;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.wp.usermodel.Paragraph;
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFFont;
...@@ -26,6 +19,15 @@ import org.apache.poi.xssf.usermodel.XSSFRow; ...@@ -26,6 +19,15 @@ import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
/** /**
* excel工具类 * excel工具类
* *
...@@ -159,6 +161,42 @@ public class ExcelUtil { ...@@ -159,6 +161,42 @@ public class ExcelUtil {
} }
} }
public static void excelExportNew(String fileName, Workbook workbook,
HttpServletResponse response) {
if (workbook != null) {
ByteArrayOutputStream byteArrayOutputStream = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
workbook.write(byteArrayOutputStream);
String suffix = ".xls";
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition",
"attachment;filename=" + new String((fileName + suffix).getBytes(), "iso-8859-1"));
OutputStream outputStream = response.getOutputStream();
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (byteArrayOutputStream != null) {
byteArrayOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
workbook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/** /**
* @param headers 列头 * @param headers 列头
* @param datas 数据 * @param datas 数据
...@@ -244,7 +282,7 @@ public class ExcelUtil { ...@@ -244,7 +282,7 @@ public class ExcelUtil {
applyRichStringFontsub(value, richString, xxsfFont); applyRichStringFontsub(value, richString, xxsfFont);
cell.setCellValue(richString); cell.setCellValue(richString);
// value = cell.getStringCellValue(); // value = cell.getStringCellValue();
}else{ } else {
cell.setCellValue(value); cell.setCellValue(value);
cell.setCellStyle(style); cell.setCellStyle(style);
} }
...@@ -259,7 +297,6 @@ public class ExcelUtil { ...@@ -259,7 +297,6 @@ public class ExcelUtil {
}*/ }*/
} }
} }
} }
...@@ -287,6 +324,19 @@ public class ExcelUtil { ...@@ -287,6 +324,19 @@ public class ExcelUtil {
} }
} }
public static XSSFRichTextString applyFontsub(String sub, XSSFFont xxsfFont) {
XSSFRichTextString richString = new XSSFRichTextString();
if (sub.contains("<sub>") || sub.contains("</sub>")) {
xxsfFont.setTypeOffset(FontFormatting.SS_SUB);
richString.setString(sub.replaceAll("</sub>", "<sub>").replaceAll("<sub>", ""));
//提取下标位置
applyRichStringFontsub(sub, richString, xxsfFont);
} else {
richString.setString(sub);
}
return richString;
}
private static void applyRichStringFontsup(String sup, XSSFRichTextString richString, XSSFFont xxsfFont) { private static void applyRichStringFontsup(String sup, XSSFRichTextString richString, XSSFFont xxsfFont) {
String[] split = sup.replaceAll("</sup>", "<sup>").split("<sup>"); String[] split = sup.replaceAll("</sup>", "<sup>").split("<sup>");
String it_str = ""; String it_str = "";
...@@ -371,7 +421,7 @@ public class ExcelUtil { ...@@ -371,7 +421,7 @@ public class ExcelUtil {
private static XSSFWorkbook generatoronsignationWorkbook(List<String> infoHeader, List<String> teamsHeader) { private static XSSFWorkbook generatoronsignationWorkbook(List<String> infoHeader, List<String> teamsHeader) {
XSSFWorkbook wb = new XSSFWorkbook(); XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("sheet1"); XSSFSheet sheet = wb.createSheet("sheet1");
XSSFCellStyle style=wb.createCellStyle(); XSSFCellStyle style = wb.createCellStyle();
//水平居中 //水平居中
style.setAlignment(HorizontalAlignment.CENTER); style.setAlignment(HorizontalAlignment.CENTER);
//垂直居中 //垂直居中
...@@ -409,7 +459,7 @@ public class ExcelUtil { ...@@ -409,7 +459,7 @@ public class ExcelUtil {
tempCell2.setCellValue(header); tempCell2.setCellValue(header);
tempCell2.setCellStyle(style); tempCell2.setCellStyle(style);
//合并表头 //合并表头
sheet.addMergedRegion(new CellRangeAddress(0, 0, i-1, i )); sheet.addMergedRegion(new CellRangeAddress(0, 0, i - 1, i));
} }
//创建第二行 //创建第二行
XSSFRow row1 = sheet.createRow(rownum++); XSSFRow row1 = sheet.createRow(rownum++);
...@@ -434,7 +484,7 @@ public class ExcelUtil { ...@@ -434,7 +484,7 @@ public class ExcelUtil {
tempCell.setCellStyle(style); tempCell.setCellStyle(style);
} }
//处理检测项信息表头 //处理检测项信息表头
for (int i = teamsHeader.size()+1 + infoHeader.size(); i < infoHeader.size()+1 + teamsHeader.size() * 3; i++) { for (int i = teamsHeader.size() + 1 + infoHeader.size(); i < infoHeader.size() + 1 + teamsHeader.size() * 3; i++) {
XSSFCell tempCell1 = row1.createCell(i); XSSFCell tempCell1 = row1.createCell(i);
tempCell1.setCellValue("单价"); tempCell1.setCellValue("单价");
tempCell1.setCellStyle(style); tempCell1.setCellStyle(style);
......
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