当前位置: 首页 > news >正文

中达世联网站建设上海个人建站

中达世联网站建设,上海个人建站,事业单位备案网站,网站建设分为几个阶段阿丹#xff1a; 有些业务逻辑需要在导出非常大量的数据#xff0c;几百甚至几千万的数据这个时候再导出excel来对于性能都不是很友好#xff0c;这个时候就需要替换实现思路来解决这个问题。 本文章提供了两种解决的方案#xff0c;也是两种从数据库中拿取数据的方式一种是…阿丹 有些业务逻辑需要在导出非常大量的数据几百甚至几千万的数据这个时候再导出excel来对于性能都不是很友好这个时候就需要替换实现思路来解决这个问题。 本文章提供了两种解决的方案也是两种从数据库中拿取数据的方式一种是原生的jdbc一种是使用mybatis来封装对象来完成的。 使用字符串数组的导出 package com.lianlu.export.util;import org.apache.poi.ss.formula.functions.T;import java.io.*; import java.lang.reflect.Field; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors;/*** CSV导出工具类用于将数据列表导出为CSV文件。*/ public class CSVExportUtil {/*** 导出CSV文件。** param dataList 需要导出的内容类型为字符串数组的列表。* param validationRulesMap 校验和替换规则的映射键为列索引值为一个映射其中键为需要替换的字符串值为替换后的字符串。* param headers CSV文件的表头类型为字符串数组。* param fileName 导出CSV文件的名称。* throws IOException 如果在写入文件过程中发生异常。*/public static void exportCSV(ListString[] dataList, MapInteger, MapString, String validationRulesMap, String[] headers, String fileName) throws IOException {// 预处理数据校验和替换ListString[] preprocessedDataList preprocessData(dataList, validationRulesMap);// 写入CSV文件writeCSVToFile(preprocessedDataList, headers, fileName);}/*** 不需要替换规则的导出* param dataList* param headers* param fileName* throws IOException*/public static void exportCSV(ListString[] dataList, String[] headers, String fileName) throws IOException {// 写入CSV文件writeCSVToFile(dataList, headers, fileName);}/*** 预处理数据列表校验和替换。** param dataList 原始数据列表。* param validationRulesMap 校验和替换规则的映射。* return 预处理后的数据列表。*/private static ListString[] preprocessData(ListString[] dataList, MapInteger, MapString, String validationRulesMap) {return dataList.stream().map(row - preprocessDataRow(row, validationRulesMap)).collect(Collectors.toList());}/*** 预处理单行数据校验和替换。** param row 单行数据。* param validationRulesMap 校验和替换规则的映射。* return 预处理后的单行数据。*/private static String[] preprocessDataRow(String[] row, MapInteger, MapString, String validationRulesMap) {for (Map.EntryInteger, MapString, String entry : validationRulesMap.entrySet()) {int columnIndex entry.getKey();MapString, String rules entry.getValue();String originalValue row[columnIndex];String replacedValue rules.getOrDefault(originalValue, originalValue);row[columnIndex] replacedValue;}return row;}/*** 将预处理后的数据写入CSV文件。** param dataList 预处理后的数据列表。* param headers CSV文件的表头。* param fileName 导出CSV文件的名称。* throws IOException 如果在写入文件过程中发生异常。*/private static void writeCSVToFile(ListString[] dataList, String[] headers, String fileName) throws IOException {try (BufferedWriter writer new BufferedWriter(new FileWriter(fileName))) {// 写入表头writer.write(String.join(,, headers));writer.newLine();// 分批写入数据以提高性能AtomicInteger counter new AtomicInteger(0);while (counter.get() dataList.size()) {int batchSize Math.min(10000, dataList.size() - counter.get()); // 每次写入10000条数据可根据实际需求调整ListString[] batchData dataList.subList(counter.get(), counter.get() batchSize);for (String[] dataRow : batchData) {writer.write(String.join(,, dataRow));writer.newLine();}counter.addAndGet(batchSize);}}}/*** 从泛型对象中获取属性值并转换为字符串数组。** param data 泛型对象* return 字符串数组包含对象的属性值*/private String[] convertObjectToArray(T data) {Class? clazz data.getClass();Field[] fields clazz.getDeclaredFields();// 获取对象的所有字段并设置它们为可访问for (Field field : fields) {field.setAccessible(true);}String[] rowData new String[fields.length];// 遍历所有字段获取每个字段的值并添加到字符串数组中for (int i 0; i fields.length; i) {try {rowData[i] fields[i].get(data).toString();} catch (IllegalAccessException e) {throw new RuntimeException(Failed to access field value, e);}}return rowData;}} 通过对象的导出 package com.lianlu.export.util;import java.io.*; import java.lang.reflect.Field; import java.util.*;public class CSVExportUtilT {/*** 导出CSV文件。** param dataList 需要导出的数据列表泛型对象列表* param validationRulesMap 校验和替换规则映射键为列索引值为校验和替换规则的映射* param headers CSV表头数组* param fileName 导出CSV文件的名称* throws IOException 如果在写入文件时发生错误*/public void exportCSV(ListT dataList, MapInteger, MapString, String validationRulesMap, String[] headers, String fileName) throws IOException {// 预处理数据校验和替换ListString[] preprocessedData preprocessData(dataList, validationRulesMap);// 写入CSV文件writeCSV(preprocessedData, headers, fileName);}/*** 预处理数据校验和替换。** param dataList 数据列表泛型对象列表* param validationRulesMap 校验和替换规则映射键为列索引值为校验和替换规则的映射* return 预处理后的数据字符串数组列表*/private ListString[] preprocessData(ListT dataList, MapInteger, MapString, String validationRulesMap) {ListString[] preprocessedData new ArrayList(dataList.size());for (T data : dataList) {String[] rowData convertObjectToArray(data);preprocessedData.add(validateAndReplace(rowData, validationRulesMap));}return preprocessedData;}/*** 从泛型对象中获取属性值并转换为字符串数组。** param data 泛型对象* return 字符串数组包含对象的属性值*/private String[] convertObjectToArray(T data) {Class? clazz data.getClass();Field[] fields clazz.getDeclaredFields();// 获取对象的所有字段并设置它们为可访问for (Field field : fields) {field.setAccessible(true);}String[] rowData new String[fields.length];// 遍历所有字段获取每个字段的值并添加到字符串数组中for (int i 0; i fields.length; i) {try {rowData[i] fields[i].get(data).toString();} catch (IllegalAccessException e) {throw new RuntimeException(Failed to access field value, e);}}return rowData;}/*** 根据提供的校验和替换规则对字符串数组进行校验和替换。** param rowData 字符串数组* param rulesMap 校验和替换规则映射键为列索引值为校验和替换规则的映射* return 校验和替换后的字符串数组*/private String[] validateAndReplace(String[] rowData, MapInteger, MapString, String rulesMap) {for (Map.EntryInteger, MapString, String entry : rulesMap.entrySet()) {int columnIndex entry.getKey();MapString, String ruleMap entry.getValue();String currentValue rowData[columnIndex];if (ruleMap.containsKey(currentValue)) {rowData[columnIndex] ruleMap.get(currentValue);}}return rowData;}/*** 将预处理后的数据写入CSV文件。** param preprocessedData 预处理后的数据字符串数组列表* param headers CSV表头数组* param fileName 导出CSV文件的名称* throws IOException 如果在写入文件时发生错误*/private void writeCSV(ListString[] preprocessedData, String[] headers, String fileName) throws IOException {try (BufferedWriter writer new BufferedWriter(new FileWriter(fileName))) {CSVWriter csvWriter new CSVWriter(writer);// 写入表头csvWriter.writeNext(headers);// 写入数据csvWriter.writeAll(preprocessedData);csvWriter.close();}} }
http://www.huolong8.cn/news/325987/

相关文章:

  • 河南住房和城乡建设厅网站首页学网站建设 去那里
  • 网站的定义中国最大的库存尾货清货平台
  • 做网站一年多少钱8090在线观看免费观看
  • 外贸网站建设服务器免费网页制作工具下载
  • 重庆seo网站系统ps怎么做网站一寸的照片
  • 苏州公司网站制作公司湖南网址大全
  • 东莞市门户网站建设怎么样搬家公司电话
  • 西安手机网站案例用php做网站的新闻
  • 网站与网页的区别.微信旧版本下载
  • 个人网站模板源码下载重庆seo论坛
  • 宁波网站制作公司哪家好广州小程序开发多少钱
  • 网站代备seo网站优化软件价格
  • 湖南城市建设技术学院官方网站老版51个人空间找照片
  • 长沙免费模板建站动漫设计学院
  • 外贸网站代运营郑州微网站
  • 用php做网站难吗企业管理有限公司经营范围有哪些
  • dreamwearver可以做网站吗怎么做盈利的网站
  • 建设全网营销型网站上海网站快速排名
  • 建设银行辽宁招聘网站丽水网站建设费用
  • 网站后端开发流程推广系统
  • 做图哪个网站素材多太仓手机网站建设
  • 福田专业网站建设公司哪家好河北邯郸是几线城市
  • 网站建设新技术品牌商城网站制作
  • 学做网站论坛全部视频注册传媒公司需要的条件
  • 微信网站开发 全屏昌黎网站建设
  • 青岛做网站优化的公司开源php企业网站
  • 蓬莱网站建设哪家好wordpress哪里找域名
  • 网站建设自主开发的三种方式使用wampserver做响应式网站
  • 哪家网站建设最好做网站的公司怎么做业务
  • 做网站的案例工厂展厅效果图