|
|
|
package com.fr.plugin.decision;
|
|
|
|
|
|
|
|
import com.finebi.burger.api.bean.configuration.table.field.FineBusinessField;
|
|
|
|
import com.finebi.foundation.api.structure.result.BIDetailCell;
|
|
|
|
import com.finebi.foundation.api.structure.result.BIDetailResult;
|
|
|
|
import com.fr.cache.Attachment;
|
|
|
|
import com.fr.cache.AttachmentSource;
|
|
|
|
import com.fr.general.Inter;
|
|
|
|
import com.fr.json.JSONArray;
|
|
|
|
import com.fr.json.JSONObject;
|
|
|
|
import com.fr.stable.StringUtils;
|
|
|
|
import com.fr.third.org.apache.poi.hssf.usermodel.*;
|
|
|
|
import com.fr.third.org.apache.poi.hssf.util.Region;
|
|
|
|
import com.fr.third.org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
|
|
|
import com.fr.third.v2.org.apache.poi.ss.usermodel.Cell;
|
|
|
|
import com.fr.third.v2.org.apache.poi.ss.usermodel.DateUtil;
|
|
|
|
import com.fr.third.v2.org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFCell;
|
|
|
|
import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFRow;
|
|
|
|
import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFSheet;
|
|
|
|
import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* created by ezreal 2020/1/12
|
|
|
|
*/
|
|
|
|
public class HWUtils {
|
|
|
|
/**
|
|
|
|
* 根据附件id获取对应附件excel对象
|
|
|
|
*
|
|
|
|
* @param attachId
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static XSSFWorkbook getExcelWork(String attachId) {
|
|
|
|
XSSFWorkbook sheets = null;
|
|
|
|
try {
|
|
|
|
Attachment attachment = AttachmentSource.getAttachment(attachId);
|
|
|
|
if (attachment != null) {
|
|
|
|
InputStream inputStream = attachment.getInputStream();
|
|
|
|
sheets = new XSSFWorkbook(inputStream);
|
|
|
|
sheets.getNumberOfSheets();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return sheets;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static JSONArray getSheetFiled(XSSFSheet sheet) {
|
|
|
|
JSONArray result = JSONArray.create();
|
|
|
|
int rowCounts = sheet.getPhysicalNumberOfRows();
|
|
|
|
int titleRow = -1;
|
|
|
|
for (int row = 0; row < rowCounts; row++) {
|
|
|
|
XSSFRow xssfRow = sheet.getRow(row);
|
|
|
|
if (xssfRow == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
titleRow = row;
|
|
|
|
//循环取每个单元格(cell)的数据
|
|
|
|
Iterator<Cell> cellIterator = xssfRow.cellIterator();
|
|
|
|
while (cellIterator.hasNext()) {
|
|
|
|
XSSFCell cell = (XSSFCell) cellIterator.next();
|
|
|
|
String value = getString(cell);
|
|
|
|
if (StringUtils.isNotBlank(value)) {
|
|
|
|
JSONObject o = JSONObject.create().put("titleRow", titleRow).put("value", cell.getColumnIndex()).put("text", value);
|
|
|
|
result.add(o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (titleRow >= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Map<String, List> getSheetContent(XSSFSheet sheet, int titleRow, JSONObject tableCol) {
|
|
|
|
Map<String, List> result = new HashMap<String, List>();
|
|
|
|
Iterator<Map.Entry<String, Object>> iterator = tableCol.iterator();
|
|
|
|
List<CellRangeAddress> combineCell = getCombineCell(sheet);
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
Map.Entry<String, Object> entry = iterator.next();
|
|
|
|
result.put(entry.getKey(), new ArrayList());
|
|
|
|
}
|
|
|
|
int lastRowNum = sheet.getLastRowNum();
|
|
|
|
for (int row = titleRow + 1; row <= lastRowNum; row++) {
|
|
|
|
XSSFRow xssfRow = sheet.getRow(row);
|
|
|
|
if (xssfRow == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (Map.Entry<String, List> entry : result.entrySet()) {
|
|
|
|
String key = entry.getKey();
|
|
|
|
int col = tableCol.getInt(key);
|
|
|
|
List value = entry.getValue();
|
|
|
|
XSSFCell cell = xssfRow.getCell(col);
|
|
|
|
|
|
|
|
if (cell != null) {
|
|
|
|
String cellValue = getCellValue(sheet, combineCell, row, col, cell);
|
|
|
|
value.add(StringUtils.isBlank(cellValue) ? StringUtils.EMPTY : cellValue);
|
|
|
|
} else {
|
|
|
|
value.add(StringUtils.EMPTY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Map<String, List> getValues(List<FineBusinessField> fields, BIDetailResult detailResult) {
|
|
|
|
Map<String, List> result = new HashMap<String, List>();
|
|
|
|
while (detailResult.hasNext()) {
|
|
|
|
List<BIDetailCell> cells = detailResult.next();
|
|
|
|
for (int i = 0; i < fields.size(); ++i) {
|
|
|
|
String fieldName = fields.get(i).getName();
|
|
|
|
Object cellValue = cells.get(i).getData() != null ? cells.get(i).getData() : StringUtils.EMPTY;
|
|
|
|
if (result.containsKey(fieldName)) {
|
|
|
|
result.get(fieldName).add(cellValue.toString());
|
|
|
|
} else {
|
|
|
|
List<Object> rowList = new ArrayList<>();
|
|
|
|
rowList.add(cellValue.toString());
|
|
|
|
result.put(fieldName, rowList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getCellValue(XSSFSheet sheet, List<CellRangeAddress> combineCell, int row, int col, XSSFCell cell) {
|
|
|
|
int firstC = 0;
|
|
|
|
int lastC = 0;
|
|
|
|
int firstR = 0;
|
|
|
|
int lastR = 0;
|
|
|
|
for (CellRangeAddress ca : combineCell) {
|
|
|
|
// 获得合并单元格的起始行, 结束行, 起始列, 结束列
|
|
|
|
firstC = ca.getFirstColumn();
|
|
|
|
lastC = ca.getLastColumn();
|
|
|
|
firstR = ca.getFirstRow();
|
|
|
|
lastR = ca.getLastRow();
|
|
|
|
if (cell.getColumnIndex() <= lastC && cell.getColumnIndex() >= firstC) {
|
|
|
|
if (cell.getRowIndex() <= lastR && cell.getRowIndex() >= firstR) {
|
|
|
|
XSSFRow xRow = sheet.getRow(firstR);
|
|
|
|
XSSFCell xCell = xRow.getCell(firstC);
|
|
|
|
return getString(xCell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return getString(cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static List<CellRangeAddress> getCombineCell(XSSFSheet sheet) {
|
|
|
|
// 获得一个 sheet 中合并单元格的数量
|
|
|
|
List<CellRangeAddress> list = new ArrayList<CellRangeAddress>();
|
|
|
|
int sheetmergerCount = sheet.getNumMergedRegions();
|
|
|
|
// 遍历合并单元格
|
|
|
|
for (int i = 0; i < sheetmergerCount; i++) {
|
|
|
|
// 获得合并单元格加入list中
|
|
|
|
CellRangeAddress ca = sheet.getMergedRegion(i);
|
|
|
|
list.add(ca);
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 把单元格的内容转为字符串
|
|
|
|
*
|
|
|
|
* @param xssfCell 单元格
|
|
|
|
* @return 字符串
|
|
|
|
*/
|
|
|
|
public static String getString(XSSFCell xssfCell) {
|
|
|
|
if (xssfCell == null) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
int type = xssfCell.getCellType();
|
|
|
|
if (type == XSSFCell.CELL_TYPE_NUMERIC) {
|
|
|
|
if (DateUtil.isCellDateFormatted(xssfCell)) {
|
|
|
|
// 如果是date类型则 ,获取该cell的date值
|
|
|
|
return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(xssfCell.getNumericCellValue()));
|
|
|
|
} else { // 纯数字
|
|
|
|
return String.valueOf(xssfCell.getNumericCellValue());
|
|
|
|
}
|
|
|
|
} else if (type == XSSFCell.CELL_TYPE_BOOLEAN) {
|
|
|
|
return String.valueOf(xssfCell.getBooleanCellValue());
|
|
|
|
} else {
|
|
|
|
return xssfCell.getStringCellValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************excel2003*************************************************/
|
|
|
|
|
|
|
|
public static HSSFWorkbook getExcel2003Work(String attachId) {
|
|
|
|
HSSFWorkbook sheets = null;
|
|
|
|
try {
|
|
|
|
Attachment attachment = AttachmentSource.getAttachment(attachId);
|
|
|
|
if (attachment != null) {
|
|
|
|
InputStream inputStream = attachment.getInputStream();
|
|
|
|
POIFSFileSystem poifsFileSystem = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
poifsFileSystem = new POIFSFileSystem(inputStream);
|
|
|
|
} catch (IOException var13) {
|
|
|
|
throw new IOException(Inter.getLocText("Fine-Engine_NS_Exception_ReadExcelError"));
|
|
|
|
}
|
|
|
|
sheets = new HSSFWorkbook(poifsFileSystem);
|
|
|
|
sheets.getNumberOfSheets();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return sheets;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static JSONArray get2003SheetFiled(HSSFSheet sheet) {
|
|
|
|
JSONArray result = JSONArray.create();
|
|
|
|
int rowCounts = sheet.getPhysicalNumberOfRows();
|
|
|
|
int titleRow = -1;
|
|
|
|
for (int row = 0; row < rowCounts; row++) {
|
|
|
|
HSSFRow hssfRow = sheet.getRow(row);
|
|
|
|
if (hssfRow == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
//循环取每个单元格(cell)的数据
|
|
|
|
for (int cellIndex = 0; cellIndex < hssfRow.getPhysicalNumberOfCells(); cellIndex++) {
|
|
|
|
HSSFCell hssfCell = hssfRow.getCell(cellIndex);
|
|
|
|
String value = get2003String(hssfCell);
|
|
|
|
if (StringUtils.isNotBlank(value)) {
|
|
|
|
titleRow = row;
|
|
|
|
JSONObject o = JSONObject.create().put("titleRow", titleRow).put("value", cellIndex).put("text", value);
|
|
|
|
result.add(o);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (titleRow >= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 把单元格的内容转为字符串
|
|
|
|
*
|
|
|
|
* @param hssfCell 单元格
|
|
|
|
* @return 字符串
|
|
|
|
*/
|
|
|
|
public static String get2003String(HSSFCell hssfCell) {
|
|
|
|
if (hssfCell == null) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
int type = hssfCell.getCellType();
|
|
|
|
if (type == HSSFCell.CELL_TYPE_NUMERIC) {
|
|
|
|
if (HSSFDateUtil.isCellDateFormatted(hssfCell)) {
|
|
|
|
// 如果是date类型则 ,获取该cell的date值
|
|
|
|
return new SimpleDateFormat("yyyy-MM-dd").format(DateUtil.getJavaDate(hssfCell.getNumericCellValue()));
|
|
|
|
} else { // 纯数字
|
|
|
|
return String.valueOf(hssfCell.getNumericCellValue());
|
|
|
|
}
|
|
|
|
} else if (type == XSSFCell.CELL_TYPE_BOOLEAN) {
|
|
|
|
return String.valueOf(hssfCell.getBooleanCellValue());
|
|
|
|
} else {
|
|
|
|
return hssfCell.getStringCellValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取2003的值
|
|
|
|
*
|
|
|
|
* @param sheet
|
|
|
|
* @param row
|
|
|
|
* @param col
|
|
|
|
* @param cell
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String get2003CellValue(HSSFSheet sheet, int row, int col, HSSFCell cell) {
|
|
|
|
int sheetMergeCount = sheet.getNumMergedRegions();
|
|
|
|
for (int i = 0; i < sheetMergeCount; i++) {
|
|
|
|
Region mergedRegionAt = sheet.getMergedRegionAt(i);
|
|
|
|
mergedRegionAt.getRowFrom();
|
|
|
|
// CellRangeAddress ca = sheet.getMergedRegion(i);
|
|
|
|
int firstColumn = mergedRegionAt.getColumnFrom();
|
|
|
|
int lastColumn = mergedRegionAt.getColumnTo();
|
|
|
|
int firstRow = mergedRegionAt.getRowFrom();
|
|
|
|
int lastRow = mergedRegionAt.getRowTo();
|
|
|
|
if (row >= firstRow && row <= lastRow) {
|
|
|
|
if (col >= firstColumn && col <= lastColumn) {
|
|
|
|
HSSFRow fRow = sheet.getRow(firstRow);
|
|
|
|
HSSFCell fCell = fRow.getCell(firstColumn);
|
|
|
|
return get2003String(fCell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return get2003String(cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Map<String, List> get2003SheetContent(HSSFSheet sheet, int titleRow, JSONObject tableCol) {
|
|
|
|
Map<String, List> result = new HashMap<String, List>();
|
|
|
|
Iterator<Map.Entry<String, Object>> iterator = tableCol.iterator();
|
|
|
|
// List<CellRangeAddress> combineCell = getCombineCell(sheet);
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
Map.Entry<String, Object> entry = iterator.next();
|
|
|
|
result.put(entry.getKey(), new ArrayList());
|
|
|
|
}
|
|
|
|
int lastRowNum = sheet.getLastRowNum();
|
|
|
|
for (int row = titleRow + 1; row <= lastRowNum; row++) {
|
|
|
|
HSSFRow hssfRow = sheet.getRow(row);
|
|
|
|
if (hssfRow == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (Map.Entry<String, List> entry : result.entrySet()) {
|
|
|
|
String key = entry.getKey();
|
|
|
|
int col = tableCol.getInt(key);
|
|
|
|
List value = entry.getValue();
|
|
|
|
HSSFCell cell = hssfRow.getCell(col);
|
|
|
|
|
|
|
|
if (cell != null) {
|
|
|
|
String cellValue = get2003CellValue(sheet, row, col, cell);
|
|
|
|
value.add(StringUtils.isBlank(cellValue) ? StringUtils.EMPTY : cellValue);
|
|
|
|
} else {
|
|
|
|
value.add(StringUtils.EMPTY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|