JSD-9277 导出excel文件属性自定义
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
6.6 KiB

/**
* Copyright (C), 2015-2021
* FileName: ExcelHandler
* Author: Louis
* Date: 2020/3/16 21:55
* Description: ExcelHandler
* History:
* <author> <time> <version> <desc>
*/
package com.fr.plugin.ibgq.utils;
import com.fanruan.api.i18n.I18nKit;
import com.fanruan.api.log.LogKit;
import com.fr.io.exporter.poi.wrapper.POIWorkbookAction;
import com.fr.main.workbook.ResultWorkBook;
import com.fr.plugin.context.PluginContexts;
import com.fr.plugin.ibgq.provider.DocSummaryAttr;
import com.fr.script.Calculator;
import com.fr.stable.ArrayUtils;
import com.fr.stable.ParameterProvider;
import com.fr.third.v2.org.apache.poi.POIXMLProperties;
import com.fr.third.v2.org.apache.poi.hpsf.CustomProperties;
import com.fr.third.v2.org.apache.poi.hpsf.DocumentSummaryInformation;
import com.fr.third.v2.org.apache.poi.hpsf.SummaryInformation;
import com.fr.third.v2.org.apache.poi.hssf.usermodel.HSSFSheet;
import com.fr.third.v2.org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.fr.third.v2.org.apache.poi.xssf.streaming.SXSSFWorkbook;
import com.fr.third.v2.org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
public class ExcelHandler {
public ExcelHandler() {
}
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\aa.xls"));
HSSFWorkbook workbook = new HSSFWorkbook();
workbook.createInformationProperties();
SummaryInformation summaryInformation = workbook.getSummaryInformation();
DocumentSummaryInformation documentSummaryInformation = workbook.getDocumentSummaryInformation();
// 添加备注信息
summaryInformation.setComments("TTTTTTT");
summaryInformation.setAuthor("ssss");
// 添加自定义属性
CustomProperties customProperties = new CustomProperties();
customProperties.put("repId", "33");
customProperties.put("ver", "211");
customProperties.put("KSOProductBuildVer", "2052-1-.8.0.630");
HSSFSheet sheet = workbook.createSheet();//创建工作表(Sheet)
sheet = workbook.createSheet("Test");//创建工作表(Sheet)
documentSummaryInformation.setCustomProperties(customProperties);
workbook.write(fileOutputStream);
fileOutputStream.close();
}
public static void setCustomProperties(ResultWorkBook resultWorkBook, POIWorkbookAction poiWorkbookAction) {
if (!PluginContexts.currentContext().isAvailable()) {
LogKit.error(I18nKit.getLocText("Plugin-ibgq_Licence_Expired"));
return;
}
DocSummaryAttr docSummaryAttr = resultWorkBook.getAttrMark(DocSummaryAttr.XML_TAG);
if (null == docSummaryAttr) {
return;
}
ParameterProvider[] parameterProviders = docSummaryAttr.getParameters(Calculator.createCalculator());
if (ArrayUtils.isEmpty(parameterProviders)) {
return;
}
Object workbook = poiWorkbookAction.getWorkbook();
if (workbook instanceof SXSSFWorkbook) {
SXSSFWorkbook sxssfWorkbook = (SXSSFWorkbook) workbook;
XSSFWorkbook xssfWorkbook = sxssfWorkbook.getXSSFWorkbook();
POIXMLProperties.CustomProperties customProperties = xssfWorkbook.getProperties().getCustomProperties();
for (ParameterProvider parameter : parameterProviders) {
addCustomProperties(customProperties, parameter);
}
}
}
/**
* Excel2003自定义属性
*
* @param resultWorkBook
* @param workbook
*/
public static void setCustomProperties(ResultWorkBook resultWorkBook, HSSFWorkbook workbook) {
if (!PluginContexts.currentContext().isAvailable()) {
LogKit.error(I18nKit.getLocText("Plugin-ibgq_Licence_Expired"));
return;
}
DocSummaryAttr docSummaryAttr = resultWorkBook.getAttrMark(DocSummaryAttr.XML_TAG);
if (null == docSummaryAttr) {
return;
}
ParameterProvider[] parameterProviders = docSummaryAttr.getParameters(Calculator.createCalculator());
if (ArrayUtils.isEmpty(parameterProviders)) {
return;
}
workbook.createInformationProperties();
DocumentSummaryInformation documentSummaryInformation = workbook.getDocumentSummaryInformation();
CustomProperties customProperties = new CustomProperties();
for (ParameterProvider parameter : parameterProviders) {
addCustomProperties(customProperties, parameter);
}
documentSummaryInformation.setCustomProperties(customProperties);
}
private static void addCustomProperties(POIXMLProperties.CustomProperties customProperties, ParameterProvider parameter) {
if (parameter.getValue() instanceof String) {
customProperties.addProperty(parameter.getName(), (String) parameter.getValue());
} else if (parameter.getValue() instanceof Double) {
customProperties.addProperty(parameter.getName(), (Double) parameter.getValue());
} else if (parameter.getValue() instanceof Integer) {
customProperties.addProperty(parameter.getName(), (Integer) parameter.getValue());
} else if (parameter.getValue() instanceof Boolean) {
customProperties.addProperty(parameter.getName(), (Boolean) parameter.getValue());
} else {
customProperties.addProperty(parameter.getName(), parameter.valueToString());
}
}
private static void addCustomProperties(CustomProperties customProperties, ParameterProvider parameter) {
if (parameter.getValue() instanceof String) {
customProperties.put(parameter.getName(), (String) parameter.getValue());
} else if (parameter.getValue() instanceof Long) {
customProperties.put(parameter.getName(), (Long) parameter.getValue());
} else if (parameter.getValue() instanceof Double) {
customProperties.put(parameter.getName(), (Double) parameter.getValue());
} else if (parameter.getValue() instanceof Integer) {
customProperties.put(parameter.getName(), (Integer) parameter.getValue());
} else if (parameter.getValue() instanceof Boolean) {
customProperties.put(parameter.getName(), (Boolean) parameter.getValue());
} else if (parameter.getValue() instanceof Date) {
customProperties.put(parameter.getName(), (Date) parameter.getValue());
} else {
customProperties.put(parameter.getName(), parameter.valueToString());
}
}
}