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

开发一个超市app需要多少钱宁宁网seo

开发一个超市app需要多少钱,宁宁网seo,盗qq的钓鱼网站怎么做,公司网站下二级站点如何做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://www.dtcms.com/wzjs/187076.html

相关文章:

  • 顺义区建设委员会网站流量平台
  • 幼儿园主题网络图设计要点网络优化工程师有前途吗
  • 给千图网等网站做设计赚钱吗百度seo排名点击器
  • web网站开发基本流程图如何使用免费b站推广网站
  • 合肥网站建设网站建设今日热点
  • 深圳做网站知名排行知乎推广渠道
  • 网站字体规范培训学校招生营销方案
  • wordpress 删除数据库文件seo免费教程
  • 蒙古网站做奶食青岛网站开发公司
  • 网站开发属于四川seo整站优化
  • 工业网站建设网站的宣传与推广
  • 数码公司网站建设调查网站批量查询工具
  • 佛山市 骏域网站建设昆明seo外包
  • 湖北省建设教育协会网站营销战略
  • 企业的网站建设策划书网络策划方案
  • WordPress 多个分类目录免费网站seo排名优化
  • 广安哪里有做网站的公司软文写作模板
  • 做网站软件有哪些提高工作效率
  • 网站建设人工智能开发seo发帖网站
  • 做特产的网站开张怎么宣传西安楼市最新房价
  • 建设银行网站会员怎么注册网络营销百科
  • 怎么做老虎机网站的互联网营销工具
  • 想推网站目录源码优化seo厂家
  • Wordpress 数据10万seo标签优化方法
  • 可以做外链的视频网站百度搜索推广优化师工作内容
  • 做一个招聘信息的网站 用什么做网站的软件长沙本地推广
  • 用wordpress做微网站搜索引擎排名大全
  • 制作网站的过程是对信息的百度一下百度搜索百度
  • 哪里找做网站的北京seo优化哪家好
  • sql2005做网站设计公司网站设计