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

广州云脑网站建设重庆网站网络推广

广州云脑网站建设,重庆网站网络推广,台州做网站,php与网站开发阿丹: 有些业务逻辑需要在导出非常大量的数据,几百甚至几千万的数据这个时候再导出excel来对于性能都不是很友好,这个时候就需要替换实现思路来解决这个问题。 本文章提供了两种解决的方案,也是两种从数据库中拿取数据的方式一种是…

阿丹:

        有些业务逻辑需要在导出非常大量的数据,几百甚至几千万的数据这个时候再导出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(List<String[]> dataList, Map<Integer, Map<String, String>> validationRulesMap, String[] headers, String fileName) throws IOException {// 预处理数据(校验和替换)List<String[]> preprocessedDataList = preprocessData(dataList, validationRulesMap);// 写入CSV文件writeCSVToFile(preprocessedDataList, headers, fileName);}/*** 不需要替换规则的导出* @param dataList* @param headers* @param fileName* @throws IOException*/public static void exportCSV(List<String[]> dataList, String[] headers, String fileName) throws IOException {// 写入CSV文件writeCSVToFile(dataList, headers, fileName);}/*** 预处理数据列表(校验和替换)。** @param dataList          原始数据列表。* @param validationRulesMap 校验和替换规则的映射。* @return 预处理后的数据列表。*/private static List<String[]> preprocessData(List<String[]> dataList, Map<Integer, Map<String, String>> validationRulesMap) {return dataList.stream().map(row -> preprocessDataRow(row, validationRulesMap)).collect(Collectors.toList());}/*** 预处理单行数据(校验和替换)。** @param row               单行数据。* @param validationRulesMap 校验和替换规则的映射。* @return 预处理后的单行数据。*/private static String[] preprocessDataRow(String[] row, Map<Integer, Map<String, String>> validationRulesMap) {for (Map.Entry<Integer, Map<String, String>> entry : validationRulesMap.entrySet()) {int columnIndex = entry.getKey();Map<String, 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(List<String[]> 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条数据,可根据实际需求调整List<String[]> 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 CSVExportUtil<T> {/*** 导出CSV文件。** @param dataList       需要导出的数据列表(泛型对象列表)* @param validationRulesMap 校验和替换规则映射(键为列索引,值为校验和替换规则的映射)* @param headers         CSV表头数组* @param fileName        导出CSV文件的名称* @throws IOException 如果在写入文件时发生错误*/public void exportCSV(List<T> dataList, Map<Integer, Map<String, String>> validationRulesMap, String[] headers, String fileName) throws IOException {// 预处理数据(校验和替换)List<String[]> preprocessedData = preprocessData(dataList, validationRulesMap);// 写入CSV文件writeCSV(preprocessedData, headers, fileName);}/*** 预处理数据(校验和替换)。** @param dataList       数据列表(泛型对象列表)* @param validationRulesMap 校验和替换规则映射(键为列索引,值为校验和替换规则的映射)* @return 预处理后的数据(字符串数组列表)*/private List<String[]> preprocessData(List<T> dataList, Map<Integer, Map<String, String>> validationRulesMap) {List<String[]> 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, Map<Integer, Map<String, String>> rulesMap) {for (Map.Entry<Integer, Map<String, String>> entry : rulesMap.entrySet()) {int columnIndex = entry.getKey();Map<String, 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(List<String[]> 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.zhongyajixie.com/news/61341.html

相关文章:

  • 网站的关键词排名中国新冠一共死去的人数
  • WordPress在线播放禁止下载宁波企业seo服务
  • 网站做qq微信微博登录域名批量注册查询
  • 上海做网站设计公司网站标题算关键词优化吗
  • 狮岭做网站中国第三波疫情将在9月份
  • 温州网站建设模板百度竞价开户公司
  • 房价下跌最惨10大城市黑帽seo论坛
  • 知名网站制作公司青岛分公司深圳整站全网推广
  • 山东教育行业网站建设推荐搜索风云排行榜
  • 广州互帮物流哪家公司做的网站seo排名如何优化
  • 做网站 ecs 虚拟主机百度品牌广告
  • 商丘网站建设.com站长工具seo排名查询
  • 合肥中小型企业网站建设方案模板企业邮箱怎么开通注册
  • 网站建设公司华网天下买赠两年网络营销策划的基本原则是什么
  • 移动端网站生成器网络推广搜索引擎
  • 在线购物系统的分析与设计重庆seo代理计费
  • 新网站建设平台找客户的软件有哪些
  • 织梦网站地图如何做韶关新闻最新今日头条
  • 长春做网站建设的公司山东seo百度推广
  • 网站长尾词怎么做成都最新热门事件
  • 包装设计网站欣赏免费行情软件网站大全
  • 文化管 网站建设规划今日头条新闻推荐
  • 深圳十大网站建设nba在线直播免费观看直播
  • 我的网站模板下载江苏网站建设制作
  • wordpress nofollow插件郑州做网络优化的公司
  • 网站备案密码怎么找回搜索引擎排名优化方法
  • wordpress留言版苏州seo按天扣费
  • 网站策划素材免费建站免费网站
  • 网站制作咨询电话seo外包方案
  • 专业信息门户网站建设石家庄网站建设方案优化