导出csv功能1
This commit is contained in:
parent
7b085cb5ff
commit
fe5c64a665
|
@ -3,21 +3,15 @@ package shkd.sys.sys.plugin.report;
|
||||||
import kd.bos.algo.DataSet;
|
import kd.bos.algo.DataSet;
|
||||||
import kd.bos.algo.JoinDataSet;
|
import kd.bos.algo.JoinDataSet;
|
||||||
import kd.bos.algo.JoinType;
|
import kd.bos.algo.JoinType;
|
||||||
import kd.bos.dataentity.entity.DynamicObject;
|
|
||||||
import kd.bos.dataentity.entity.DynamicObjectCollection;
|
|
||||||
import kd.bos.db.DB;
|
import kd.bos.db.DB;
|
||||||
import kd.bos.db.DBRoute;
|
import kd.bos.db.DBRoute;
|
||||||
import kd.bos.entity.report.AbstractReportListDataPlugin;
|
import kd.bos.entity.report.AbstractReportListDataPlugin;
|
||||||
import kd.bos.entity.report.FastFilter;
|
|
||||||
import kd.bos.entity.report.FilterItemInfo;
|
|
||||||
import kd.bos.entity.report.ReportQueryParam;
|
import kd.bos.entity.report.ReportQueryParam;
|
||||||
import kd.bos.logging.Log;
|
import kd.bos.logging.Log;
|
||||||
import kd.bos.logging.LogFactory;
|
import kd.bos.logging.LogFactory;
|
||||||
import kd.bos.orm.ORM;
|
|
||||||
import kd.bos.orm.query.QCP;
|
|
||||||
import kd.bos.orm.query.QFilter;
|
import kd.bos.orm.query.QFilter;
|
||||||
import kd.bos.servicehelper.QueryServiceHelper;
|
import kd.bos.servicehelper.QueryServiceHelper;
|
||||||
import kd.bos.util.CollectionUtils;
|
import shkd.sys.sys.plugin.report.util.ReportUtils;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
|
@ -12,6 +12,7 @@ import kd.bos.orm.query.QFilter;
|
||||||
import kd.bos.permission.api.HasPermOrgResult;
|
import kd.bos.permission.api.HasPermOrgResult;
|
||||||
import kd.bos.servicehelper.QueryServiceHelper;
|
import kd.bos.servicehelper.QueryServiceHelper;
|
||||||
import kd.bos.servicehelper.permission.PermissionServiceHelper;
|
import kd.bos.servicehelper.permission.PermissionServiceHelper;
|
||||||
|
import shkd.sys.sys.plugin.report.util.ReportUtils;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
|
@ -5,28 +5,23 @@ import kd.bos.algo.JoinDataSet;
|
||||||
import kd.bos.algo.JoinType;
|
import kd.bos.algo.JoinType;
|
||||||
import kd.bos.dataentity.entity.DynamicObject;
|
import kd.bos.dataentity.entity.DynamicObject;
|
||||||
import kd.bos.dataentity.entity.DynamicObjectCollection;
|
import kd.bos.dataentity.entity.DynamicObjectCollection;
|
||||||
import kd.bos.dataentity.entity.LocaleString;
|
|
||||||
import kd.bos.db.DB;
|
import kd.bos.db.DB;
|
||||||
import kd.bos.db.DBRoute;
|
import kd.bos.db.DBRoute;
|
||||||
import kd.bos.entity.MainEntityType;
|
|
||||||
import kd.bos.entity.report.*;
|
import kd.bos.entity.report.*;
|
||||||
import kd.bos.entity.tree.TreeNode;
|
import kd.bos.entity.tree.TreeNode;
|
||||||
import kd.bos.logging.Log;
|
import kd.bos.logging.Log;
|
||||||
import kd.bos.logging.LogFactory;
|
import kd.bos.logging.LogFactory;
|
||||||
import kd.bos.org.model.OrgTreeBuildType;
|
import kd.bos.org.model.OrgTreeBuildType;
|
||||||
import kd.bos.org.model.OrgTreeParam;
|
import kd.bos.org.model.OrgTreeParam;
|
||||||
import kd.bos.orm.ORM;
|
|
||||||
import kd.bos.orm.query.QCP;
|
import kd.bos.orm.query.QCP;
|
||||||
import kd.bos.orm.query.QFilter;
|
import kd.bos.orm.query.QFilter;
|
||||||
import kd.bos.servicehelper.BusinessDataServiceHelper;
|
import kd.bos.servicehelper.BusinessDataServiceHelper;
|
||||||
import kd.bos.servicehelper.MetadataServiceHelper;
|
|
||||||
import kd.bos.servicehelper.QueryServiceHelper;
|
import kd.bos.servicehelper.QueryServiceHelper;
|
||||||
import kd.bos.servicehelper.operation.SaveServiceHelper;
|
import kd.bos.servicehelper.operation.SaveServiceHelper;
|
||||||
import kd.bos.servicehelper.org.OrgUnitServiceHelper;
|
import kd.bos.servicehelper.org.OrgUnitServiceHelper;
|
||||||
import kd.bos.servicehelper.org.OrgViewType;
|
import kd.bos.servicehelper.org.OrgViewType;
|
||||||
import kd.bos.util.CollectionUtils;
|
|
||||||
import shkd.sys.sys.plugin.form.PaymentProcessingBillPlugin;
|
|
||||||
import shkd.sys.sys.plugin.report.domain.FinanceVarietyEnum;
|
import shkd.sys.sys.plugin.report.domain.FinanceVarietyEnum;
|
||||||
|
import shkd.sys.sys.plugin.report.util.ReportUtils;
|
||||||
|
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
|
|
@ -6,23 +6,20 @@ import kd.bos.dataentity.entity.DynamicObjectCollection;
|
||||||
import kd.bos.db.DB;
|
import kd.bos.db.DB;
|
||||||
import kd.bos.db.DBRoute;
|
import kd.bos.db.DBRoute;
|
||||||
import kd.bos.entity.report.AbstractReportListDataPlugin;
|
import kd.bos.entity.report.AbstractReportListDataPlugin;
|
||||||
import kd.bos.entity.report.FastFilter;
|
|
||||||
import kd.bos.entity.report.FilterItemInfo;
|
|
||||||
import kd.bos.entity.report.ReportQueryParam;
|
import kd.bos.entity.report.ReportQueryParam;
|
||||||
import kd.bos.entity.tree.TreeNode;
|
import kd.bos.entity.tree.TreeNode;
|
||||||
import kd.bos.logging.Log;
|
import kd.bos.logging.Log;
|
||||||
import kd.bos.logging.LogFactory;
|
import kd.bos.logging.LogFactory;
|
||||||
import kd.bos.org.model.OrgTreeBuildType;
|
import kd.bos.org.model.OrgTreeBuildType;
|
||||||
import kd.bos.org.model.OrgTreeParam;
|
import kd.bos.org.model.OrgTreeParam;
|
||||||
import kd.bos.orm.ORM;
|
|
||||||
import kd.bos.orm.query.QCP;
|
import kd.bos.orm.query.QCP;
|
||||||
import kd.bos.orm.query.QFilter;
|
import kd.bos.orm.query.QFilter;
|
||||||
import kd.bos.servicehelper.BusinessDataServiceHelper;
|
import kd.bos.servicehelper.BusinessDataServiceHelper;
|
||||||
import kd.bos.servicehelper.QueryServiceHelper;
|
import kd.bos.servicehelper.QueryServiceHelper;
|
||||||
import kd.bos.servicehelper.org.OrgUnitServiceHelper;
|
import kd.bos.servicehelper.org.OrgUnitServiceHelper;
|
||||||
import kd.bos.servicehelper.org.OrgViewType;
|
import kd.bos.servicehelper.org.OrgViewType;
|
||||||
import kd.bos.util.CollectionUtils;
|
|
||||||
import shkd.sys.sys.plugin.report.domain.FinanceVarietyEnum;
|
import shkd.sys.sys.plugin.report.domain.FinanceVarietyEnum;
|
||||||
|
import shkd.sys.sys.plugin.report.util.ReportUtils;
|
||||||
|
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
|
|
@ -0,0 +1,82 @@
|
||||||
|
package shkd.sys.sys.plugin.report.util;
|
||||||
|
|
||||||
|
import com.opencsv.CSVWriter;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
public class CsvExportUtil {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通用 CSV 导出
|
||||||
|
*
|
||||||
|
* @param os 输出流(可以是 FileOutputStream / response.getOutputStream())
|
||||||
|
* @param headers 表头,可为 null
|
||||||
|
* @param rows 数据,每行是一个 List<Object>,元素自动 toString()
|
||||||
|
* @throws IOException IO 异常
|
||||||
|
*/
|
||||||
|
public static void writeCsv(OutputStream os,
|
||||||
|
List<String> headers,
|
||||||
|
Collection<? extends List<?>> rows) throws IOException {
|
||||||
|
|
||||||
|
try (CSVWriter writer = new CSVWriter(
|
||||||
|
new OutputStreamWriter(os, StandardCharsets.UTF_8))) {
|
||||||
|
|
||||||
|
// 1. 表头
|
||||||
|
if (headers != null && !headers.isEmpty()) {
|
||||||
|
writer.writeNext(headers.toArray(new String[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 内容
|
||||||
|
for (List<?> row : rows) {
|
||||||
|
String[] line = row.stream()
|
||||||
|
.map(e -> e == null ? "" : e.toString())
|
||||||
|
.toArray(String[]::new);
|
||||||
|
writer.writeNext(line);
|
||||||
|
}
|
||||||
|
writer.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把数据直接写进磁盘文件
|
||||||
|
*
|
||||||
|
* @param targetFile 目标文件路径(如 Paths.get("/tmp/report.csv"))
|
||||||
|
* @param headers 表头(可为 null)
|
||||||
|
* @param rows 数据行
|
||||||
|
*/
|
||||||
|
public static void writeCsvToFile(Path targetFile,
|
||||||
|
List<String> headers,
|
||||||
|
Collection<? extends List<?>> rows) throws IOException {
|
||||||
|
|
||||||
|
// 自动创建父目录
|
||||||
|
Files.createDirectories(targetFile.getParent());
|
||||||
|
|
||||||
|
try (BufferedWriter bw = Files.newBufferedWriter(targetFile,
|
||||||
|
StandardCharsets.UTF_8);
|
||||||
|
CSVWriter writer = new CSVWriter(bw)) {
|
||||||
|
|
||||||
|
// 1. 表头
|
||||||
|
if (headers != null && !headers.isEmpty()) {
|
||||||
|
writer.writeNext(headers.toArray(new String[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 数据
|
||||||
|
for (List<?> row : rows) {
|
||||||
|
String[] line = row.stream()
|
||||||
|
.map(e -> e == null ? "" : e.toString())
|
||||||
|
.toArray(String[]::new);
|
||||||
|
writer.writeNext(line);
|
||||||
|
}
|
||||||
|
writer.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,83 @@
|
||||||
|
package shkd.sys.sys.plugin.report.util;
|
||||||
|
|
||||||
|
import dm.jdbc.util.StringUtil;
|
||||||
|
import kd.bos.cache.CacheFactory;
|
||||||
|
import kd.bos.cache.TempFileCache;
|
||||||
|
import kd.bos.dataentity.metadata.clr.DataEntityPropertyCollection;
|
||||||
|
import kd.bos.entity.MainEntityType;
|
||||||
|
import kd.bos.entity.report.IReportListModel;
|
||||||
|
import kd.bos.form.control.Toolbar;
|
||||||
|
import kd.bos.form.control.events.ItemClickEvent;
|
||||||
|
import kd.bos.report.ReportList;
|
||||||
|
import kd.bos.report.plugin.AbstractReportFormPlugin;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.EventObject;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
public class ExportCSV extends AbstractReportFormPlugin {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerListener(EventObject e) {
|
||||||
|
super.registerListener(e);
|
||||||
|
// 获取工具栏,监听整个工具栏
|
||||||
|
Toolbar export = this.getView().getControl("toolbarap");
|
||||||
|
// 监听工具栏这个按钮的点击事件
|
||||||
|
export.addItemClickListener(this);
|
||||||
|
export.addClickListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void itemClick(ItemClickEvent evt) {
|
||||||
|
super.itemClick(evt);
|
||||||
|
String itemKey = evt.getItemKey();
|
||||||
|
if (StringUtil.equals("shkd_exportcsv",itemKey)){
|
||||||
|
MainEntityType dataEntityType = this.getModel().getDataEntityType();
|
||||||
|
DataEntityPropertyCollection properties = dataEntityType.getProperties();
|
||||||
|
String reportName = dataEntityType.getDisplayName().getLocaleValue();
|
||||||
|
|
||||||
|
// List<String> header = properties.stream()
|
||||||
|
// .map(o -> o.getDisplayName().getLocaleValue())
|
||||||
|
// .collect(Collectors.toList());
|
||||||
|
|
||||||
|
String timestamp = LocalDateTime.now()
|
||||||
|
.format(DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
|
||||||
|
|
||||||
|
int random = ThreadLocalRandom.current().nextInt(100, 1000); // 3 位随机数
|
||||||
|
|
||||||
|
String reportNameExt = timestamp + random;
|
||||||
|
|
||||||
|
IReportListModel reportList = ((ReportList) this.getControl("reportlistap")).getReportModel();
|
||||||
|
int rowCount = reportList.getRowCount();
|
||||||
|
List<Object> rows = IntStream.range(1, rowCount)
|
||||||
|
.mapToObj(reportList::getRowData)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
try {
|
||||||
|
// DateUtils.get
|
||||||
|
Path tempFile = Files.createTempFile(reportName + reportNameExt, ".csv");
|
||||||
|
CsvExportUtil.writeCsvToFile(tempFile,null, Collections.singleton(rows));
|
||||||
|
// 上传到临时文件服务器 获取压缩包下载地址
|
||||||
|
TempFileCache tempFileCache = CacheFactory.getCommonCacheFactory().getTempFileCache();
|
||||||
|
String url = tempFileCache.saveAsUrl(tempFile.getFileName().toString(), Files.newInputStream(tempFile), 3 * 60);
|
||||||
|
// 下载压缩包
|
||||||
|
this.getView().download(url);
|
||||||
|
Files.delete(tempFile);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package shkd.sys.sys.plugin.report;
|
package shkd.sys.sys.plugin.report.util;
|
||||||
|
|
||||||
import kd.bos.entity.report.FastFilter;
|
import kd.bos.entity.report.FastFilter;
|
||||||
import kd.bos.entity.report.FilterItemInfo;
|
import kd.bos.entity.report.FilterItemInfo;
|
Loading…
Reference in New Issue