forked from fanruan/easyexcel
zhuangjiaju
5 years ago
20 changed files with 529 additions and 17 deletions
@ -1,18 +1,40 @@
|
||||
package com.alibaba.excel.converters; |
||||
|
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
|
||||
/** |
||||
* Converter unique key |
||||
* Converter unique key.Consider that you can just use class as the key. |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class ConverterKeyBuild { |
||||
|
||||
private static final Map<String, String> BOXING_MAP = new HashMap<String, String>(16); |
||||
|
||||
static { |
||||
BOXING_MAP.put(int.class.getName(), Integer.class.getName()); |
||||
BOXING_MAP.put(byte.class.getName(), Byte.class.getName()); |
||||
BOXING_MAP.put(long.class.getName(), Long.class.getName()); |
||||
BOXING_MAP.put(double.class.getName(), Double.class.getName()); |
||||
BOXING_MAP.put(float.class.getName(), Float.class.getName()); |
||||
BOXING_MAP.put(char.class.getName(), Character.class.getName()); |
||||
BOXING_MAP.put(short.class.getName(), Short.class.getName()); |
||||
BOXING_MAP.put(boolean.class.getName(), Boolean.class.getName()); |
||||
} |
||||
|
||||
public static String buildKey(Class clazz) { |
||||
return clazz.getName(); |
||||
String className = clazz.getName(); |
||||
String boxingClassName = BOXING_MAP.get(clazz.getName()); |
||||
if (boxingClassName == null) { |
||||
return className; |
||||
} |
||||
return boxingClassName; |
||||
} |
||||
|
||||
public static String buildKey(Class clazz, CellDataTypeEnum cellDataTypeEnum) { |
||||
return clazz.getName() + "-" + cellDataTypeEnum.toString(); |
||||
return buildKey(clazz) + "-" + cellDataTypeEnum.toString(); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,41 @@
|
||||
package com.alibaba.excel.converters.bytearray; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
|
||||
/** |
||||
* Boxing Byte array and image converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class BoxingByteArrayImageConverter implements Converter<Byte[]> { |
||||
@Override |
||||
public Class supportJavaTypeKey() { |
||||
return Byte[].class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.IMAGE; |
||||
} |
||||
|
||||
@Override |
||||
public Byte[] convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
throw new UnsupportedOperationException("Cannot convert images to byte arrays"); |
||||
} |
||||
|
||||
@Override |
||||
public CellData convertToExcelData(Byte[] value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
byte[] byteValue = new byte[value.length]; |
||||
for (int i = 0; i < value.length; i++) { |
||||
byteValue[i] = value[i]; |
||||
} |
||||
return new CellData(byteValue); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,37 @@
|
||||
package com.alibaba.excel.converters.bytearray; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
|
||||
/** |
||||
* Byte array and image converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class ByteArrayImageConverter implements Converter<byte[]> { |
||||
@Override |
||||
public Class supportJavaTypeKey() { |
||||
return byte[].class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.IMAGE; |
||||
} |
||||
|
||||
@Override |
||||
public byte[] convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
throw new UnsupportedOperationException("Cannot convert images to byte arrays"); |
||||
} |
||||
|
||||
@Override |
||||
public CellData convertToExcelData(byte[] value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
return new CellData(value); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,41 @@
|
||||
package com.alibaba.excel.converters.file; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
import com.alibaba.excel.util.FileUtils; |
||||
|
||||
/** |
||||
* File and image converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class FileImageConverter implements Converter<File> { |
||||
@Override |
||||
public Class supportJavaTypeKey() { |
||||
return File.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.IMAGE; |
||||
} |
||||
|
||||
@Override |
||||
public File convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
throw new UnsupportedOperationException("Cannot convert images to file"); |
||||
} |
||||
|
||||
@Override |
||||
public CellData convertToExcelData(File value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) throws IOException { |
||||
return new CellData(FileUtils.readFileToByteArray(value)); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,41 @@
|
||||
package com.alibaba.excel.converters.inputstream; |
||||
|
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
import com.alibaba.excel.util.IOUtils; |
||||
|
||||
/** |
||||
* File and image converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class InputStreamImageConverter implements Converter<InputStream> { |
||||
@Override |
||||
public Class supportJavaTypeKey() { |
||||
return InputStream.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.IMAGE; |
||||
} |
||||
|
||||
@Override |
||||
public InputStream convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
throw new UnsupportedOperationException("Cannot convert images to input stream"); |
||||
} |
||||
|
||||
@Override |
||||
public CellData convertToExcelData(InputStream value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) throws IOException { |
||||
return new CellData(IOUtils.toByteArray(value)); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,41 @@
|
||||
package com.alibaba.excel.converters.string; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
|
||||
import com.alibaba.excel.converters.Converter; |
||||
import com.alibaba.excel.enums.CellDataTypeEnum; |
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.GlobalConfiguration; |
||||
import com.alibaba.excel.metadata.property.ExcelContentProperty; |
||||
import com.alibaba.excel.util.FileUtils; |
||||
|
||||
/** |
||||
* String and image converter |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class StringImageConverter implements Converter<String> { |
||||
@Override |
||||
public Class supportJavaTypeKey() { |
||||
return String.class; |
||||
} |
||||
|
||||
@Override |
||||
public CellDataTypeEnum supportExcelTypeKey() { |
||||
return CellDataTypeEnum.IMAGE; |
||||
} |
||||
|
||||
@Override |
||||
public String convertToJavaData(CellData cellData, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) { |
||||
throw new UnsupportedOperationException("Cannot convert images to string"); |
||||
} |
||||
|
||||
@Override |
||||
public CellData convertToExcelData(String value, ExcelContentProperty contentProperty, |
||||
GlobalConfiguration globalConfiguration) throws IOException { |
||||
return new CellData(FileUtils.readFileToByteArray(new File(value))); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,85 @@
|
||||
package com.alibaba.excel.util; |
||||
|
||||
import java.io.ByteArrayOutputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.io.OutputStream; |
||||
|
||||
/** |
||||
* IO Utils |
||||
* |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
public class IOUtils { |
||||
public static final int EOF = -1; |
||||
/** |
||||
* The default buffer size ({@value}) to use for |
||||
*/ |
||||
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; |
||||
|
||||
/** |
||||
* Gets the contents of an InputStream as a byte[]. |
||||
* |
||||
* @param input |
||||
* @return |
||||
* @throws IOException |
||||
*/ |
||||
public static byte[] toByteArray(final InputStream input) throws IOException { |
||||
final ByteArrayOutputStream output = new ByteArrayOutputStream(); |
||||
try { |
||||
copy(input, output); |
||||
return output.toByteArray(); |
||||
} finally { |
||||
output.toByteArray(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Gets the contents of an InputStream as a byte[]. |
||||
* |
||||
* @param input |
||||
* @param size |
||||
* @return |
||||
* @throws IOException |
||||
*/ |
||||
public static byte[] toByteArray(final InputStream input, final int size) throws IOException { |
||||
if (size < 0) { |
||||
throw new IllegalArgumentException("Size must be equal or greater than zero: " + size); |
||||
} |
||||
if (size == 0) { |
||||
return new byte[0]; |
||||
} |
||||
final byte[] data = new byte[size]; |
||||
int offset = 0; |
||||
int read; |
||||
while (offset < size && (read = input.read(data, offset, size - offset)) != EOF) { |
||||
offset += read; |
||||
} |
||||
if (offset != size) { |
||||
throw new IOException("Unexpected read size. current: " + offset + ", expected: " + size); |
||||
} |
||||
return data; |
||||
} |
||||
|
||||
/** |
||||
* Copies bytes |
||||
* |
||||
* @param input |
||||
* @param output |
||||
* @return |
||||
* @throws IOException |
||||
*/ |
||||
public static int copy(final InputStream input, final OutputStream output) throws IOException { |
||||
long count = 0; |
||||
int n; |
||||
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; |
||||
while (EOF != (n = input.read(buffer))) { |
||||
output.write(buffer, 0, n); |
||||
count += n; |
||||
} |
||||
if (count > Integer.MAX_VALUE) { |
||||
return -1; |
||||
} |
||||
return (int)count; |
||||
} |
||||
} |
@ -0,0 +1,25 @@
|
||||
package com.alibaba.easyexcel.test.core.converter; |
||||
|
||||
import java.io.File; |
||||
import java.io.InputStream; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.annotation.write.style.ColumnWidth; |
||||
import com.alibaba.excel.annotation.write.style.ContentRowHeight; |
||||
import com.alibaba.excel.converters.string.StringImageConverter; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author Jiaju Zhuang |
||||
*/ |
||||
@Data |
||||
@ContentRowHeight(500) |
||||
@ColumnWidth(500 / 8) |
||||
public class ImageData { |
||||
private File file; |
||||
private InputStream inputStream; |
||||
@ExcelProperty(converter = StringImageConverter.class) |
||||
private String string; |
||||
private byte[] byteArray; |
||||
} |
@ -0,0 +1,20 @@
|
||||
package com.alibaba.easyexcel.test.temp; |
||||
|
||||
import java.util.List; |
||||
|
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* |
||||
* @author Jiaju Zhuang |
||||
**/ |
||||
@Data |
||||
public class StyleData { |
||||
private byte[] byteValue; |
||||
private Byte[] byteValue2; |
||||
private byte byteValue1; |
||||
private Byte byteValue4; |
||||
private byte byteValue3; |
||||
private String[] ss; |
||||
private List<String> s1s; |
||||
} |
After Width: | Height: | Size: 12 KiB |
Loading…
Reference in new issue