|
|
@ -4,37 +4,46 @@ import java.io.BufferedInputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.InputStream; |
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
|
|
|
|
|
|
|
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 com.alibaba.excel.read.metadata.ReadWorkbook; |
|
|
|
import com.alibaba.excel.read.metadata.ReadWorkbook; |
|
|
|
import com.alibaba.excel.util.StringUtils; |
|
|
|
import com.alibaba.excel.util.StringUtils; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import lombok.Getter; |
|
|
|
import org.apache.poi.poifs.filesystem.FileMagic; |
|
|
|
import org.apache.poi.poifs.filesystem.FileMagic; |
|
|
|
|
|
|
|
import org.apache.poi.util.IOUtils; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @author jipengfei |
|
|
|
* @author jipengfei |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
@Getter |
|
|
|
public enum ExcelTypeEnum { |
|
|
|
public enum ExcelTypeEnum { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* csv |
|
|
|
* csv |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
CSV(".csv"), |
|
|
|
CSV(".csv", new byte[] {-27, -89, -109, -27}), |
|
|
|
/** |
|
|
|
/** |
|
|
|
* xls |
|
|
|
* xls |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
XLS(".xls"), |
|
|
|
XLS(".xls", new byte[] {-48, -49, 17, -32, -95, -79, 26, -31}), |
|
|
|
/** |
|
|
|
/** |
|
|
|
* xlsx |
|
|
|
* xlsx |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
XLSX(".xlsx"); |
|
|
|
XLSX(".xlsx", new byte[] {80, 75, 3, 4}); |
|
|
|
|
|
|
|
|
|
|
|
private String value; |
|
|
|
final String value; |
|
|
|
|
|
|
|
final byte[] magic; |
|
|
|
|
|
|
|
|
|
|
|
ExcelTypeEnum(String value) { |
|
|
|
ExcelTypeEnum(String value, byte[] magic) { |
|
|
|
this.setValue(value); |
|
|
|
this.value = value; |
|
|
|
|
|
|
|
this.magic = magic; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final static int MAX_PATTERN_LENGTH = 8; |
|
|
|
|
|
|
|
|
|
|
|
public static ExcelTypeEnum valueOf(ReadWorkbook readWorkbook) { |
|
|
|
public static ExcelTypeEnum valueOf(ReadWorkbook readWorkbook) { |
|
|
|
ExcelTypeEnum excelType = readWorkbook.getExcelType(); |
|
|
|
ExcelTypeEnum excelType = readWorkbook.getExcelType(); |
|
|
|
if (excelType != null) { |
|
|
|
if (excelType != null) { |
|
|
@ -85,22 +94,27 @@ public enum ExcelTypeEnum { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static ExcelTypeEnum recognitionExcelType(InputStream inputStream) throws Exception { |
|
|
|
private static ExcelTypeEnum recognitionExcelType(InputStream inputStream) throws Exception { |
|
|
|
FileMagic fileMagic = FileMagic.valueOf(inputStream); |
|
|
|
// Grab the first bytes of this stream
|
|
|
|
if (FileMagic.OLE2.equals(fileMagic)) { |
|
|
|
byte[] data = IOUtils.peekFirstNBytes(inputStream, MAX_PATTERN_LENGTH); |
|
|
|
return XLS; |
|
|
|
if (findMagic(XLSX.magic, data)) { |
|
|
|
} |
|
|
|
|
|
|
|
if (FileMagic.OOXML.equals(fileMagic)) { |
|
|
|
|
|
|
|
return XLSX; |
|
|
|
return XLSX; |
|
|
|
|
|
|
|
} else if (findMagic(CSV.magic, data)) { |
|
|
|
|
|
|
|
return CSV; |
|
|
|
|
|
|
|
} else if (findMagic(XLS.magic, data)) { |
|
|
|
|
|
|
|
return XLS; |
|
|
|
} |
|
|
|
} |
|
|
|
throw new ExcelCommonException( |
|
|
|
throw new ExcelCommonException( |
|
|
|
"Convert excel format exception.You can try specifying the 'excelType' yourself"); |
|
|
|
"Convert excel format exception.You can try specifying the 'excelType' yourself"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public String getValue() { |
|
|
|
private static boolean findMagic(byte[] expected, byte[] actual) { |
|
|
|
return value; |
|
|
|
int i = 0; |
|
|
|
|
|
|
|
for (byte expectedByte : expected) { |
|
|
|
|
|
|
|
if (actual[i++] != expectedByte && expectedByte != '?') { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void setValue(String value) { |
|
|
|
|
|
|
|
this.value = value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|