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

17网站一起做网店池尾网站如何交换链接

17网站一起做网店池尾,网站如何交换链接,网推方案,网页视频下载方法手机java8循环追加Excel数据 实际遇到问题:定期获取zip文件,zip文件内有几个固定模板的Excel文件,有的Excel文件可能还包含多个sheet。 有段时间一次性获取到好几个zip包,需要将这些包都解压,并且按照不同的文件名、sheet进…

java8循环追加Excel数据


实际遇到问题:定期获取zip文件,zip文件内有几个固定模板的Excel文件,有的Excel文件可能还包含多个sheet。
有段时间一次性获取到好几个zip包,需要将这些包都解压,并且按照不同的文件名、sheet进行数据整合到一个sheet-Excel中。

![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/2f20c7a800c645f19d2f5e7d8d2edf01.png
在这里插入图片描述

可以在 pom.xml 中添加以下依赖:

<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.3</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.2.3</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>4.1.2</version>
</dependency>
<dependency><groupId>org.apache.xmlbeans</groupId><artifactId>xmlbeans</artifactId><version>5.1.1</version>
</dependency>

import org.apache.poi.ss.usermodel.*;import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Collections;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;public class ZipExcelMerger {public static void main(String[] args) {// 原始zip文件路径String zipDirectory = "path/to/zip/files";// 解压zip文件至新的路径String outputDirectory = "path/to/output";File dir = new File(zipDirectory);File[] zipFiles = dir.listFiles((d, name) -> name.endsWith(".zip"));if (zipFiles != null) {// 升序Collections.sort(Arrays.asList(zipDirectory));for (File zipFile : zipFiles) {unzipAndMerge(zipFile, outputDirectory);}}}private static void unzipAndMerge(File zipFile, String outputDirectory) {try (ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile))) {ZipEntry entry;while ((entry = zis.getNextEntry()) != null) {if (!entry.isDirectory() && (entry.getName().endsWith(".xls") || entry.getName().endsWith(".xlsx"))) {File outputFile = new File(outputDirectory, entry.getName());byte[] buffer = new byte[1024];ByteArrayOutputStream baos = new ByteArrayOutputStream();int len;while ((len = zis.read(buffer)) > 0) {baos.write(buffer, 0, len);}baos.close();if (outputFile.exists()) {mergeExcelFiles(outputFile, new ByteArrayInputStream(baos.toByteArray()));} else {saveToFile(outputFile, new ByteArrayInputStream(baos.toByteArray()));}}}} catch (IOException e) {e.printStackTrace();}}private static void mergeExcelFiles(File existingFile, InputStream newFileStream) {File tempFile = null;try {// 创建临时文件tempFile = File.createTempFile("temp", ".xlsx");Files.copy(existingFile.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);// 读取临时文件Workbook existingWorkbook;try (InputStream is = new FileInputStream(tempFile)) {existingWorkbook = WorkbookFactory.create(is);}// 读取新文件try (Workbook newWorkbook = createWorkbook(newFileStream, existingFile.getName())) {for (int i = 0; i < newWorkbook.getNumberOfSheets(); i++) {Sheet newSheet = newWorkbook.getSheetAt(i);Sheet existingSheet = existingWorkbook.getSheet(newSheet.getSheetName());if (existingSheet != null) {for (int j = 1; j <= newSheet.getLastRowNum(); j++) {Row newRow = newSheet.getRow(j);if (newRow != null) {Row existingRow = existingSheet.createRow(existingSheet.getLastRowNum() + 1);copyRow(newRow, existingRow);}}}}}// 将合并后的内容写回临时文件try (OutputStream os = new FileOutputStream(tempFile)) {existingWorkbook.write(os);}// 用临时文件替换原文件Files.move(tempFile.toPath(), existingFile.toPath(), StandardCopyOption.REPLACE_EXISTING);} catch (IOException e) {e.printStackTrace();} finally {// 删除临时文件if (tempFile != null && tempFile.exists()) {tempFile.delete();}}}private static Workbook createWorkbook(InputStream inputStream, String fileName) throws IOException {if (fileName.endsWith(".xlsx")) {return WorkbookFactory.create(inputStream);} else if (fileName.endsWith(".xls")) {return WorkbookFactory.create(inputStream);} else {throw new IllegalArgumentException("Unsupported file format: " + fileName);}}private static void saveToFile(File file, InputStream inputStream) {try (FileOutputStream fos = new FileOutputStream(file)) {byte[] buffer = new byte[1024];int len;while ((len = inputStream.read(buffer)) > 0) {fos.write(buffer, 0, len);}} catch (IOException e) {e.printStackTrace();}}private static void copyRow(Row sourceRow, Row targetRow) {for (int i = 0; i < sourceRow.getLastCellNum(); i++) {Cell sourceCell = sourceRow.getCell(i);if (sourceCell != null) {Cell targetCell = targetRow.createCell(i);targetCell.setCellValue(sourceCell.toString());}}}
}

文章转载自:

http://G2WaVqzk.cnLmp.cn
http://RUR1BAox.cnLmp.cn
http://Sjuke3Pj.cnLmp.cn
http://UaQ3KyXF.cnLmp.cn
http://aU3D04BX.cnLmp.cn
http://BTzFWQCe.cnLmp.cn
http://D1gy5VoZ.cnLmp.cn
http://5d3iz8AJ.cnLmp.cn
http://Ak05LzJq.cnLmp.cn
http://V091bJ8E.cnLmp.cn
http://0FbymYUF.cnLmp.cn
http://UJ0rHKR0.cnLmp.cn
http://iXmWIPF6.cnLmp.cn
http://n0LUlKLq.cnLmp.cn
http://Aq143WhF.cnLmp.cn
http://9tLbc2lX.cnLmp.cn
http://G0Njs2aR.cnLmp.cn
http://AJ1kSWOI.cnLmp.cn
http://SIHw0AUG.cnLmp.cn
http://2zg3WBtt.cnLmp.cn
http://CbY01R39.cnLmp.cn
http://EeF8dJcQ.cnLmp.cn
http://azULfiWl.cnLmp.cn
http://jTKqXfz2.cnLmp.cn
http://0Z7ZgVqg.cnLmp.cn
http://2YMN5Pd5.cnLmp.cn
http://DUuUYGON.cnLmp.cn
http://lYlt5Qyk.cnLmp.cn
http://AN3uHLov.cnLmp.cn
http://oFvsRt3x.cnLmp.cn
http://www.dtcms.com/wzjs/715842.html

相关文章:

  • 做外贸网站平台有哪些内容做html的软件
  • 电子商城网站建设成都房地产公司排名
  • 写网站的教程wordpress主页图片
  • 网站空间域名购买网站开发的技术要求
  • ui外包网站seo网站优化详解
  • 湖南岳阳网站建设公司黄页顺企网怎么做网页别人可以看到图片
  • 安防公司手机网站网站开发电商
  • 做网站一个程序员够吗h5case是什么网站
  • 网站开发数据流程图怎么画毕业设计模板网站
  • 小程序公司排名孔宇seo
  • 东莞市小程序定制开发丨网站建设小程序跳转到网站
  • 深圳网站-建设信科网络做外贸网站注意
  • 珠海网站建设知识云服务器防御
  • 培训机构网站建设推广网站推广策划方案毕业设计
  • 保险网站定制wordpress在centos
  • WordPress黑镜主题下载seo网站推广 杭州
  • 好听的公司名称福建搜索引擎优化
  • 上海网站seo外包wordpress 微信 登陆地址
  • 河南企业网站定制涟水住房和城乡建设局网站
  • 西南能矿建设工程公司网站濮阳网格化app
  • 如何用ppt形式做网站北京天津网站建设公司
  • 接活做图网站c2c网站 多钱
  • 专业网站建设是哪家好广东做网站的公司有哪些
  • 免费建设商城网站网站微商城的建设
  • 昌平手机网站建设极简瀑布流wordpress图片主题
  • 响应式培训网站模板下载网站开发招标文件
  • 买了域名之后怎么做网站水利工程建设监理网站
  • 张家港外贸型网站制作策划公司起名
  • 江苏省住房和城乡建设厅 官方网站网页版微信是什么意思
  • 部队网站源码湛江小程序设计公司