package com.thhy.materials.modules.biz.utils;
|
|
import com.thhy.general.annotations.Excel;
|
import com.thhy.materials.modules.biz.materialsplan.entity.StatResultVo;
|
import com.thhy.materials.modules.biz.materialsplan.entity.StatVo;
|
import org.apache.poi.hssf.usermodel.HSSFCell;
|
import org.apache.poi.hssf.usermodel.HSSFRow;
|
import org.apache.poi.hssf.usermodel.HSSFSheet;
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
import org.apache.poi.hssf.util.HSSFColor;
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.io.OutputStream;
|
import java.lang.annotation.Annotation;
|
import java.lang.reflect.Field;
|
import java.net.URLEncoder;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
import static com.thhy.general.utils.ExcelUtils.HeaderStyle;
|
|
public class ExcelExtendUtils {
|
|
public static void create(List<StatResultVo> sheets, String fileName, HttpServletResponse response){
|
//声明一个工作簿
|
HSSFWorkbook workbook = new HSSFWorkbook();
|
|
sheets.forEach(sh ->{
|
HSSFSheet sheet = workbook.createSheet(sh.getMaterialCName());
|
sheet.setDefaultColumnWidth(20);
|
|
//创建表头
|
HSSFRow rows = sheet.createRow(0);
|
rows.setHeight((short) 403);
|
HSSFCell cell = rows.createCell(0);
|
sheet.setColumnWidth(0,1311*3);
|
cell.setCellStyle(HeaderStyle(workbook, HorizontalAlignment.CENTER, VerticalAlignment.CENTER,11,(short) -1, HSSFColor.HSSFColorPredefined.GREY_40_PERCENT.getIndex(),true));
|
cell.setCellValue("时间");
|
HSSFCell cell1 = rows.createCell(1);
|
sheet.setColumnWidth(1,1311*3);
|
cell1.setCellStyle(HeaderStyle(workbook, HorizontalAlignment.CENTER, VerticalAlignment.CENTER,11,(short) -1, HSSFColor.HSSFColorPredefined.GREY_40_PERCENT.getIndex(),true));
|
cell1.setCellValue("用量(kg)");
|
|
List<StatVo> statVoList = sh.getStatVoList();
|
|
for(int i = 0;i<statVoList.size();i++){
|
HSSFRow row = sheet.createRow(i+1);
|
row.setHeight((short) 375);
|
for(int j =0;j<2;j++){
|
HSSFCell cell3 = row.createCell(j);
|
cell3.setCellValue(j==0?statVoList.get(i).getShuDate():String.valueOf(statVoList.get(i).getMaterialValue()));
|
}
|
}
|
});
|
OutputStream outputStream = null;
|
try {
|
fileName = fileName+".xls";
|
//设置编码、输出文件格式
|
response.setContentType("application/msexcel");
|
response.setCharacterEncoding("utf-8");
|
fileName = URLEncoder.encode(fileName,"UTF-8");
|
response.setHeader("Content-Disposition","attachment;filename="+fileName);
|
outputStream = response.getOutputStream();
|
workbook.write(outputStream);
|
//byte[] bytes = workbook.getBytes();
|
//outputStream.write(bytes);
|
} catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
if(outputStream!=null){
|
try {
|
outputStream.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|
}
|