|
@@ -1,14 +1,11 @@
|
|
|
package cn.com.ctop.common.module.utils;
|
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
|
|
-import org.apache.poi.ss.usermodel.BorderStyle;
|
|
|
-import org.apache.poi.ss.usermodel.Cell;
|
|
|
-import org.apache.poi.ss.usermodel.FillPatternType;
|
|
|
-import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
|
-import org.apache.poi.ss.usermodel.IndexedColors;
|
|
|
-import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFCell;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFFont;
|
|
@@ -16,6 +13,7 @@ import org.apache.poi.xssf.usermodel.XSSFRichTextString;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+import org.bytedeco.javacpp.presets.opencv_core;
|
|
|
import org.jeecg.common.util.DateUtils;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
@@ -111,6 +109,182 @@ public class ExportExcelUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void exportExcelBudget(XSSFWorkbook workbook, int sheetNum, String sheetTitle, String[] headers, List<List<Object>> result) {
|
|
|
+ XSSFSheet sheet = workbook.createSheet();
|
|
|
+ workbook.setSheetName(sheetNum, sheetTitle);
|
|
|
+ // 设置表格默认列宽度为20个字节
|
|
|
+ sheet.setDefaultColumnWidth((short) 20);
|
|
|
+ // 生成一个样式
|
|
|
+ XSSFCellStyle style = workbook.createCellStyle();
|
|
|
+ // 设置这些样式
|
|
|
+ style.setFillForegroundColor(IndexedColors.PALE_BLUE.index);
|
|
|
+ style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
+ style.setBorderBottom(BorderStyle.THIN);
|
|
|
+ style.setBorderLeft(BorderStyle.THIN);
|
|
|
+ style.setBorderRight(BorderStyle.THIN);
|
|
|
+ style.setBorderTop(BorderStyle.THIN);
|
|
|
+ // style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ style.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
|
|
|
+ // 生成一个字体
|
|
|
+ XSSFFont font = workbook.createFont();
|
|
|
+ font.setColor(IndexedColors.BLACK.index);
|
|
|
+ font.setFontHeightInPoints((short) 12);
|
|
|
+ font.setBold(true);
|
|
|
+ // 把字体应用到当前的样式
|
|
|
+ style.setFont(font);
|
|
|
+
|
|
|
+ // 指定当单元格内容显示不下时自动换行
|
|
|
+ style.setWrapText(true);
|
|
|
+
|
|
|
+ // 绿色字体
|
|
|
+ CellStyle greenStyle = workbook.createCellStyle();
|
|
|
+ Font greenFont = workbook.createFont();
|
|
|
+ greenFont.setColor(IndexedColors.GREEN.getIndex());
|
|
|
+ greenStyle.setFont(greenFont);
|
|
|
+
|
|
|
+ // 红色字体
|
|
|
+ CellStyle redStyle = workbook.createCellStyle();
|
|
|
+ Font redFont = workbook.createFont();
|
|
|
+ redFont.setColor(IndexedColors.RED.getIndex());
|
|
|
+ redStyle.setFont(redFont);
|
|
|
+
|
|
|
+
|
|
|
+ // 产生表格标题行
|
|
|
+ XSSFRow row = sheet.createRow(0);
|
|
|
+ for (int i = 0; i < headers.length; i++) {
|
|
|
+ XSSFCell cell = row.createCell((short) i);
|
|
|
+ cell.setCellStyle(style);
|
|
|
+ XSSFRichTextString text = new XSSFRichTextString(headers[i]);
|
|
|
+ cell.setCellValue(text.toString());
|
|
|
+ }
|
|
|
+ // 遍历集合数据,产生数据行
|
|
|
+ if (result != null) {
|
|
|
+ int index = 1;
|
|
|
+ for (List<Object> m : result) {
|
|
|
+ row = sheet.createRow(index);
|
|
|
+ int cellIndex = 0;
|
|
|
+ for (Object obj : m) {
|
|
|
+ XSSFCell cell = row.createCell((short) cellIndex);
|
|
|
+ if (!Check.isNull(obj)) {
|
|
|
+ String s = String.valueOf(obj);
|
|
|
+
|
|
|
+ if (s.contains("&br;") || s.contains(":red") || s.contains(":green")) {
|
|
|
+ if (s.contains("&br;")) {
|
|
|
+ JSONObject data = getData(s);
|
|
|
+ if (!Check.isNull(data)) {
|
|
|
+ cell.setCellValue(data.getString("data"));
|
|
|
+ JSONArray dataArr = data.getJSONArray("array");
|
|
|
+ if (!Check.isNull(dataArr)) {
|
|
|
+ for (int i = 0; i < dataArr.size(); i++) {
|
|
|
+ JSONObject dataJson = dataArr.getJSONObject(i);
|
|
|
+ String color = dataJson.getString("color");
|
|
|
+ if (color.equals("red")) {
|
|
|
+ cell.getRichStringCellValue().applyFont(dataJson.getInteger("start"), dataJson.getInteger("end"), redFont);
|
|
|
+ } else if (color.equals("green")) {
|
|
|
+ cell.getRichStringCellValue().applyFont(dataJson.getInteger("start"), dataJson.getInteger("end"), greenFont);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ cell.setCellValue(0);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (s.contains(":red")) {
|
|
|
+ String value = s.replace(":red", "");
|
|
|
+ cell.setCellValue(value);
|
|
|
+ int i = value.lastIndexOf("↑");
|
|
|
+ cell.getRichStringCellValue().applyFont(i, value.length(), redFont);
|
|
|
+ } else if (s.contains(":green")) {
|
|
|
+ String value = s.replace(":green", "");
|
|
|
+ cell.setCellValue(value);
|
|
|
+ int i = value.lastIndexOf("↓");
|
|
|
+ cell.getRichStringCellValue().applyFont(i, value.length(), greenFont);
|
|
|
+ } else {
|
|
|
+ cell.setCellValue(0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+ cell.setCellValue(s);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ cell.setCellValue(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ cellIndex++;
|
|
|
+ }
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ String a = null;
|
|
|
+
|
|
|
+ System.err.println(a.length());
|
|
|
+ }
|
|
|
+
|
|
|
+ private static JSONObject getData(String str) {
|
|
|
+ JSONObject returnJson = new JSONObject();
|
|
|
+ JSONArray array = new JSONArray();
|
|
|
+ String[] split = str.split("&br;");
|
|
|
+ if (split.length == 1) {
|
|
|
+ String data = split[0];
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ if (data.contains(":hong")) {
|
|
|
+ jsonObject.put("color", "red");
|
|
|
+ data = data.replace(":hong", "");
|
|
|
+ int startIndex = data.indexOf("↑");
|
|
|
+ jsonObject.put("start", startIndex);
|
|
|
+ jsonObject.put("end", data.length());
|
|
|
+ } else if (data.contains(":lv")) {
|
|
|
+ jsonObject.put("color", "green");
|
|
|
+ data = data.replace(":lv", "");
|
|
|
+ int startIndex = data.indexOf("↓");
|
|
|
+ jsonObject.put("start", startIndex);
|
|
|
+ jsonObject.put("end", data.length());
|
|
|
+ }
|
|
|
+ array.add(jsonObject);
|
|
|
+ returnJson.put("data", data);
|
|
|
+ returnJson.put("array", array);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ String data = "";
|
|
|
+ Integer dataLength = 0;
|
|
|
+ for (int i = 0; i < split.length; i++) {
|
|
|
+ String value = split[i];
|
|
|
+ JSONObject valJson = new JSONObject();
|
|
|
+ if (value.contains(":hong")) {
|
|
|
+ valJson.put("color", "red");
|
|
|
+ String newValue = value.replace(":hong", "");
|
|
|
+ int startIndex = newValue.indexOf("↑");
|
|
|
+ valJson.put("start", dataLength + startIndex);
|
|
|
+ valJson.put("end", dataLength + newValue.length());
|
|
|
+ data += newValue;
|
|
|
+ dataLength += newValue.length();
|
|
|
+
|
|
|
+ } else if (value.contains(":lv")) {
|
|
|
+ valJson.put("color", "green");
|
|
|
+ String newValue = value.replace(":lv", "");
|
|
|
+ int startIndex = newValue.indexOf("↓");
|
|
|
+ valJson.put("start", dataLength + startIndex);
|
|
|
+ valJson.put("end", dataLength + newValue.length());
|
|
|
+ data += newValue;
|
|
|
+ dataLength += newValue.length();
|
|
|
+ }
|
|
|
+ array.add(valJson);
|
|
|
+
|
|
|
+ }
|
|
|
+ returnJson.put("array", array);
|
|
|
+ returnJson.put("data", data);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return returnJson;
|
|
|
+ }
|
|
|
|
|
|
public void exportExcelForAttendance(XSSFWorkbook workbook, int sheetNum, String sheetTitle, String[] headers, List<List<Object>> result) {
|
|
|
// 生成一个表格
|
|
@@ -360,7 +534,7 @@ public class ExportExcelUtils {
|
|
|
value = "";
|
|
|
}
|
|
|
} else {
|
|
|
- value = new DecimalFormat("0.00").format(cell.getNumericCellValue()).replace(".00","");
|
|
|
+ value = new DecimalFormat("0.00").format(cell.getNumericCellValue()).replace(".00", "");
|
|
|
}
|
|
|
break;
|
|
|
case HSSFCell.CELL_TYPE_STRING: // 字符串
|