Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private List<String> 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.
Expand Down
297 changes: 178 additions & 119 deletions src/main/java/com/ebay/xcelite/Xcelite.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
30 changes: 30 additions & 0 deletions src/test/java/DateValueConverter.java
Original file line number Diff line number Diff line change
@@ -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<String, Long> {
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;
}
}
}
Loading