Browse Source

Merge pull request #3844 from alibaba/developing

Developing
pull/3875/head v4.0.1
Jiaju Zhuang 5 months ago committed by GitHub
parent
commit
ea64d8a4d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      .github/workflows/ci.yml
  2. 2
      README.md
  3. 4
      easyexcel-core/pom.xml
  4. 136
      easyexcel-core/src/main/java/com/alibaba/excel/util/EasyExcelTempFileCreationStrategy.java
  5. 13
      easyexcel-core/src/main/java/com/alibaba/excel/util/FileUtils.java
  6. 12
      easyexcel-support/pom.xml
  7. 2
      easyexcel-test/pom.xml
  8. 1
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatData.java
  9. 14
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatTest.java
  10. 10
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java
  11. 3
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/TempLargeDataTest.java
  12. BIN
      easyexcel-test/src/test/resources/dataformat/dataformat.xls
  13. BIN
      easyexcel-test/src/test/resources/dataformat/dataformat.xlsx
  14. 13
      pom.xml
  15. 5
      update.md

6
.github/workflows/ci.yml

@ -37,7 +37,7 @@ jobs:
java-version: ${{ matrix.java }} java-version: ${{ matrix.java }}
distribution: ${{ matrix.distribution }} distribution: ${{ matrix.distribution }}
- name: Cache local Maven repository - name: Cache local Maven repository
uses: actions/cache@v3 uses: actions/cache@main
with: with:
path: ~/.m2/repository path: ~/.m2/repository
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
@ -46,7 +46,11 @@ jobs:
- name: Chmod - name: Chmod
run: chmod +x mvnw run: chmod +x mvnw
- name: Test with Maven - name: Test with Maven
if: ${{ matrix.java == '8' }}
run: ./mvnw test -B -Dmaven.test.skip=false run: ./mvnw test -B -Dmaven.test.skip=false
- name: Test with Maven
if: ${{ matrix.java != '8' }}
run: ./mvnw test -B -Dmaven.test.skip=false -DargLine="--add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/sun.reflect.annotation=ALL-UNNAMED"
- name: Maven Build - name: Maven Build
run: ./mvnw install -B -V run: ./mvnw install -B -V
- name: Java Doc - name: Java Doc

2
README.md

@ -33,7 +33,7 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>4.0.0</version> <version>4.0.1</version>
</dependency> </dependency>
``` ```

4
easyexcel-core/pom.xml

@ -36,9 +36,5 @@
<groupId>org.ehcache</groupId> <groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId> <artifactId>ehcache</artifactId>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

136
easyexcel-core/src/main/java/com/alibaba/excel/util/EasyExcelTempFileCreationStrategy.java

@ -0,0 +1,136 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package com.alibaba.excel.util;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.poi.util.DefaultTempFileCreationStrategy;
import org.apache.poi.util.TempFileCreationStrategy;
import static org.apache.poi.util.TempFile.JAVA_IO_TMPDIR;
/**
* In the scenario where `poifiles` are cleaned up, the {@link DefaultTempFileCreationStrategy} will throw a
* java.nio.file.NoSuchFileException. Therefore, it is necessary to verify the existence of the temporary file every
* time it is created.
*
* @author Jiaju Zhuang
*/
public class EasyExcelTempFileCreationStrategy implements TempFileCreationStrategy {
/**
* Name of POI files directory in temporary directory.
*/
public static final String POIFILES = "poifiles";
/**
* To use files.deleteOnExit after clean JVM exit, set the <code>-Dpoi.delete.tmp.files.on.exit</code> JVM property
*/
public static final String DELETE_FILES_ON_EXIT = "poi.delete.tmp.files.on.exit";
/**
* The directory where the temporary files will be created (<code>null</code> to use the default directory).
*/
private volatile File dir;
/**
* The lock to make dir initialized only once.
*/
private final Lock dirLock = new ReentrantLock();
/**
* Creates the strategy so that it creates the temporary files in the default directory.
*
* @see File#createTempFile(String, String)
*/
public EasyExcelTempFileCreationStrategy() {
this(null);
}
/**
* Creates the strategy allowing to set the
*
* @param dir The directory where the temporary files will be created (<code>null</code> to use the default
* directory).
* @see Files#createTempFile(Path, String, String, FileAttribute[])
*/
public EasyExcelTempFileCreationStrategy(File dir) {
this.dir = dir;
}
private void createPOIFilesDirectory() throws IOException {
// Create our temp dir only once by double-checked locking
// The directory is not deleted, even if it was created by this TempFileCreationStrategy
if (dir == null || !dir.exists()) {
dirLock.lock();
try {
if (dir == null || !dir.exists()) {
String tmpDir = System.getProperty(JAVA_IO_TMPDIR);
if (tmpDir == null) {
throw new IOException("System's temporary directory not defined - set the -D" + JAVA_IO_TMPDIR
+ " jvm property!");
}
Path dirPath = Paths.get(tmpDir, POIFILES);
dir = Files.createDirectories(dirPath).toFile();
}
} finally {
dirLock.unlock();
}
return;
}
}
@Override
public File createTempFile(String prefix, String suffix) throws IOException {
// Identify and create our temp dir, if needed
createPOIFilesDirectory();
// Generate a unique new filename
File newFile = Files.createTempFile(dir.toPath(), prefix, suffix).toFile();
// Set the delete on exit flag, but only when explicitly disabled
if (System.getProperty(DELETE_FILES_ON_EXIT) != null) {
newFile.deleteOnExit();
}
// All done
return newFile;
}
/* (non-JavaDoc) Created directory path is <JAVA_IO_TMPDIR>/poifiles/prefix0123456789 */
@Override
public File createTempDirectory(String prefix) throws IOException {
// Identify and create our temp dir, if needed
createPOIFilesDirectory();
// Generate a unique new filename
File newDirectory = Files.createTempDirectory(dir.toPath(), prefix).toFile();
//this method appears to be only used in tests, so it is probably ok to use deleteOnExit
newDirectory.deleteOnExit();
// All done
return newDirectory;
}
}

13
easyexcel-core/src/main/java/com/alibaba/excel/util/FileUtils.java

@ -12,7 +12,6 @@ import java.util.UUID;
import com.alibaba.excel.exception.ExcelAnalysisException; import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.exception.ExcelCommonException;
import org.apache.poi.util.DefaultTempFileCreationStrategy;
import org.apache.poi.util.TempFile; import org.apache.poi.util.TempFile;
/** /**
@ -111,7 +110,7 @@ public class FileUtils {
/** /**
* Write inputStream to file * Write inputStream to file
* *
* @param file file * @param file file
* @param inputStream inputStream * @param inputStream inputStream
*/ */
public static void writeToFile(File file, InputStream inputStream) { public static void writeToFile(File file, InputStream inputStream) {
@ -121,8 +120,8 @@ public class FileUtils {
/** /**
* Write inputStream to file * Write inputStream to file
* *
* @param file file * @param file file
* @param inputStream inputStream * @param inputStream inputStream
* @param closeInputStream closeInputStream * @param closeInputStream closeInputStream
*/ */
public static void writeToFile(File file, InputStream inputStream, boolean closeInputStream) { public static void writeToFile(File file, InputStream inputStream, boolean closeInputStream) {
@ -154,11 +153,8 @@ public class FileUtils {
} }
} }
public static void createPoiFilesDirectory() { public static void createPoiFilesDirectory() {
File poiFilesPathFile = new File(poiFilesPath); TempFile.setTempFileCreationStrategy(new EasyExcelTempFileCreationStrategy());
createDirectory(poiFilesPathFile);
TempFile.setTempFileCreationStrategy(new DefaultTempFileCreationStrategy(poiFilesPathFile));
} }
public static File createCacheTmpFile() { public static File createCacheTmpFile() {
@ -171,7 +167,6 @@ public class FileUtils {
} }
/** /**
*
* @param directory * @param directory
*/ */
public static File createDirectory(File directory) { public static File createDirectory(File directory) {

12
easyexcel-support/pom.xml

@ -24,8 +24,8 @@
<version>5.3.37</version> <version>5.3.37</version>
<exclusions> <exclusions>
<exclusion> <exclusion>
<groupId>org.springframework</groupId> <groupId>*</groupId>
<artifactId>spring-jcl</artifactId> <artifactId>*</artifactId>
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
@ -49,7 +49,7 @@
<createDependencyReducedPom>true</createDependencyReducedPom> <createDependencyReducedPom>true</createDependencyReducedPom>
<!-- Make sure the transitive dependencies are written to the generated pom under <dependencies> --> <!-- Make sure the transitive dependencies are written to the generated pom under <dependencies> -->
<promoteTransitiveDependencies>true</promoteTransitiveDependencies> <promoteTransitiveDependencies>true</promoteTransitiveDependencies>
<artifactSet combine.self="override"> <artifactSet>
<includes> <includes>
<include>org.springframework:spring-core</include> <include>org.springframework:spring-core</include>
</includes> </includes>
@ -63,6 +63,12 @@
</includes> </includes>
</filter> </filter>
</filters> </filters>
<relocations>
<relocation>
<pattern>org.springframework</pattern>
<shadedPattern>com.alibaba.excel.support</shadedPattern>
</relocation>
</relocations>
</configuration> </configuration>
</execution> </execution>
</executions> </executions>

2
easyexcel-test/pom.xml

@ -74,7 +74,7 @@
<includes> <includes>
<include>/com/alibaba/easyexcel/test/core/**/*.java</include> <include>/com/alibaba/easyexcel/test/core/**/*.java</include>
</includes> </includes>
<testFailureIgnore>true</testFailureIgnore> <testFailureIgnore>false</testFailureIgnore>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

1
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatData.java

@ -13,6 +13,7 @@ import lombok.Setter;
public class DateFormatData { public class DateFormatData {
private String date; private String date;
private String dateStringCn; private String dateStringCn;
private String dateStringCn2;
private String dateStringUs; private String dateStringUs;
private String number; private String number;
private String numberStringCn; private String numberStringCn;

14
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/dataformat/DateFormatTest.java

@ -3,6 +3,7 @@ package com.alibaba.easyexcel.test.core.dataformat;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
@ -46,15 +47,22 @@ public class DateFormatTest {
List<DateFormatData> list = List<DateFormatData> list =
EasyExcel.read(file, DateFormatData.class, null).locale(Locale.CHINA).sheet().doReadSync(); EasyExcel.read(file, DateFormatData.class, null).locale(Locale.CHINA).sheet().doReadSync();
for (DateFormatData data : list) { for (DateFormatData data : list) {
if (data.getDateStringCn() != null && !data.getDateStringCn().equals(data.getDate())) { if (!Objects.equals(data.getDateStringCn(), data.getDate()) && !Objects.equals(data.getDateStringCn2(),
log.info("date:cn:{},{}", data.getDateStringCn(), data.getDate()); data.getDate())) {
log.info("date:cn:{},{},{}", data.getDateStringCn(), data.getDateStringCn2(), data.getDate());
} }
if (data.getNumberStringCn() != null && !data.getNumberStringCn().equals(data.getNumber())) { if (data.getNumberStringCn() != null && !data.getNumberStringCn().equals(data.getNumber())) {
log.info("number:cn{},{}", data.getNumberStringCn(), data.getNumber()); log.info("number:cn{},{}", data.getNumberStringCn(), data.getNumber());
} }
} }
for (DateFormatData data : list) { for (DateFormatData data : list) {
Assertions.assertEquals(data.getDateStringCn(), data.getDate()); // The way dates are read in Chinese is different on Linux and Mac, so it is acceptable if it matches
// either one.
// For example, on Linux: 1-Jan -> 1-1月
// On Mac: 1-Jan -> 1-一月
Assertions.assertTrue(
Objects.equals(data.getDateStringCn(), data.getDate()) || Objects.equals(data.getDateStringCn2(),
data.getDate()));
Assertions.assertEquals(data.getNumberStringCn(), data.getNumber()); Assertions.assertEquals(data.getNumberStringCn(), data.getNumber());
} }
} }

10
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteV33Test.java

@ -1,5 +1,8 @@
package com.alibaba.easyexcel.test.temp; package com.alibaba.easyexcel.test.temp;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -135,4 +138,11 @@ public class WriteV33Test {
return list; return list;
} }
@Test
public void test4() throws Exception{
Path path= Files.createTempFile(new File("/Users/zhuangjiaju/test/test0422/test/xx").toPath(),System.currentTimeMillis()+"",".jpg");
System.out.println(path);
}
} }

3
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/TempLargeDataTest.java

@ -8,7 +8,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import com.alibaba.easyexcel.test.core.large.LargeDataTest;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
@ -31,7 +30,7 @@ import org.slf4j.LoggerFactory;
public class TempLargeDataTest { public class TempLargeDataTest {
private static final Logger LOGGER = LoggerFactory.getLogger(LargeDataTest.class); private static final Logger LOGGER = LoggerFactory.getLogger(TempLargeDataTest.class);
private int i = 0; private int i = 0;
private static File fileFill07; private static File fileFill07;

BIN
easyexcel-test/src/test/resources/dataformat/dataformat.xls

Binary file not shown.

BIN
easyexcel-test/src/test/resources/dataformat/dataformat.xlsx

Binary file not shown.

13
pom.xml

@ -20,7 +20,7 @@
<properties> <properties>
<revision>4.0.0</revision> <revision>4.0.1</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.8</jdk.version> <jdk.version>1.8</jdk.version>
<gpg.skip>true</gpg.skip> <gpg.skip>true</gpg.skip>
@ -113,11 +113,6 @@
<artifactId>ehcache</artifactId> <artifactId>ehcache</artifactId>
<version>3.9.11</version> <version>3.9.11</version>
</dependency> </dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>
@ -190,12 +185,12 @@
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version> <version>3.3.0</version>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId> <artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version> <version>3.6.0</version>
</plugin> </plugin>
<plugin> <plugin>
<groupId>org.codehaus.mojo</groupId> <groupId>org.codehaus.mojo</groupId>
@ -235,11 +230,13 @@
<exclude>com/alibaba/excel/metadata/DataFormatter.java</exclude> <exclude>com/alibaba/excel/metadata/DataFormatter.java</exclude>
<exclude>com/alibaba/excel/util/DateUtils.java</exclude> <exclude>com/alibaba/excel/util/DateUtils.java</exclude>
<exclude>com/alibaba/excel/util/MapUtils.java</exclude> <exclude>com/alibaba/excel/util/MapUtils.java</exclude>
<exclude>com/alibaba/excel/util/EasyExcelTempFileCreationStrategy.java</exclude>
<exclude>com/alibaba/excel/metadata/format/DataFormatter.java</exclude> <exclude>com/alibaba/excel/metadata/format/DataFormatter.java</exclude>
<exclude>com/alibaba/excel/metadata/format/ExcelGeneralNumberFormat.java</exclude> <exclude>com/alibaba/excel/metadata/format/ExcelGeneralNumberFormat.java</exclude>
<exclude>com/alibaba/excel/metadata/csv/CsvDataFormat.java</exclude> <exclude>com/alibaba/excel/metadata/csv/CsvDataFormat.java</exclude>
<exclude>com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java</exclude> <exclude>com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java</exclude>
<exclude>com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java</exclude> <exclude>com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java</exclude>
<exclude>com/alibaba/excel/write/executor/ExcelWriteFillExecutor.java</exclude>
</excludes> </excludes>
</configuration> </configuration>
<executions> <executions>

5
update.md

@ -1,3 +1,8 @@
# 4.0.1
* `commons-io` 修改为依赖 `poi`的版本
* 修复临时目录被清理可能提示`NoSuchFileException`的异常
# 4.0.0 # 4.0.0
* `poi`由`4.1.2`升级到`5.2.5` * `poi`由`4.1.2`升级到`5.2.5`

Loading…
Cancel
Save