com.github.tonyluo.excel.ExcelUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of excel-util Show documentation
Show all versions of excel-util Show documentation
import/export excel util, base poi
package com.github.tonyluo.excel;
import com.github.tonyluo.excel.util.ExcelConverter;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.util.List;
public class ExcelUtil {
/**
* 导出Excel文件到磁盘
* @param filePath file path
* @param entityList data list
* @param class
* @return file path
* @throws IOException IOException
* @throws InstantiationException InstantiationException
* @throws IllegalAccessException IllegalAccessException
*/
public static String exportToFile(String filePath, List entityList) throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException {
// workbook
XSSFWorkbook workbook = ExcelConverter.generateWorkbook(entityList);
FileOutputStream fileOutputStream = null;
try {
// workbook 2 FileOutputStream
fileOutputStream = new FileOutputStream(filePath);
workbook.write(fileOutputStream);
fileOutputStream.flush();
} finally {
closeWorkbook(workbook, fileOutputStream);
}
return filePath;
}
/**
* 导出Excel字节数据
* @param entityList data list
* @param class
* @return byte
* @throws IOException IOException
* @throws InstantiationException InstantiationException
* @throws IllegalAccessException IllegalAccessException
*/
public static byte[] exportToBytes(List entityList) throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException {
// workbook
XSSFWorkbook workbook = ExcelConverter.generateWorkbook(entityList);
ByteArrayOutputStream outputStream = null;
byte[] result = null;
try {
outputStream = new ByteArrayOutputStream();
workbook.write(outputStream);
// flush
outputStream.flush();
result = outputStream.toByteArray();
} finally {
closeWorkbook(workbook, outputStream);
}
return result;
}
private static void closeWorkbook(XSSFWorkbook workbook, OutputStream outputStream) throws IOException {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
if (workbook != null) {
// dispose of temporary files backing this workbook on disk
workbook.close();
}
}
/**
*
* @param path excel file path
* @param clazz class
* @param startRow start row
* @param class
* @return java bean list
* @throws IOException IOException
* @throws InstantiationException InstantiationException
* @throws IllegalAccessException IllegalAccessException
*/
public static List importFromPath(String path, Class clazz, int startRow) throws IOException, IllegalAccessException, InstantiationException, ClassNotFoundException {
Workbook book = ExcelConverter.readFile(path);
return importExcel(book,clazz,0,startRow,-1);
}
/**
*
* @param file excel file path
* @param clazz class
* @param startRow start row
* @param class
* @return java bean list
* @throws IOException IOException
* @throws InstantiationException InstantiationException
* @throws IllegalAccessException IllegalAccessException
*/
public static List importFromFile(File file, Class clazz, int startRow) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
Workbook book = ExcelConverter.readFile(file);
return importExcel(book,clazz,0,startRow,-1);
}
/**
*
* @param stream input stream
* @param clazz class
* @param startRow start row
* @param class
* @return java bean list
* @throws IOException IOException
* @throws InstantiationException InstantiationException
* @throws IllegalAccessException IllegalAccessException
*/
public static List importFromInputStream(InputStream stream, Class clazz, int startRow) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException {
Workbook book = ExcelConverter.readFromInputStream(stream);
return importExcel(book,clazz,0,startRow,-1);
}
//=====================================================================
/**
*
* @param book workbook
* @param clazz class
* @param sheetIndex sheet Index
* @param startRow start row
* @param endRow end row
* @param class
* @return java bean list
* @throws InstantiationException InstantiationException
* @throws IllegalAccessException IllegalAccessException
*/
public static List importExcel(Workbook book , Class clazz, int sheetIndex, int startRow, int endRow) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return ExcelConverter.getBeanListFromWorkBook(book, clazz,sheetIndex, startRow,endRow);
}
}