参考:Luckysheet 导入导出 - Java后台处理和js前端实现_u014632228的博客-CSDN博客_luckysheet导出前言:之前看了好多导出方法,导出的excel表格列宽变大,行高变高等问题。后面单个去调整,目前导出的excel是和导入的excel能够保持样式列宽行高一样。不过没有将计算公式带入到导出。
上代码: poi 是3.17 版本
excelData 是前端传的luckysheet报表json数据:luckySheet.getAllSheets();
package com.ts.comm.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
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.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class LuckySheetExportUtil {
public static void exportLuckySheetXlsxByPOI(HttpServletResponse response, String excelData) {
excelData = excelData.replace("
", "\r\n");//去除luckysheet中
的换行
JSonArray jsonArray = (JSONArray) JSONObject.parse(excelData);
XSSFWorkbook excel = new XSSFWorkbook();
ServletOutputStream outputStream = null;
for (int sheetIndex = 0; sheetIndex < jsonArray.size(); sheetIndex++) {
JSonObject jsonObject = (JSONObject) jsonArray.get(sheetIndex);
JSonArray celldataObjectList = jsonObject.getJSonArray("celldata");
JSonArray rowObjectList = jsonObject.getJSonArray("visibledatarow");
JSonArray colObjectList = jsonObject.getJSonArray("visibledatacolumn");
JSonArray dataObjectList = jsonObject.getJSonArray("data");
JSonObject mergeObject = jsonObject.getJSonObject("config").getJSonObject("merge");//合并单元格
JSonObject columnlenObject = jsonObject.getJSonObject("config").getJSonObject("columnlen");//表格列宽
JSonObject rowlenObject = jsonObject.getJSonObject("config").getJSonObject("rowlen");//表格行高
JSonArray borderInfoObjectList = jsonObject.getJSonObject("config").getJSonArray("borderInfo");//边框样式
//参考:https://blog.csdn.net/jdtugfcg/article/details/84100315
//创建操作Excel的XSSFWorkbook对象
XSSFCellStyle cellStyle = excel.createCellStyle();
//创建XSSFSheet对象
XSSFSheet sheet = excel.createSheet(jsonObject.getString("name"));
//我们都知道excel是表格,即由一行一行组成的,那么这一行在java类中就是一个XSSFRow对象,我们通过XSSFSheet对象就可以创建XSSFRow对象
//如:创建表格中的第一行(我们常用来做标题的行) XSSFRow firstRow = sheet.createRow(0); 注意下标从0开始
//根据luckysheet创建行列
//创建行和列
int rowSize = rowObjectList.size();
for (int i = 0; i < rowSize; i++) {
XSSFRow row = sheet.createRow(i);//创建行
try {
// row.setHeightInPoints(Float.parseFloat(rowlenObject.get(i) + ""));//行高px值 最原始写法
//luckysheet 行高 * 0.75 四舍五入取整后的值为excel导出的行高值
row.setHeightInPoints(Float.parseFloat(Math.round(rowlenObject.getInteger(i + "") * 0.75) + ""));//行高px值
} catch (Exception e) {
row.setHeightInPoints(20f);//默认行高
}
double sheetLen;
int colSize = colObjectList.size();
for (int j = 0; j < colSize; j++) {
if (columnlenObject.getInteger(j + "") != null) {
// sheet.setColumnWidth(j, columnlenObject.getInteger(j + "") * 34);//列宽px值 最原始写法
//luckysheet (宽度-3)/8 = excel的宽度 (int)(excel宽度+0.62)*256 = 导出表格实际列宽
//double sheetLen = (columnlenObject.getInteger(j + "") - 3) / 8;
sheetLen = (double) (columnlenObject.getInteger(j + "") - 3) / 8;
sheet.setColumnWidth(j, (int) ((sheetLen + 0.62) * 256));//列宽px值
}
row.createCell(j);//创建列
}
}
//设置值,样式
setCellValue(celldataObjectList, borderInfoObjectList, sheet, excel);
}
try {
outputStream = response.getOutputStream();
excel.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (outputStream != null) {
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void setMergeAndColorByObject(com.alibaba.fastjson.JSonObject jsonObjectValue, XSSFSheet sheet, XSSFCellStyle style) {
JSonObject mergeObject = (JSONObject) jsonObjectValue.get("mc");
if (mergeObject != null) {
int r = (int) (mergeObject.get("r"));
int c = (int) (mergeObject.get("c"));
if ((mergeObject.get("rs") != null && (mergeObject.get("cs") != null))) {
int rs = (int) (mergeObject.get("rs"));
int cs = (int) (mergeObject.get("cs"));
CellRangeAddress region = new CellRangeAddress(r, r + rs - 1, (short) (c), (short) (c + cs - 1));
sheet.addMergedRegion(region);
}
}
if (jsonObjectValue.getString("bg") != null) {
int bg = Integer.parseInt(jsonObjectValue.getString("bg").replace("#", ""), 16);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND); //设置填充方案
style.setFillForegroundColor(new XSSFColor(new Color(bg))); //设置填充颜色
}
}
private static void setBorder(JSonArray borderInfoObjectList, XSSFWorkbook workbook, XSSFSheet sheet) {
//设置边框样式map
Map bordMap = new HashMap<>();
bordMap.put(1, BorderStyle.THIN);
bordMap.put(2, BorderStyle.HAIR);
bordMap.put(3, BorderStyle.DOTTED);
bordMap.put(4, BorderStyle.DASHED);
bordMap.put(5, BorderStyle.DASH_DOT);
bordMap.put(6, BorderStyle.DASH_DOT_DOT);
bordMap.put(7, BorderStyle.DOUBLE);
bordMap.put(8, BorderStyle.MEDIUM);
bordMap.put(9, BorderStyle.MEDIUM_DASHED);
bordMap.put(10, BorderStyle.MEDIUM_DASH_DOT);
bordMap.put(11, BorderStyle.MEDIUM_DASH_DOT_DOT);
bordMap.put(12, BorderStyle.SLANTED_DASH_DOT);
bordMap.put(13, BorderStyle.THICK);
//一定要通过 cell.getCellStyle() 不然的话之前设置的样式会丢失
//设置边框
for (int i = 0; i < borderInfoObjectList.size(); i++) {
JSonObject borderInfoObject = (JSONObject) borderInfoObjectList.get(i);
if (borderInfoObject.get("rangeType").equals("cell")) {//单个单元格
JSonObject borderValueObject = borderInfoObject.getJSonObject("value");
JSonObject l = borderValueObject.getJSonObject("l");
JSonObject r = borderValueObject.getJSonObject("r");
JSonObject t = borderValueObject.getJSonObject("t");
JSonObject b = borderValueObject.getJSonObject("b");
int row = borderValueObject.getInteger("row_index");
int col = borderValueObject.getInteger("col_index");
XSSFCell cell = sheet.getRow(row).getCell(col);
if (l != null) {
cell.getCellStyle().setBorderLeft(bordMap.get((int) l.get("style"))); //左边框
int bg = Integer.parseInt(l.getString("color").replace("#", ""), 16);
cell.getCellStyle().setLeftBorderColor(new XSSFColor(new Color(bg)));//左边框颜色
}
if (r != null) {
cell.getCellStyle().setBorderRight(bordMap.get((int) r.get("style"))); //右边框
int bg = Integer.parseInt(r.getString("color").replace("#", ""), 16);
cell.getCellStyle().setRightBorderColor(new XSSFColor(new Color(bg)));//右边框颜色
}
if (t != null) {
cell.getCellStyle().setBorderTop(bordMap.get((int) t.get("style"))); //顶部边框
int bg = Integer.parseInt(t.getString("color").replace("#", ""), 16);
cell.getCellStyle().setTopBorderColor(new XSSFColor(new Color(bg)));//顶部边框颜色
}
if (b != null) {
cell.getCellStyle().setBorderBottom(bordMap.get((int) b.get("style"))); //底部边框
int bg = Integer.parseInt(b.getString("color").replace("#", ""), 16);
cell.getCellStyle().setBottomBorderColor(new XSSFColor(new Color(bg)));//底部边框颜色
}
} else if (borderInfoObject.get("rangeType").equals("range")) {//选区
int bg_ = Integer.parseInt(borderInfoObject.getString("color").replace("#", ""), 16);
int style_ = borderInfoObject.getInteger("style");
JSonObject rangObject = (JSONObject) ((JSONArray) (borderInfoObject.get("range"))).get(0);
JSonArray rowList = rangObject.getJSonArray("row");
JSonArray columnList = rangObject.getJSonArray("column");
for (int row_ = rowList.getInteger(0); row_ < rowList.getInteger(rowList.size() - 1) + 1; row_++) {
for (int col_ = columnList.getInteger(0); col_ < columnList.getInteger(columnList.size() - 1) + 1; col_++) {
XSSFCell cell = sheet.getRow(row_).getCell(col_);
cell.getCellStyle().setBorderLeft(bordMap.get(style_)); //左边框
cell.getCellStyle().setLeftBorderColor(new XSSFColor(new Color(bg_)));//左边框颜色
cell.getCellStyle().setBorderRight(bordMap.get(style_)); //右边框
cell.getCellStyle().setRightBorderColor(new XSSFColor(new Color(bg_)));//右边框颜色
cell.getCellStyle().setBorderTop(bordMap.get(style_)); //顶部边框
cell.getCellStyle().setTopBorderColor(new XSSFColor(new Color(bg_)));//顶部边框颜色
cell.getCellStyle().setBorderBottom(bordMap.get(style_)); //底部边框
cell.getCellStyle().setBottomBorderColor(new XSSFColor(new Color(bg_)));//底部边框颜色 }
}
}
}
}
}
private static void setCellValue(JSonArray jsonObjectList, JSonArray borderInfoObjectList, XSSFSheet
sheet, XSSFWorkbook workbook) {
//设置字体大小和颜色
Map fontMap = new HashMap<>();
fontMap.put(-1, "Arial");
fontMap.put(0, "Times New Roman");
fontMap.put(1, "Arial");
fontMap.put(2, "Tahoma");
fontMap.put(3, "Verdana");
fontMap.put(4, "微软雅黑");
fontMap.put(5, "宋体");
fontMap.put(6, "黑体");
fontMap.put(7, "楷体");
fontMap.put(8, "仿宋");
fontMap.put(9, "新宋体");
fontMap.put(10, "华文新魏");
fontMap.put(11, "华文行楷");
fontMap.put(12, "华文隶书");
for (int index = 0; index < jsonObjectList.size(); index++) {
XSSFCellStyle style = workbook.createCellStyle();//样式
XSSFFont font = workbook.createFont();//字体样式
com.alibaba.fastjson.JSonObject object = jsonObjectList.getJSonObject(index);
String str_ = (int) object.get("r") + "_" + object.get("c") + "=" + ((com.alibaba.fastjson.JSONObject) object.get("v")).get("v") + "n";
JSonObject jsonObjectValue = ((com.alibaba.fastjson.JSONObject) object.get("v"));
String value = "";
if (jsonObjectValue != null) {
//value = jsonObjectValue.getString("v");//v 值可能是公式值
value = jsonObjectValue.getString("m");//取m 显示值
if (value == null || "".equals(value)) {
try {
value = jsonObjectValue.getJSonObject("ct").getJSonArray("s").getJSonObject(0).getString("v");
} catch (Exception e) {
//异常不做处理。证明没有值
}
}
}
if (sheet.getRow((int) object.get("r")) != null && sheet.getRow((int) object.get("r")).getCell((int) object.get("c")) != null) {
XSSFCell cell = sheet.getRow((int) object.get("r")).getCell((int) object.get("c"));
// if (jsonObjectValue != null && jsonObjectValue.get("f") != null) {//如果有公式,设置公式
// value = jsonObjectValue.getString("f");
// cell.setCellFormula(value.substring(1, value.length()));//不需要=符号
// }
//合并单元格与填充单元格颜色
setMergeAndColorByObject(jsonObjectValue, sheet, style);
//填充值
cell.setCellValue(value);
XSSFRow row = sheet.getRow((int) object.get("r"));
//设置垂直水平对齐方式
int vt = jsonObjectValue.getInteger("vt") == null ? 1 : jsonObjectValue.getInteger("vt");//垂直对齐 0 中间、1 上、2下
int ht = jsonObjectValue.getInteger("ht") == null ? 1 : jsonObjectValue.getInteger("ht");//0 居中、1 左、2右
switch (vt) {
case 0:
style.setVerticalAlignment(VerticalAlignment.CENTER);
break;
case 1:
style.setVerticalAlignment(VerticalAlignment.TOP);
break;
case 2:
style.setVerticalAlignment(VerticalAlignment.BOTTOM);
break;
}
switch (ht) {
case 0:
style.setAlignment(HorizontalAlignment.CENTER);
break;
case 1:
style.setAlignment(HorizontalAlignment.LEFT);
break;
case 2:
style.setAlignment(HorizontalAlignment.RIGHT);
break;
}
//设置合并单元格的样式有问题
String ff = jsonObjectValue.getString("ff");//0 Times New Roman、 1 Arial、2 Tahoma 、3 Verdana、4 微软雅黑、5 宋体(Song)、6 黑体(ST Heiti)、7 楷体(ST Kaiti)、 8 仿宋(ST FangSong)、9 新宋体(ST Song)、10 华文新魏、11 华文行楷、12 华文隶书
int fs = jsonObjectValue.getInteger("fs") == null ? 14 : jsonObjectValue.getInteger("fs");//字体大小
int bl = jsonObjectValue.getInteger("bl") == null ? 0 : jsonObjectValue.getInteger("bl");//粗体 0 常规 、 1加粗
int it = jsonObjectValue.getInteger("it") == null ? 0 : jsonObjectValue.getInteger("it");//斜体 0 常规 、 1 斜体
String fc = jsonObjectValue.getString("fc") == null ? "" : jsonObjectValue.getString("fc");//字体颜色
font.setFontName(fontMap.get(ff));//字体名字
if (fc.length() > 0) {
font.setColor(new XSSFColor(new Color(Integer.parseInt(fc.replace("#", ""), 16))));
}
font.setFontName(ff);//字体名字
font.setFontHeightInPoints((short) fs);//字体大小
if (bl == 1) {
font.setBold(true);//粗体显示
}
font.setItalic(it == 1 ? true : false);//斜体
style.setFont(font);
style.setWrapText(true);//设置自动换行
cell.setCellStyle(style);
} else {
//数据格式错误
//System.out.println("错误的=" + index + ">>>" + str_);
}
}
//设置边框
setBorder(borderInfoObjectList, workbook, sheet);
}
}
参考:https://blog.csdn.net/u014632228/article/details/109738221



