- package org.beanguo.excelOpt;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.List;
- import org.beanguo.excelOpt.impl.Excel2003;
- import org.beanguo.excelOpt.impl.Excel2007;
- /**
- * Excel
- *
- * @version 1.0
- * @author Jamin
- * @createTime 2012-11-07
- * */
- public class ExcelOpt {
- /**
- * Excel
- *
- * @param Excel,
- * @return
- * @throws FileNotFoundException
- * */
- public static <X> List<List<X>> readExcel(String filePath, Class<X> clazz) throws FileNotFoundException{
- if(filePath == null){
- throw new FileNotFoundException("filePathnull");
- }
- return readExcel(new File(filePath), clazz);
- }
- /**
- * Excel
- *
- * @param Excel,
- * @return
- * */
- public static <X> List<List<X>> readExcel(File file, Class<X> clazz){
- Excel<X> excel = null;
- try{
- excel = new Excel2003<X>(file, clazz);
- return excel.readExcel();
- }catch(Exception e){
- excel = new Excel2007<X>(file, clazz);
- return excel.readExcel();
- }
- }
- /**
- * Excel
- *
- * @param Excel,,
- * @return
- * @throws FileNotFoundException
- * */
- public static <X> List<X> readExcel(String filePath, Class<X> clazz, int pageNo) throws FileNotFoundException{
- if(filePath == null){
- throw new FileNotFoundException("filePathnull");
- }
- return readExcel(filePath, clazz, pageNo);
- }
- /**
- * Excel
- *
- * @param Excel,,
- * @return
- * */
- public static <X> List<X> readExcel(File file, Class<X> clazz, int pageNo){
- if(pageNo < 1){
- throw new IndexOutOfBoundsException("pageNo="+pageNo+"1");
- }
- Excel<X> excel = null;
- try{
- excel = new Excel2003<X>(file, clazz);
- return excel.readExcel(pageNo-1);
- }catch(Exception e){
- excel = new Excel2007<X>(file, clazz);
- return excel.readExcel(pageNo-1);
- }
- }
- /**
- * Excel
- *
- * @param Excel,,
- * */
- public static <X> void writeExcel(File file, Class<X> clazz, List<List<X>> list){
- Excel<X> excel = new Excel2003<X>(file, clazz);
- excel.writeExcel(list);
- }
- /**
- * Excel
- *
- * @param Excel,,
- * @throws FileNotFoundException
- * */
- public static <X> void writeExcel(String filePath, Class<X> clazz, List<List<X>> list) throws FileNotFoundException{
- if(filePath == null){
- throw new FileNotFoundException("filePathnull");
- }
- Excel<X> excel = new Excel2003<X>(new File(filePath), clazz);
- excel.writeExcel(list);
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/070120148449.html
来源: http://www.codesnippet.cn/detail/070120148449.html