Commit a9ef062ebd7a780cd8ed852a0f214b413a384713
1 parent
b3edaa75
1.
Showing
2 changed files
with
565 additions
and
221 deletions
src/main/java/com/bsth/entity/Energy.java
0 → 100644
| 1 | +package com.bsth.entity; | ||
| 2 | + | ||
| 3 | +import com.fasterxml.jackson.annotation.JsonFormat; | ||
| 4 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
| 5 | + | ||
| 6 | +import javax.persistence.Column; | ||
| 7 | +import javax.persistence.GeneratedValue; | ||
| 8 | +import javax.persistence.GenerationType; | ||
| 9 | +import javax.persistence.Id; | ||
| 10 | +import java.util.Date; | ||
| 11 | + | ||
| 12 | +/** | ||
| 13 | + * @author Hill | ||
| 14 | + */ | ||
| 15 | +@JsonIgnoreProperties(ignoreUnknown = true) | ||
| 16 | +public class Energy { | ||
| 17 | + | ||
| 18 | + @Id | ||
| 19 | + @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| 20 | + private Integer id; | ||
| 21 | + | ||
| 22 | + /** | ||
| 23 | + * 能源站点 | ||
| 24 | + */ | ||
| 25 | + private String energyStation; | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * 组织单位 | ||
| 29 | + */ | ||
| 30 | + private String unit; | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * 客户名 | ||
| 34 | + */ | ||
| 35 | + private String customer; | ||
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * 车牌号 | ||
| 39 | + */ | ||
| 40 | + private String plate; | ||
| 41 | + | ||
| 42 | + /** | ||
| 43 | + * 起始时间 | ||
| 44 | + */ | ||
| 45 | + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | ||
| 46 | + private Date startDate; | ||
| 47 | + | ||
| 48 | + /** | ||
| 49 | + * 结束时间 | ||
| 50 | + */ | ||
| 51 | + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | ||
| 52 | + private Date terminalDate; | ||
| 53 | + | ||
| 54 | + /** | ||
| 55 | + * 充电时长(分钟) | ||
| 56 | + */ | ||
| 57 | + private int chargingMinutes; | ||
| 58 | + | ||
| 59 | + /** | ||
| 60 | + * 尖时电量 | ||
| 61 | + */ | ||
| 62 | + private float energy1; | ||
| 63 | + | ||
| 64 | + /** | ||
| 65 | + * 峰时电量 | ||
| 66 | + */ | ||
| 67 | + private float energy2; | ||
| 68 | + | ||
| 69 | + /** | ||
| 70 | + * 平时电量 | ||
| 71 | + */ | ||
| 72 | + private float energy3; | ||
| 73 | + | ||
| 74 | + /** | ||
| 75 | + * 谷时电量 | ||
| 76 | + */ | ||
| 77 | + private float energy4; | ||
| 78 | + | ||
| 79 | + /** | ||
| 80 | + * 原始电量 | ||
| 81 | + */ | ||
| 82 | + private float originEnergy; | ||
| 83 | + | ||
| 84 | + /** | ||
| 85 | + * 电量费用 | ||
| 86 | + */ | ||
| 87 | + private float energyCost; | ||
| 88 | + | ||
| 89 | + /** | ||
| 90 | + * 服务费用 | ||
| 91 | + */ | ||
| 92 | + private float serviceCost; | ||
| 93 | + | ||
| 94 | + /** | ||
| 95 | + * 总费用 | ||
| 96 | + */ | ||
| 97 | + private float totalCost; | ||
| 98 | + | ||
| 99 | + /** | ||
| 100 | + * 创建日期 | ||
| 101 | + */ | ||
| 102 | + @Column(updatable = false, name = "create_date", columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP") | ||
| 103 | + private Date createDate; | ||
| 104 | + | ||
| 105 | + /** | ||
| 106 | + * 更新日期 | ||
| 107 | + */ | ||
| 108 | + @Column(name = "update_date", columnDefinition = "timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP") | ||
| 109 | + private Date updateDate; | ||
| 110 | + | ||
| 111 | + public Integer getId() { | ||
| 112 | + return id; | ||
| 113 | + } | ||
| 114 | + | ||
| 115 | + public void setId(Integer id) { | ||
| 116 | + this.id = id; | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + public String getEnergyStation() { | ||
| 120 | + return energyStation; | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + public void setEnergyStation(String energyStation) { | ||
| 124 | + this.energyStation = energyStation; | ||
| 125 | + } | ||
| 126 | + | ||
| 127 | + public String getUnit() { | ||
| 128 | + return unit; | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + public void setUnit(String unit) { | ||
| 132 | + this.unit = unit; | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + public String getCustomer() { | ||
| 136 | + return customer; | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + public void setCustomer(String customer) { | ||
| 140 | + this.customer = customer; | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + public String getPlate() { | ||
| 144 | + return plate; | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + public void setPlate(String plate) { | ||
| 148 | + this.plate = plate; | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + public Date getStartDate() { | ||
| 152 | + return startDate; | ||
| 153 | + } | ||
| 154 | + | ||
| 155 | + public void setStartDate(Date startDate) { | ||
| 156 | + this.startDate = startDate; | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + public Date getTerminalDate() { | ||
| 160 | + return terminalDate; | ||
| 161 | + } | ||
| 162 | + | ||
| 163 | + public void setTerminalDate(Date terminalDate) { | ||
| 164 | + this.terminalDate = terminalDate; | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + public int getChargingMinutes() { | ||
| 168 | + return chargingMinutes; | ||
| 169 | + } | ||
| 170 | + | ||
| 171 | + public void setChargingMinutes(int chargingMinutes) { | ||
| 172 | + this.chargingMinutes = chargingMinutes; | ||
| 173 | + } | ||
| 174 | + | ||
| 175 | + public float getEnergy1() { | ||
| 176 | + return energy1; | ||
| 177 | + } | ||
| 178 | + | ||
| 179 | + public void setEnergy1(float energy1) { | ||
| 180 | + this.energy1 = energy1; | ||
| 181 | + } | ||
| 182 | + | ||
| 183 | + public float getEnergy2() { | ||
| 184 | + return energy2; | ||
| 185 | + } | ||
| 186 | + | ||
| 187 | + public void setEnergy2(float energy2) { | ||
| 188 | + this.energy2 = energy2; | ||
| 189 | + } | ||
| 190 | + | ||
| 191 | + public float getEnergy3() { | ||
| 192 | + return energy3; | ||
| 193 | + } | ||
| 194 | + | ||
| 195 | + public void setEnergy3(float energy3) { | ||
| 196 | + this.energy3 = energy3; | ||
| 197 | + } | ||
| 198 | + | ||
| 199 | + public float getEnergy4() { | ||
| 200 | + return energy4; | ||
| 201 | + } | ||
| 202 | + | ||
| 203 | + public void setEnergy4(float energy4) { | ||
| 204 | + this.energy4 = energy4; | ||
| 205 | + } | ||
| 206 | + | ||
| 207 | + public float getOriginEnergy() { | ||
| 208 | + return originEnergy; | ||
| 209 | + } | ||
| 210 | + | ||
| 211 | + public void setOriginEnergy(float originEnergy) { | ||
| 212 | + this.originEnergy = originEnergy; | ||
| 213 | + } | ||
| 214 | + | ||
| 215 | + public float getEnergyCost() { | ||
| 216 | + return energyCost; | ||
| 217 | + } | ||
| 218 | + | ||
| 219 | + public void setEnergyCost(float energyCost) { | ||
| 220 | + this.energyCost = energyCost; | ||
| 221 | + } | ||
| 222 | + | ||
| 223 | + public float getServiceCost() { | ||
| 224 | + return serviceCost; | ||
| 225 | + } | ||
| 226 | + | ||
| 227 | + public void setServiceCost(float serviceCost) { | ||
| 228 | + this.serviceCost = serviceCost; | ||
| 229 | + } | ||
| 230 | + | ||
| 231 | + public float getTotalCost() { | ||
| 232 | + return totalCost; | ||
| 233 | + } | ||
| 234 | + | ||
| 235 | + public void setTotalCost(float totalCost) { | ||
| 236 | + this.totalCost = totalCost; | ||
| 237 | + } | ||
| 238 | + | ||
| 239 | + public Date getCreateDate() { | ||
| 240 | + return createDate; | ||
| 241 | + } | ||
| 242 | + | ||
| 243 | + public void setCreateDate(Date createDate) { | ||
| 244 | + this.createDate = createDate; | ||
| 245 | + } | ||
| 246 | + | ||
| 247 | + public Date getUpdateDate() { | ||
| 248 | + return updateDate; | ||
| 249 | + } | ||
| 250 | + | ||
| 251 | + public void setUpdateDate(Date updateDate) { | ||
| 252 | + this.updateDate = updateDate; | ||
| 253 | + } | ||
| 254 | +} |
src/main/java/com/bsth/service/schedule/utils/PoiUtils.java
| 1 | -package com.bsth.service.schedule.utils; | ||
| 2 | - | ||
| 3 | -import org.apache.poi.hssf.usermodel.HSSFDateUtil; | ||
| 4 | -import org.apache.poi.ss.usermodel.*; | ||
| 5 | -import org.apache.poi.xssf.usermodel.*; | ||
| 6 | - | ||
| 7 | -import java.awt.Color; | ||
| 8 | -import java.text.DecimalFormat; | ||
| 9 | -import java.text.SimpleDateFormat; | ||
| 10 | -import java.util.Date; | ||
| 11 | - | ||
| 12 | -/** | ||
| 13 | - * POI操作的until方法。 | ||
| 14 | - */ | ||
| 15 | -public class PoiUtils { | ||
| 16 | - /** | ||
| 17 | - * 使用poi读取cell中的值 | ||
| 18 | - * @param cell | ||
| 19 | - * @return | ||
| 20 | - */ | ||
| 21 | - public static String getStringValueFromCell(Cell cell) { | ||
| 22 | - SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd"); | ||
| 23 | - DecimalFormat decimalFormat = new DecimalFormat("#.#"); | ||
| 24 | - String cellValue = ""; | ||
| 25 | - if(cell == null) { | ||
| 26 | - return cellValue; | ||
| 27 | - } | ||
| 28 | - else if(cell.getCellType() == Cell.CELL_TYPE_STRING) { | ||
| 29 | - cellValue = cell.getStringCellValue(); | ||
| 30 | - } | ||
| 31 | - | ||
| 32 | - else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { | ||
| 33 | - if(HSSFDateUtil.isCellDateFormatted(cell)) { | ||
| 34 | - double d = cell.getNumericCellValue(); | ||
| 35 | - Date date = HSSFDateUtil.getJavaDate(d); | ||
| 36 | - cellValue = sFormat.format(date); | ||
| 37 | - } | ||
| 38 | - else { | ||
| 39 | - cellValue = decimalFormat.format((cell.getNumericCellValue())); | ||
| 40 | - } | ||
| 41 | - } | ||
| 42 | - else if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { | ||
| 43 | - cellValue = ""; | ||
| 44 | - } | ||
| 45 | - else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { | ||
| 46 | - cellValue = String.valueOf(cell.getBooleanCellValue()); | ||
| 47 | - } | ||
| 48 | - else if(cell.getCellType() == Cell.CELL_TYPE_ERROR) { | ||
| 49 | - cellValue = ""; | ||
| 50 | - } | ||
| 51 | - else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { | ||
| 52 | - cellValue = cell.getCellFormula().toString(); | ||
| 53 | - } | ||
| 54 | - return cellValue; | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - public static XSSFCell createStringXSSFCell( | ||
| 58 | - XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, String value, | ||
| 59 | - Color backgroundColor) { | ||
| 60 | - XSSFCell xssfCell = createXSSFCell( | ||
| 61 | - xssfWorkbook, xssfRow, column, | ||
| 62 | - value, XSSFCell.CELL_TYPE_STRING, | ||
| 63 | - HorizontalAlignment.CENTER, VerticalAlignment.CENTER, | ||
| 64 | - BorderStyle.MEDIUM, new Color(0xdedede), | ||
| 65 | - (short) 13, new Color(0x2765A7), "宋体", | ||
| 66 | - backgroundColor, FillPatternType.SOLID_FOREGROUND | ||
| 67 | - ); | ||
| 68 | - DataFormat dataFormat = xssfWorkbook.createDataFormat(); | ||
| 69 | - xssfCell.getCellStyle().setDataFormat(dataFormat.getFormat("@")); | ||
| 70 | - return xssfCell; | ||
| 71 | - } | ||
| 72 | - | ||
| 73 | - public static XSSFCell createStringXSSFCell( | ||
| 74 | - XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, String value) { | ||
| 75 | - XSSFCell xssfCell = createXSSFCell( | ||
| 76 | - xssfWorkbook, xssfRow, column, | ||
| 77 | - value, XSSFCell.CELL_TYPE_STRING, | ||
| 78 | - HorizontalAlignment.CENTER, VerticalAlignment.CENTER, | ||
| 79 | - BorderStyle.MEDIUM, new Color(0xdedede), | ||
| 80 | - (short) 13, new Color(0x2765A7), "宋体", | ||
| 81 | - new Color(0xffffff), FillPatternType.SOLID_FOREGROUND | ||
| 82 | - ); | ||
| 83 | - DataFormat dataFormat = xssfWorkbook.createDataFormat(); | ||
| 84 | - xssfCell.getCellStyle().setDataFormat(dataFormat.getFormat("@")); | ||
| 85 | - return xssfCell; | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | - public static XSSFCell createDoubleXSSFCell( | ||
| 89 | - XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, Double value) { | ||
| 90 | - XSSFCell xssfCell = createXSSFCell( | ||
| 91 | - xssfWorkbook, xssfRow, column, | ||
| 92 | - value, XSSFCell.CELL_TYPE_NUMERIC, | ||
| 93 | - HorizontalAlignment.CENTER, VerticalAlignment.CENTER, | ||
| 94 | - BorderStyle.MEDIUM, new Color(0xdedede), | ||
| 95 | - (short) 13, new Color(0x2765A7), "宋体", | ||
| 96 | - new Color(0xffffff), FillPatternType.SOLID_FOREGROUND | ||
| 97 | - ); | ||
| 98 | - | ||
| 99 | - DataFormat dataFormat = xssfWorkbook.createDataFormat(); | ||
| 100 | - xssfCell.getCellStyle().setDataFormat(dataFormat.getFormat("0.00")); | ||
| 101 | - return xssfCell; | ||
| 102 | - } | ||
| 103 | - | ||
| 104 | - public static XSSFCell createIntegerXSSFCell( | ||
| 105 | - XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, Integer value) { | ||
| 106 | - return createXSSFCell( | ||
| 107 | - xssfWorkbook, xssfRow, column, | ||
| 108 | - value, XSSFCell.CELL_TYPE_NUMERIC, | ||
| 109 | - HorizontalAlignment.CENTER, VerticalAlignment.CENTER, | ||
| 110 | - BorderStyle.MEDIUM, new Color(0xdedede), | ||
| 111 | - (short) 13, new Color(0x2765A7), "宋体", | ||
| 112 | - new Color(0xffffff), FillPatternType.SOLID_FOREGROUND | ||
| 113 | - ); | ||
| 114 | - } | ||
| 115 | - | ||
| 116 | - public static XSSFCell createBlankXSSFCell( | ||
| 117 | - XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column) { | ||
| 118 | - return createXSSFCell( | ||
| 119 | - xssfWorkbook, xssfRow, column, | ||
| 120 | - null, XSSFCell.CELL_TYPE_BLANK, | ||
| 121 | - HorizontalAlignment.CENTER, VerticalAlignment.CENTER, | ||
| 122 | - BorderStyle.MEDIUM, new Color(0xdedede), | ||
| 123 | - (short) 13, new Color(0x2765A7), "宋体", | ||
| 124 | - new Color(0xffffff), FillPatternType.SOLID_FOREGROUND | ||
| 125 | - ); | ||
| 126 | - } | ||
| 127 | - | ||
| 128 | - public static XSSFCell setStringStyleXSSFCellStyle(XSSFWorkbook xssfWorkbook, XSSFCell xssfCell) { | ||
| 129 | - DataFormat dataFormat = xssfWorkbook.createDataFormat(); | ||
| 130 | - xssfCell.getCellStyle().setDataFormat(dataFormat.getFormat("@")); | ||
| 131 | - return xssfCell; | ||
| 132 | - } | ||
| 133 | - | ||
| 134 | - public static XSSFCell setIntegerStyleXSSFCellStyle(XSSFWorkbook xssfWorkbook, XSSFCell xssfCell) { | ||
| 135 | - CreationHelper creationHelper = xssfWorkbook.getCreationHelper(); | ||
| 136 | - XSSFCellStyle xssfCellStyle = xssfCell.getCellStyle(); | ||
| 137 | - xssfCellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0")); | ||
| 138 | - return xssfCell; | ||
| 139 | - } | ||
| 140 | - public static XSSFCell setDoubleStyleXSSFCellStyle(XSSFWorkbook xssfWorkbook, XSSFCell xssfCell) { | ||
| 141 | - CreationHelper creationHelper = xssfWorkbook.getCreationHelper(); | ||
| 142 | - XSSFCellStyle xssfCellStyle = xssfCell.getCellStyle(); | ||
| 143 | - xssfCellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0.00")); | ||
| 144 | - return xssfCell; | ||
| 145 | - } | ||
| 146 | - | ||
| 147 | - public static XSSFCell createXSSFCell( | ||
| 148 | - XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, | ||
| 149 | - Object value, int valueType, | ||
| 150 | - HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, | ||
| 151 | - BorderStyle borderStyle, Color borderColor, | ||
| 152 | - short fontSize, Color fontColor, String fontName, | ||
| 153 | - Color backgroudColor, FillPatternType fillPatternType) { | ||
| 154 | - CreationHelper creationHelper = xssfWorkbook.getCreationHelper(); | ||
| 155 | - | ||
| 156 | - // 1、创建单元格对象 | ||
| 157 | - XSSFCell cell = xssfRow.createCell(column); | ||
| 158 | - | ||
| 159 | - // 2、设定样式 | ||
| 160 | - XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle(); | ||
| 161 | - | ||
| 162 | - // 设定值 | ||
| 163 | - if (valueType == XSSFCell.CELL_TYPE_STRING) { | ||
| 164 | -// cell.setCellValue(creationHelper.createRichTextString( | ||
| 165 | -// WorkbookUtil.createSafeSheetName(String.valueOf(value)))); | ||
| 166 | - cell.setCellValue(creationHelper.createRichTextString(String.valueOf(value))); | ||
| 167 | - } else if (valueType == XSSFCell.CELL_TYPE_NUMERIC) { | ||
| 168 | - if (value instanceof Date) { // 日期 | ||
| 169 | - cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-mm-dd")); | ||
| 170 | - cell.setCellValue((Date) value); | ||
| 171 | - } else if (value instanceof Double) { | ||
| 172 | - cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0.00")); | ||
| 173 | - cell.setCellValue((Double) value); | ||
| 174 | - } else if (value instanceof Integer) { | ||
| 175 | - cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0")); | ||
| 176 | - cell.setCellValue(Double.valueOf(value.toString())); | ||
| 177 | - } else { | ||
| 178 | - throw new RuntimeException("只支持 Date Double Integer 单元格类型"); | ||
| 179 | - } | ||
| 180 | - } else if (valueType == XSSFCell.CELL_TYPE_BLANK) { | ||
| 181 | - cell.setCellType(Cell.CELL_TYPE_BLANK); | ||
| 182 | - } | ||
| 183 | - | ||
| 184 | - else { | ||
| 185 | - throw new RuntimeException("暂时不支持字符串、日期、数字以为的类型"); | ||
| 186 | - } | ||
| 187 | - | ||
| 188 | - // 对齐方式 | ||
| 189 | - cellStyle.setAlignment(horizontalAlignment); | ||
| 190 | - cellStyle.setVerticalAlignment(verticalAlignment); | ||
| 191 | - | ||
| 192 | - // 边框样式 | ||
| 193 | - cellStyle.setBorderTop(borderStyle); | ||
| 194 | - cellStyle.setTopBorderColor(new XSSFColor(borderColor)); | ||
| 195 | - cellStyle.setBorderBottom(borderStyle); | ||
| 196 | - cellStyle.setBottomBorderColor(new XSSFColor(borderColor)); | ||
| 197 | - cellStyle.setBorderLeft(borderStyle); | ||
| 198 | - cellStyle.setLeftBorderColor(new XSSFColor(borderColor)); | ||
| 199 | - cellStyle.setBorderRight(borderStyle); | ||
| 200 | - cellStyle.setRightBorderColor(new XSSFColor(borderColor)); | ||
| 201 | - | ||
| 202 | - // 字体颜色 | ||
| 203 | - XSSFFont font = xssfWorkbook.createFont(); | ||
| 204 | - font.setColor(new XSSFColor(fontColor)); | ||
| 205 | - font.setFontHeightInPoints(fontSize); | ||
| 206 | - font.setFontName(fontName); | ||
| 207 | - cellStyle.setFont(font); | ||
| 208 | - | ||
| 209 | - | ||
| 210 | - // 单元背景色 | ||
| 211 | - cellStyle.setFillForegroundColor(new XSSFColor(backgroudColor)); | ||
| 212 | - cellStyle.setFillPattern(fillPatternType); | ||
| 213 | - | ||
| 214 | - // TODO | ||
| 215 | - | ||
| 216 | - cell.setCellStyle(cellStyle); | ||
| 217 | - return cell; | ||
| 218 | - } | ||
| 219 | - | ||
| 220 | -} | ||
| 221 | - | 1 | +package com.bsth.service.schedule.utils; |
| 2 | + | ||
| 3 | +import com.bsth.entity.Energy; | ||
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 5 | +import org.apache.poi.hssf.usermodel.*; | ||
| 6 | +import org.apache.poi.poifs.filesystem.POIFSFileSystem; | ||
| 7 | +import org.apache.poi.ss.usermodel.*; | ||
| 8 | +import org.apache.poi.xssf.usermodel.*; | ||
| 9 | + | ||
| 10 | +import java.awt.Color; | ||
| 11 | +import java.io.File; | ||
| 12 | +import java.io.FileInputStream; | ||
| 13 | +import java.io.IOException; | ||
| 14 | +import java.lang.reflect.Field; | ||
| 15 | +import java.text.DecimalFormat; | ||
| 16 | +import java.text.SimpleDateFormat; | ||
| 17 | +import java.util.*; | ||
| 18 | + | ||
| 19 | +/** | ||
| 20 | + * POI操作的until方法。 | ||
| 21 | + */ | ||
| 22 | +public class PoiUtils { | ||
| 23 | + | ||
| 24 | + private static ObjectMapper mapper = new ObjectMapper(); | ||
| 25 | + | ||
| 26 | + /** | ||
| 27 | + * 使用poi读取cell中的值 | ||
| 28 | + * @param cell | ||
| 29 | + * @return | ||
| 30 | + */ | ||
| 31 | + public static String getStringValueFromCell(Cell cell) { | ||
| 32 | + SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd"); | ||
| 33 | + DecimalFormat decimalFormat = new DecimalFormat("#.#"); | ||
| 34 | + String cellValue = ""; | ||
| 35 | + if(cell == null) { | ||
| 36 | + return cellValue; | ||
| 37 | + } | ||
| 38 | + else if(cell.getCellType() == Cell.CELL_TYPE_STRING) { | ||
| 39 | + cellValue = cell.getStringCellValue(); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) { | ||
| 43 | + if(HSSFDateUtil.isCellDateFormatted(cell)) { | ||
| 44 | + double d = cell.getNumericCellValue(); | ||
| 45 | + Date date = HSSFDateUtil.getJavaDate(d); | ||
| 46 | + cellValue = sFormat.format(date); | ||
| 47 | + } | ||
| 48 | + else { | ||
| 49 | + cellValue = decimalFormat.format((cell.getNumericCellValue())); | ||
| 50 | + } | ||
| 51 | + } | ||
| 52 | + else if(cell.getCellType() == Cell.CELL_TYPE_BLANK) { | ||
| 53 | + cellValue = ""; | ||
| 54 | + } | ||
| 55 | + else if(cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { | ||
| 56 | + cellValue = String.valueOf(cell.getBooleanCellValue()); | ||
| 57 | + } | ||
| 58 | + else if(cell.getCellType() == Cell.CELL_TYPE_ERROR) { | ||
| 59 | + cellValue = ""; | ||
| 60 | + } | ||
| 61 | + else if(cell.getCellType() == Cell.CELL_TYPE_FORMULA) { | ||
| 62 | + cellValue = cell.getCellFormula().toString(); | ||
| 63 | + } | ||
| 64 | + return cellValue; | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + public static XSSFCell createStringXSSFCell( | ||
| 68 | + XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, String value, | ||
| 69 | + Color backgroundColor) { | ||
| 70 | + XSSFCell xssfCell = createXSSFCell( | ||
| 71 | + xssfWorkbook, xssfRow, column, | ||
| 72 | + value, XSSFCell.CELL_TYPE_STRING, | ||
| 73 | + HorizontalAlignment.CENTER, VerticalAlignment.CENTER, | ||
| 74 | + BorderStyle.MEDIUM, new Color(0xdedede), | ||
| 75 | + (short) 13, new Color(0x2765A7), "宋体", | ||
| 76 | + backgroundColor, FillPatternType.SOLID_FOREGROUND | ||
| 77 | + ); | ||
| 78 | + DataFormat dataFormat = xssfWorkbook.createDataFormat(); | ||
| 79 | + xssfCell.getCellStyle().setDataFormat(dataFormat.getFormat("@")); | ||
| 80 | + return xssfCell; | ||
| 81 | + } | ||
| 82 | + | ||
| 83 | + public static XSSFCell createStringXSSFCell( | ||
| 84 | + XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, String value) { | ||
| 85 | + XSSFCell xssfCell = createXSSFCell( | ||
| 86 | + xssfWorkbook, xssfRow, column, | ||
| 87 | + value, XSSFCell.CELL_TYPE_STRING, | ||
| 88 | + HorizontalAlignment.CENTER, VerticalAlignment.CENTER, | ||
| 89 | + BorderStyle.MEDIUM, new Color(0xdedede), | ||
| 90 | + (short) 13, new Color(0x2765A7), "宋体", | ||
| 91 | + new Color(0xffffff), FillPatternType.SOLID_FOREGROUND | ||
| 92 | + ); | ||
| 93 | + DataFormat dataFormat = xssfWorkbook.createDataFormat(); | ||
| 94 | + xssfCell.getCellStyle().setDataFormat(dataFormat.getFormat("@")); | ||
| 95 | + return xssfCell; | ||
| 96 | + } | ||
| 97 | + | ||
| 98 | + public static XSSFCell createDoubleXSSFCell( | ||
| 99 | + XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, Double value) { | ||
| 100 | + XSSFCell xssfCell = createXSSFCell( | ||
| 101 | + xssfWorkbook, xssfRow, column, | ||
| 102 | + value, XSSFCell.CELL_TYPE_NUMERIC, | ||
| 103 | + HorizontalAlignment.CENTER, VerticalAlignment.CENTER, | ||
| 104 | + BorderStyle.MEDIUM, new Color(0xdedede), | ||
| 105 | + (short) 13, new Color(0x2765A7), "宋体", | ||
| 106 | + new Color(0xffffff), FillPatternType.SOLID_FOREGROUND | ||
| 107 | + ); | ||
| 108 | + | ||
| 109 | + DataFormat dataFormat = xssfWorkbook.createDataFormat(); | ||
| 110 | + xssfCell.getCellStyle().setDataFormat(dataFormat.getFormat("0.00")); | ||
| 111 | + return xssfCell; | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + public static XSSFCell createIntegerXSSFCell( | ||
| 115 | + XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, Integer value) { | ||
| 116 | + return createXSSFCell( | ||
| 117 | + xssfWorkbook, xssfRow, column, | ||
| 118 | + value, XSSFCell.CELL_TYPE_NUMERIC, | ||
| 119 | + HorizontalAlignment.CENTER, VerticalAlignment.CENTER, | ||
| 120 | + BorderStyle.MEDIUM, new Color(0xdedede), | ||
| 121 | + (short) 13, new Color(0x2765A7), "宋体", | ||
| 122 | + new Color(0xffffff), FillPatternType.SOLID_FOREGROUND | ||
| 123 | + ); | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + public static XSSFCell createBlankXSSFCell( | ||
| 127 | + XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column) { | ||
| 128 | + return createXSSFCell( | ||
| 129 | + xssfWorkbook, xssfRow, column, | ||
| 130 | + null, XSSFCell.CELL_TYPE_BLANK, | ||
| 131 | + HorizontalAlignment.CENTER, VerticalAlignment.CENTER, | ||
| 132 | + BorderStyle.MEDIUM, new Color(0xdedede), | ||
| 133 | + (short) 13, new Color(0x2765A7), "宋体", | ||
| 134 | + new Color(0xffffff), FillPatternType.SOLID_FOREGROUND | ||
| 135 | + ); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + public static XSSFCell setStringStyleXSSFCellStyle(XSSFWorkbook xssfWorkbook, XSSFCell xssfCell) { | ||
| 139 | + DataFormat dataFormat = xssfWorkbook.createDataFormat(); | ||
| 140 | + xssfCell.getCellStyle().setDataFormat(dataFormat.getFormat("@")); | ||
| 141 | + return xssfCell; | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + public static XSSFCell setIntegerStyleXSSFCellStyle(XSSFWorkbook xssfWorkbook, XSSFCell xssfCell) { | ||
| 145 | + CreationHelper creationHelper = xssfWorkbook.getCreationHelper(); | ||
| 146 | + XSSFCellStyle xssfCellStyle = xssfCell.getCellStyle(); | ||
| 147 | + xssfCellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0")); | ||
| 148 | + return xssfCell; | ||
| 149 | + } | ||
| 150 | + public static XSSFCell setDoubleStyleXSSFCellStyle(XSSFWorkbook xssfWorkbook, XSSFCell xssfCell) { | ||
| 151 | + CreationHelper creationHelper = xssfWorkbook.getCreationHelper(); | ||
| 152 | + XSSFCellStyle xssfCellStyle = xssfCell.getCellStyle(); | ||
| 153 | + xssfCellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0.00")); | ||
| 154 | + return xssfCell; | ||
| 155 | + } | ||
| 156 | + | ||
| 157 | + public static XSSFCell createXSSFCell( | ||
| 158 | + XSSFWorkbook xssfWorkbook, XSSFRow xssfRow, short column, | ||
| 159 | + Object value, int valueType, | ||
| 160 | + HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, | ||
| 161 | + BorderStyle borderStyle, Color borderColor, | ||
| 162 | + short fontSize, Color fontColor, String fontName, | ||
| 163 | + Color backgroudColor, FillPatternType fillPatternType) { | ||
| 164 | + CreationHelper creationHelper = xssfWorkbook.getCreationHelper(); | ||
| 165 | + | ||
| 166 | + // 1、创建单元格对象 | ||
| 167 | + XSSFCell cell = xssfRow.createCell(column); | ||
| 168 | + | ||
| 169 | + // 2、设定样式 | ||
| 170 | + XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle(); | ||
| 171 | + | ||
| 172 | + // 设定值 | ||
| 173 | + if (valueType == XSSFCell.CELL_TYPE_STRING) { | ||
| 174 | +// cell.setCellValue(creationHelper.createRichTextString( | ||
| 175 | +// WorkbookUtil.createSafeSheetName(String.valueOf(value)))); | ||
| 176 | + cell.setCellValue(creationHelper.createRichTextString(String.valueOf(value))); | ||
| 177 | + } else if (valueType == XSSFCell.CELL_TYPE_NUMERIC) { | ||
| 178 | + if (value instanceof Date) { // 日期 | ||
| 179 | + cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-mm-dd")); | ||
| 180 | + cell.setCellValue((Date) value); | ||
| 181 | + } else if (value instanceof Double) { | ||
| 182 | + cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0.00")); | ||
| 183 | + cell.setCellValue((Double) value); | ||
| 184 | + } else if (value instanceof Integer) { | ||
| 185 | + cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("0")); | ||
| 186 | + cell.setCellValue(Double.valueOf(value.toString())); | ||
| 187 | + } else { | ||
| 188 | + throw new RuntimeException("只支持 Date Double Integer 单元格类型"); | ||
| 189 | + } | ||
| 190 | + } else if (valueType == XSSFCell.CELL_TYPE_BLANK) { | ||
| 191 | + cell.setCellType(Cell.CELL_TYPE_BLANK); | ||
| 192 | + } | ||
| 193 | + | ||
| 194 | + else { | ||
| 195 | + throw new RuntimeException("暂时不支持字符串、日期、数字以为的类型"); | ||
| 196 | + } | ||
| 197 | + | ||
| 198 | + // 对齐方式 | ||
| 199 | + cellStyle.setAlignment(horizontalAlignment); | ||
| 200 | + cellStyle.setVerticalAlignment(verticalAlignment); | ||
| 201 | + | ||
| 202 | + // 边框样式 | ||
| 203 | + cellStyle.setBorderTop(borderStyle); | ||
| 204 | + cellStyle.setTopBorderColor(new XSSFColor(borderColor)); | ||
| 205 | + cellStyle.setBorderBottom(borderStyle); | ||
| 206 | + cellStyle.setBottomBorderColor(new XSSFColor(borderColor)); | ||
| 207 | + cellStyle.setBorderLeft(borderStyle); | ||
| 208 | + cellStyle.setLeftBorderColor(new XSSFColor(borderColor)); | ||
| 209 | + cellStyle.setBorderRight(borderStyle); | ||
| 210 | + cellStyle.setRightBorderColor(new XSSFColor(borderColor)); | ||
| 211 | + | ||
| 212 | + // 字体颜色 | ||
| 213 | + XSSFFont font = xssfWorkbook.createFont(); | ||
| 214 | + font.setColor(new XSSFColor(fontColor)); | ||
| 215 | + font.setFontHeightInPoints(fontSize); | ||
| 216 | + font.setFontName(fontName); | ||
| 217 | + cellStyle.setFont(font); | ||
| 218 | + | ||
| 219 | + | ||
| 220 | + // 单元背景色 | ||
| 221 | + cellStyle.setFillForegroundColor(new XSSFColor(backgroudColor)); | ||
| 222 | + cellStyle.setFillPattern(fillPatternType); | ||
| 223 | + | ||
| 224 | + // TODO | ||
| 225 | + | ||
| 226 | + cell.setCellStyle(cellStyle); | ||
| 227 | + return cell; | ||
| 228 | + } | ||
| 229 | + | ||
| 230 | + /** | ||
| 231 | + * 导入数据 | ||
| 232 | + * @param file 文件 | ||
| 233 | + * @param skipRows 跳过行数 | ||
| 234 | + * @param columns 对应的字段列信息 | ||
| 235 | + * @param cls 对象类型class | ||
| 236 | + * @param <T> 对象类型 | ||
| 237 | + * @return | ||
| 238 | + */ | ||
| 239 | + public static <T> List<T> importFromExcel(File file, int skipRows, String[] columns, Class<T> cls) { | ||
| 240 | + List<T> list = new ArrayList<>(); | ||
| 241 | + try { | ||
| 242 | + POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file)); | ||
| 243 | + HSSFWorkbook wb = new HSSFWorkbook(fs); | ||
| 244 | + HSSFFormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); | ||
| 245 | + HSSFSheet sheet = wb.getSheetAt(0); | ||
| 246 | + int rows = sheet.getLastRowNum() + 1; | ||
| 247 | + | ||
| 248 | + List<Map<String, Object>> data = new ArrayList<>(); | ||
| 249 | + for (int i = skipRows;i < rows;i++) { | ||
| 250 | + HSSFRow row = sheet.getRow(i); | ||
| 251 | + Map<String, Object> map = new HashMap<>(); | ||
| 252 | + int cols = row.getLastCellNum(); | ||
| 253 | + if (columns.length != cols) { | ||
| 254 | + throw new IllegalArgumentException("异常的数据列数"); | ||
| 255 | + } | ||
| 256 | + | ||
| 257 | + for (int j = 0;j < cols;j++) { | ||
| 258 | + String column = columns[j]; | ||
| 259 | + if (column != null) { | ||
| 260 | + map.put(columns[j], getCellValue(evaluator, row.getCell(j))); | ||
| 261 | + } | ||
| 262 | + } | ||
| 263 | + data.add(map); | ||
| 264 | + } | ||
| 265 | + list = mapper.convertValue(data, mapper.getTypeFactory().constructParametrizedType(ArrayList.class, List.class, cls)); | ||
| 266 | + } catch (IOException e) { | ||
| 267 | + e.printStackTrace(); | ||
| 268 | + } | ||
| 269 | + | ||
| 270 | + return list; | ||
| 271 | + } | ||
| 272 | + | ||
| 273 | + private static Object getCellValue(FormulaEvaluator evaluator, HSSFCell cell) { | ||
| 274 | + Object value = null; | ||
| 275 | + if (cell != null) { | ||
| 276 | + CellValue cellValue = evaluator.evaluate(cell); | ||
| 277 | + switch (cellValue.getCellType()) { | ||
| 278 | + case Cell.CELL_TYPE_NUMERIC: | ||
| 279 | + value = cellValue.getNumberValue(); | ||
| 280 | + if (HSSFDateUtil.isCellDateFormatted(cell)) { | ||
| 281 | + value = cell.getDateCellValue(); | ||
| 282 | + } | ||
| 283 | + break; | ||
| 284 | + case Cell.CELL_TYPE_STRING: | ||
| 285 | + value = cellValue.getStringValue(); | ||
| 286 | + break; | ||
| 287 | + case Cell.CELL_TYPE_BOOLEAN: | ||
| 288 | + value = cellValue.getBooleanValue(); | ||
| 289 | + break; | ||
| 290 | + case Cell.CELL_TYPE_BLANK: | ||
| 291 | + case Cell.CELL_TYPE_ERROR: | ||
| 292 | + value = ""; | ||
| 293 | + break; | ||
| 294 | + default: | ||
| 295 | + break; | ||
| 296 | + } | ||
| 297 | + } | ||
| 298 | + | ||
| 299 | + return value; | ||
| 300 | + } | ||
| 301 | + | ||
| 302 | + public static void main(String[] args) { | ||
| 303 | + /*for (Field field : Energy.class.getDeclaredFields()) { | ||
| 304 | + System.out.print(String.format("\"%s\",",field.getName())); | ||
| 305 | + }*/ | ||
| 306 | + String[] columns = new String[]{null,"energyStation",null,null,"unit","customer",null,"plate",null,"startDate","terminalDate","chargingMinutes","energy1","energy2","energy3","energy4","originEnergy","energyCost","serviceCost","totalCost"}; | ||
| 307 | + List<Energy> energies = importFromExcel(new File("C:\\Users\\Hill\\Downloads\\充电数据.xls"), 2, columns, Energy.class); | ||
| 308 | + System.out.println(energies); | ||
| 309 | + } | ||
| 310 | +} | ||
| 311 | + |