diff --git a/README.md b/README.md index 0e775b7..9d271f5 100644 --- a/README.md +++ b/README.md @@ -131,7 +131,7 @@ private List mailAddresses; The CSVColumnValueConverter takes a collection of objects and serializes it to a comma seperated String. Alternately when deserializing, the converter takes a comma seperated String and deserializes it to a collection of Objects. So writing a collection of users will result with a cloumn named "Emails" and the column data will look someting like that: -john@mail.com,danny@mail.com,jerry@mail.com +john@mail.com,danny@mail.com,jerry@mail.com,love3400wind@163.com When reading the sheet to a collection of Users, the column "Emails" will be deserialized to an ArrayList. If you prefer a different collection implementation rather than the default ArrayList, you can always extend the CSVColumnValueConverter and override the getCollection() method to return your preferred implementation. diff --git a/src/main/java/com/ebay/xcelite/Xcelite.java b/src/main/java/com/ebay/xcelite/Xcelite.java index 90b7e9b..f8fad2f 100644 --- a/src/main/java/com/ebay/xcelite/Xcelite.java +++ b/src/main/java/com/ebay/xcelite/Xcelite.java @@ -15,149 +15,208 @@ */ package com.ebay.xcelite; +import com.ebay.xcelite.exceptions.XceliteException; +import com.ebay.xcelite.sheet.XceliteSheet; +import com.ebay.xcelite.sheet.XceliteSheetImpl; +import org.apache.poi.POIXMLDocument; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; + import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; - -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; - -import com.ebay.xcelite.exceptions.XceliteException; -import com.ebay.xcelite.sheet.XceliteSheet; -import com.ebay.xcelite.sheet.XceliteSheetImpl; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PushbackInputStream; /** * Class description... - * + * * @author kharel (kharel@ebay.com) * @creation_date Nov 9, 2013 - * */ public class Xcelite { - private final Workbook workbook; - private File file; - - public Xcelite() { - workbook = new XSSFWorkbook(); - } - - public Xcelite(File file) { - try { - this.file = file; - workbook = new XSSFWorkbook(new FileInputStream(file)); - } catch (FileNotFoundException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new RuntimeException(e); + private final Workbook workbook; + private File file; + + public Xcelite() { + workbook = new XSSFWorkbook(); } - } - - /** - * Creates new sheet. - * - * @return XceliteSheet object - */ - public XceliteSheet createSheet() { - return new XceliteSheetImpl(workbook.createSheet(), file); - } - - /** - * Creates new sheet with specified name. - * - * @param name the sheet name * - * @return XceliteSheet object - */ - public XceliteSheet createSheet(String name) { - return new XceliteSheetImpl(workbook.createSheet(name), file); - } - - /** - * Gets the sheet at the specified index. - * - * @param sheetIndex the sheet index - * @return XceliteSheet object - */ - public XceliteSheet getSheet(int sheetIndex) { - Sheet sheet = workbook.getSheetAt(sheetIndex); - if (sheet == null) { - throw new XceliteException(String.format("Could not find sheet at index %s", sheetIndex)); + + public Xcelite(File file) { + try { + this.file = file; + //workbook = new XSSFWorkbook(new FileInputStream(file)); + workbook = create(new FileInputStream(file)); + + + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException(e); + } } - return new XceliteSheetImpl(sheet, file); - } - - /** - * Gets the sheet with the specified index. - * - * @param sheetIndex the sheet name - * @return XceliteSheet object - */ - public XceliteSheet getSheet(String sheetName) { - Sheet sheet = workbook.getSheet(sheetName); - if (sheet == null) { - throw new XceliteException(String.format("Could not find sheet named \"%s\"", sheetName)); + + /** + * handle exception : Package should contain a content type part [M1.13] + * @param in + * @return + */ + public static Workbook create(InputStream in) { + try { + if (!in.markSupported()) { + in = new PushbackInputStream(in, 8); + } + if (POIFSFileSystem.hasPOIFSHeader(in)) { + return new HSSFWorkbook(in); + } + if (POIXMLDocument.hasOOXMLHeader(in)) { + return new XSSFWorkbook(OPCPackage.open(in)); + } + } catch (InvalidFormatException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + throw new IllegalArgumentException("你的excel版本目前poi解析不了"); + } + + /** + * Creates new sheet. + * + * @return XceliteSheet object + */ + public XceliteSheet createSheet() { + return new XceliteSheetImpl(workbook.createSheet(), file); + } + + /** + * Creates new sheet with specified name. + * + * @param name the sheet name * + * @return XceliteSheet object + */ + public XceliteSheet createSheet(String name) { + return new XceliteSheetImpl(workbook.createSheet(name), file); } - return new XceliteSheetImpl(sheet, file); - } - - /** - * Saves data to the same file given in construction. If no such file - * specified an exception is thrown. - */ - public void write() { - if (file == null) { - throw new XceliteException("No file given in Xcelite object construction. Consider using method write(file)"); + + public XceliteSheet createSheetFromTemplate(String name, String templateFile) { + return new XceliteSheetImpl(workbook.createSheet(name), new File(templateFile)); + } + + /** + * Gets the sheet at the specified index. + * + * @param sheetIndex the sheet index + * @return XceliteSheet object + */ + public XceliteSheet getSheet(int sheetIndex) { + Sheet sheet = workbook.getSheetAt(sheetIndex); + if (sheet == null) { + throw new XceliteException(String.format("Could not find sheet at index %s", sheetIndex)); + } + return new XceliteSheetImpl(sheet, file); } - write(file); - } - - /** - * Saves data to a new file. - * - * @param file the file to save the data into - */ - public void write(File file) { - FileOutputStream out = null; - try { - out = new FileOutputStream(file, false); - workbook.write(out); - } catch (FileNotFoundException e) { - throw new RuntimeException(e); - } catch (IOException e) { - new RuntimeException(e); - } finally { - if (out != null) + + /** + * Gets the sheet with the specified index. + * + * @param sheetIndex the sheet name + * @return XceliteSheet object + */ + public XceliteSheet getSheet(String sheetName) { + Sheet sheet = workbook.getSheet(sheetName); + if (sheet == null) { + throw new XceliteException(String.format("Could not find sheet named \"%s\"", sheetName)); + } + return new XceliteSheetImpl(sheet, file); + } + + /** + * Saves data to the same file given in construction. If no such file + * specified an exception is thrown. + */ + public void write() { + if (file == null) { + throw new XceliteException("No file given in Xcelite object construction. Consider using method write(file)"); + } + write(file); + } + + /** + * Saves data to a new file. + * + * @param file the file to save the data into + */ + public void write(File file) { + FileOutputStream out = null; try { - out.close(); + out = new FileOutputStream(file, false); + workbook.write(out); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); } catch (IOException e) { - new RuntimeException(e); + new RuntimeException(e); + } finally { + if (out != null) + try { + out.close(); + } catch (IOException e) { + new RuntimeException(e); + } } } - } - - /** - * Gets the excel file as byte array. - * - * @return byte array which represents the excel file - */ - public byte[] getBytes() { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { - workbook.write(baos); - } catch (IOException e) { - new RuntimeException(e); - } finally { - if (baos != null) + + /** + * write data to the giving OutputStream + * + * @param out the OutputStream to write the data into + */ + public void write(OutputStream out) { + try { + workbook.write(out); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } catch (IOException e) { + new RuntimeException(e); + } finally { + if (out != null) + try { + out.close(); + } catch (IOException e) { + new RuntimeException(e); + } + } + } + + /** + * Gets the excel file as byte array. + * + * @return byte array which represents the excel file + */ + public byte[] getBytes() { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { - baos.close(); + workbook.write(baos); } catch (IOException e) { - new RuntimeException(e); + new RuntimeException(e); + } finally { + if (baos != null) + try { + baos.close(); + } catch (IOException e) { + new RuntimeException(e); + } } + return baos.toByteArray(); } - return baos.toByteArray(); - } } diff --git a/src/test/java/DateValueConverter.java b/src/test/java/DateValueConverter.java new file mode 100644 index 0000000..5dc1460 --- /dev/null +++ b/src/test/java/DateValueConverter.java @@ -0,0 +1,30 @@ +import com.ebay.xcelite.converters.ColumnValueConverter; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +/** + * @author heyunxia (love3400wind@163.com) + * @version 1.0 + * @since 2015-10-21 下午12:31 + */ +public class DateValueConverter implements ColumnValueConverter { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + @Override + public String serialize(Long value) { + Date date = new Date(); + date.setTime(value); + return sdf.format(date); + } + + @Override + public Long deserialize(String value) { + try { + return sdf.parse(value).getTime(); + } catch (ParseException e) { + e.printStackTrace(); + return 0l; + } + } +} diff --git a/src/test/java/EnumOrderStatus.java b/src/test/java/EnumOrderStatus.java new file mode 100644 index 0000000..1d6ddd2 --- /dev/null +++ b/src/test/java/EnumOrderStatus.java @@ -0,0 +1,43 @@ +/** + * @author heyunxia. + * @Description 订单状态 + * @time 2015/5/21 17:39 + */ +public enum EnumOrderStatus { + + PAYED(11, "已支付,待分配"), + CANCELBY_CUSTOMER(12, "用户取消"), + CANCELBY_STAFF(13, "调度人员取消"), + EXPIRED(14, "已过期"), + ASSIGNED(17, "已分配"), + FINISH_WASHING(19, "师傅洗车完成"), + UNKONWN(999, "未知状态"); + + private final int code; + private final String reason; + + EnumOrderStatus(int statusCode, String reasonPhrase) { + this.code = statusCode; + this.reason = reasonPhrase; + + } + + public static EnumOrderStatus fromStatusCode(int statusCode) { + for (EnumOrderStatus item : values()) { + if (item.code == statusCode) { + return item; + } + } + + return UNKONWN; + } + + public int getStatusCode() { + return this.code; + } + + public String toString() { + return this.reason; + } + +} diff --git a/src/test/java/EnumValueConverter.java b/src/test/java/EnumValueConverter.java new file mode 100644 index 0000000..ae3292e --- /dev/null +++ b/src/test/java/EnumValueConverter.java @@ -0,0 +1,18 @@ +import com.ebay.xcelite.converters.ColumnValueConverter; + +/** + * @author heyunxia + * @version 1.0 + * @since 2015-10-21 上午11:54 + */ +public class EnumValueConverter implements ColumnValueConverter { + @Override + public String deserialize(EnumOrderStatus value) { + return null; + } + + @Override + public EnumOrderStatus serialize(String value) { + return EnumOrderStatus.fromStatusCode(Integer.valueOf(value)); + } +} diff --git a/src/test/java/PriceValueConverter.java b/src/test/java/PriceValueConverter.java new file mode 100644 index 0000000..07ca3db --- /dev/null +++ b/src/test/java/PriceValueConverter.java @@ -0,0 +1,36 @@ +import com.ebay.xcelite.converters.ColumnValueConverter; + +import java.math.BigDecimal; + +/** + * @author heyunxia + * @version 1.0 + * @since 2015-10-21 下午12:02 + */ +public class PriceValueConverter implements ColumnValueConverter { + @Override + public Integer deserialize(String value) { + return null; + } + + @Override + public String serialize(Integer value) { + + return fmtPrice(value.longValue()); + } + + + public static Long fmtPrice(String price) { + + Float fPrice = Float.parseFloat(price) * 100; + return fPrice.longValue(); + } + // 分 转换 元(String) + public static String fmtPrice(Long price) { + BigDecimal a = new BigDecimal(price); + BigDecimal b = new BigDecimal(100); + BigDecimal c = a.divide(b, 2, BigDecimal.ROUND_HALF_UP); + + return c.toString(); + } +} diff --git a/src/test/java/TestExportOrder.java b/src/test/java/TestExportOrder.java new file mode 100644 index 0000000..d9e4bb2 --- /dev/null +++ b/src/test/java/TestExportOrder.java @@ -0,0 +1,45 @@ +import com.ebay.xcelite.Xcelite; +import com.ebay.xcelite.sheet.XceliteSheet; +import com.ebay.xcelite.writer.SheetWriter; +import com.google.common.collect.Lists; + +import java.io.File; +import java.util.List; + +/** + * @author heyunxia + * @version 1.0 + * @since 2015-10-21 上午11:39 + */ +public class TestExportOrder { + public static void main(String[] args) { + Xcelite xcelite = new Xcelite(); + XceliteSheet sheet = xcelite.createSheet("users"); + SheetWriter writer = sheet.getBeanWriter(WashOrder.class); + List users = Lists.newArrayList(); + WashOrder washOrder = new WashOrder("01", "姓名", 1111, "11", 1, 1444910884496l); + users.add(washOrder); + + washOrder = new WashOrder("02", "姓名1",111, "12", 2, 1444900884496l); + users.add(washOrder); + + washOrder = new WashOrder("03", "姓名2", 111, "14", 3, 1444911884496l); + users.add(washOrder); + + washOrder = new WashOrder("04", "姓名3", 1911, "13", 4, 1444930884496l); + users.add(washOrder); + + washOrder = new WashOrder("05", "姓名4", 1811, "17", 5, 1444940884496l); + users.add(washOrder); + + washOrder = new WashOrder("06", "姓名5", 1981, "19", 7, 1444910584496l); + users.add(washOrder); + + + writer.write(users); + xcelite.write(new File("users_doc.xlsx")); + + } +} + + diff --git a/src/test/java/WashOrder.java b/src/test/java/WashOrder.java new file mode 100755 index 0000000..fc8506c --- /dev/null +++ b/src/test/java/WashOrder.java @@ -0,0 +1,111 @@ +import com.ebay.xcelite.annotations.Column; +import com.ebay.xcelite.annotations.Row; + +import java.io.Serializable; + +/** + * @author heyunxia (love3400wind@163.com) + * @version 1.0 + * @since 2015-10-21 下午12:31 + */ + +@Row(colsOrder = {"编号", "姓名", "支付方式", "价格", "支付状态", "支付时间"}) +public class WashOrder implements Serializable { + + + private static final long serialVersionUID = 8794087388514650389L; + + /** + * 主键ID + */ + @Column(name = "编号") + private String id; + /** + * 会员姓名 + */ + @Column(name = "姓名") + private String name; + /** + * 原价 + */ + @Column(name = "价格", converter = PriceValueConverter.class) + private Integer sellPrice; + + /** + * 支付方式 + */ + @Column(name = "支付方式", converter = EnumValueConverter.class) + private String payType; + + /** + * 支付状态 + */ + @Column(name = "支付状态") + private Integer payStatus; + + /** + * 支付时间 + */ + @Column(name = "支付时间", ignoreType = true, converter = DateValueConverter.class) + private Long payAt; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getSellPrice() { + return sellPrice; + } + + public void setSellPrice(Integer sellPrice) { + this.sellPrice = sellPrice; + } + + public String getPayType() { + return payType; + } + + public void setPayType(String payType) { + this.payType = payType; + } + + public Integer getPayStatus() { + return payStatus; + } + + public void setPayStatus(Integer payStatus) { + this.payStatus = payStatus; + } + + public Long getPayAt() { + return payAt; + } + + public void setPayAt(Long payAt) { + this.payAt = payAt; + } + + public WashOrder() { + } + + public WashOrder(String id, String name, Integer sellPrice, String payType, Integer payStatus, Long payAt) { + this.id = id; + this.name = name; + this.sellPrice = sellPrice; + this.payType = payType; + this.payStatus = payStatus; + this.payAt = payAt; + } +} diff --git a/users_doc.xlsx b/users_doc.xlsx new file mode 100644 index 0000000..d8062f5 Binary files /dev/null and b/users_doc.xlsx differ