Examination_system/Examination_system-1/.svn/pristine/cc/cc16c54af0531dcec5350f33cc1...

194 lines
8.1 KiB
Plaintext
Raw Normal View History

2023-10-30 13:10:40 +08:00
package com.bonus.exp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
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.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import com.bonus.core.DateTimeHelper;
@SuppressWarnings("deprecation")
public class POIOutputHelperSafetyBalance {
public static HSSFWorkbook excel(List<Map<String, Object>> result, List<String> list, String filename,String projectName) {
// 获取工作簿对象
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFCellStyle tittleStyle = createTittleStyle(workbook);
HSSFCellStyle headerStyle = createHeaderStyle(workbook);
HSSFCellStyle contentStyle = createCellStyle(workbook);
String sheetName = filename;
sheet.setDefaultColumnWidth(15);
HSSFRow row;
workbook.setSheetName(0, sheetName);
HSSFCell cell;
int nColumn = list.size();
String time = "时间:"+DateTimeHelper.getNowDate();
//表头
HSSFRow row0 = sheet.createRow(0);
row0.setHeight((short) 700);
String[] row_second = {sheetName};
for (int i= 0; i < row_second.length; i++) {
HSSFCell tempCell = row0.createCell(i);
tempCell.setCellValue(row_second[i]);
tempCell.setCellStyle(tittleStyle);
}
HSSFRow row3 = sheet.createRow(1);
row3.setHeight((short) 500);
String[] row_second3 = {projectName,"","","","","","","",time,"","","","",""};
for (int i= 0; i < row_second.length; i++) {
HSSFCell tempCell = row3.createCell(i);
tempCell.setCellValue(row_second3[i]);
tempCell.setCellStyle(headerStyle);
}
//第一行
HSSFRow row1 = sheet.createRow(2);
row1.setHeight((short) 400);
String[] row_second1 = {"序号", "设备名称", "规格型号", "单位", "领用数量","","","","退回数量","","","","","备注"};
for (int h = 0; h < row_second1.length; h++) {
HSSFCell tempCell = row1.createCell(h);
tempCell.setCellValue(row_second1[h]);
tempCell.setCellStyle(headerStyle);
}
int ii = 3;
row = sheet.createRow((short) ii);
//row.setHeightInPoints(20);
for (int j = 0; j < nColumn; j++) {
cell = row.createCell((short) j);
if (list.get(j) != null) {
cell.setCellStyle(headerStyle);
cell.setCellValue(list.get(j));
} else {
cell.setCellStyle(headerStyle);
cell.setCellValue("");
}
if(j>=0 && j<4 ||j==13) {
sheet.addMergedRegion(new CellRangeAddress(2, 3, j, j));
}
sheet.addMergedRegion(new CellRangeAddress(0, 1, j,j));
}
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 8, 13));
sheet.addMergedRegion(new CellRangeAddress(2, 2, 4, 7));
sheet.addMergedRegion(new CellRangeAddress(2, 2, 8, 12));
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 13));
ii++;
for (int i = 0; i < result.size(); i++) {
Map<String, Object> resulttrow = result.get(i);
List<Object> rowdata = map2List(resulttrow);
row = sheet.createRow((short) ii);
row.setHeightInPoints(15);
for (int j = 0; j < nColumn; j++) {
cell = row.createCell((short) j);
if (rowdata.get(j) != null) {
Object data = rowdata.get(j);
Boolean isNum = false;//data是否为数值
if (data != null || "".equals(data)) {
//判断data是否为数值型
isNum = data.toString().matches("^(-?\\d+)(\\.\\d+)?$");
}
//如果单元格内容是数值类型则设置cell的类型为数值型设置data的类型为数值类型
if (isNum ) {
// 设置单元格内容为double类型
cell.setCellValue(Double.parseDouble(data.toString()));
//contentStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));//保留两位小数点
// HSSFDataFormat df = workbook.createDataFormat(); //此处设置数据格式  
// contentStyle.setDataFormat(df.getFormat("0.000")); //小数点后保留两位可以写contentStyle.setDataFormat(df.getFormat("#,#0.00"));  
// 设置单元格格式
cell.setCellStyle(contentStyle);
} else {
cell.setCellStyle(contentStyle);
// 设置单元格内容为字符型
cell.setCellValue(data.toString());
}
} else {
cell.setCellStyle(contentStyle);
cell.setCellValue("");
}
}
ii++;
}
return workbook;
}
public static List<Object> map2List(Map<String, Object> map) {
// 将Map Key 转化为List
List<Object> mapValuesList = new ArrayList<Object>(map.values());
return mapValuesList;
}
public static HSSFCellStyle createTittleStyle(HSSFWorkbook workbook){
HSSFCellStyle tittleStyle = workbook.createCellStyle();
tittleStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 垂直居中
tittleStyle.setAlignment(CellStyle.ALIGN_CENTER);// 水平居中
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short) 16);// 设置字体大小
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//设置加粗
tittleStyle.setWrapText(true);
tittleStyle.setFont(font);
tittleStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);//填充单元格
tittleStyle.setFillForegroundColor(HSSFColor.WHITE.index);//填充颜色
tittleStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
tittleStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
tittleStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
tittleStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
return tittleStyle;
}
public static HSSFCellStyle createHeaderStyle(HSSFWorkbook workbook){
HSSFCellStyle headerStyle = workbook.createCellStyle();
headerStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 垂直居中
headerStyle.setAlignment(CellStyle.ALIGN_CENTER);// 水平居中
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headerStyle.setFillForegroundColor(HSSFColor.WHITE.index);
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
Font font = workbook.createFont();
font.setFontHeightInPoints((short)12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// font.setColor(HSSFColor.GREEN.index); //绿字
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerStyle.setFont(font);
return headerStyle;
}
public static HSSFCellStyle createCellStyle(HSSFWorkbook workbook){
HSSFCellStyle contentStyle = workbook.createCellStyle();
contentStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);// 垂直居中
contentStyle.setAlignment(CellStyle.ALIGN_CENTER);// 水平居中
contentStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
contentStyle.setFillForegroundColor(HSSFColor.WHITE.index);
contentStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); //下边框
contentStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);//左边框
contentStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);//上边框
contentStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);//右边框
Font font = workbook.createFont();
font.setFontHeightInPoints((short)10);
font.setColor(HSSFColor.BLACK.index); //黑字
contentStyle.setFont(font);
return contentStyle;
}
}