jxl和POI的区别
来源:互联网 发布:企业电话查询软件 编辑:程序博客网 时间:2024/05/29 03:46
首先从优缺点上来说
一、jxl
优点:
Jxl对中文支持非常好,操作简单,方法看名知意。Jxl是纯javaAPI,在跨平台上表现的非常完美,代码可以再windows或者Linux上运行而无需重新编写支持Excel 95-2000的所有版本(网上说目前可以支持Excel2007了,还没有尝试过)生成Excel 2000标准格式支持字体、数字、日期操作能够修饰单元格属性支持图像和图表,但是这套API对图形和图表的支持很有限,而且仅仅识别PNG格式。
缺点:效率低,图片支持不完善,对格式的支持不如POI强大
二、POI
优点:
效率高(数据来源:http://blog.csdn.net/jarvis_java/article/details/4924099)支持公式,宏,一些企业应用上会非常实用能够修饰单元格属性支持字体、数字、日期操作
缺点:不成熟,代码不能跨平台,貌似不少同行在使用工程中还碰到让人郁闷的BUG(最近的项目中也是遇到了一些bug,不过目前没有查出来是代码的问题还是POI的问题,总之问题很诡异,数据替代参数总有失败的。关于不能跨平台这一说,我也没有试验过,不过Java不是跨平台吗?POI是JAVA的一个组件,怎么就不能跨平台了呢,总之这些问题还需要在以后的项目中多多实践,才能比较出区别之处。)
使用JXL反射操作导入数据
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.sql.Timestamp;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;import jxl.Cell;import jxl.CellType;import jxl.DateCell;import jxl.Sheet;import jxl.Workbook;/** * EXECL文件导入处理工具类 * <p> */@SuppressWarnings("unchecked")public class FileImportUtil { public final static <T> List<T> parseExcel(File excel, String className, String[] fieldNames) { List result = new ArrayList(); FileInputStream fs = null; try { fs = new FileInputStream(excel); Workbook wb = Workbook.getWorkbook(fs); Sheet sheet = wb.getSheet(0); int row = sheet.getRows(); String value = null; int nullNum = 0; for (int r = 1; r < row; r++) { nullNum = 0; Object o = Class.forName(className).newInstance(); for (int i = 0; i < fieldNames.length; i++) { value = sheet.getCell(i, r).getContents(); if (value == null || value.trim().length() == 0) { nullNum++; continue; } insertValue(o, fieldNames[i], fetchExcelValue(sheet.getCell(i, r))); } if (nullNum < fieldNames.length) { result.add(o); } } } catch (Exception e) { throw new UnsupportedOperationException("解析EXCEL生成对象错误,请根据Excel模板上传正确文件!"); } finally { if (fs != null) try { fs.close(); } catch (IOException e) { } } return result; } private static final String fetchExcelValue(Cell cell) { String result = null; if (cell.getType() == CellType.DATE) { DateCell dt = (DateCell) cell; DateFormat df = dt.getDateFormat(); result = df.format(dt.getDate()); } else { result = cell.getContents(); } return result; } private static final void insertValue(Object o, String fieldName, String value) throws SecurityException, NoSuchFieldException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, ParseException { Field field = o.getClass().getDeclaredField(fieldName); Method method = o.getClass().getMethod( "set" + field.getName().substring(0, 1).toUpperCase() + field.getName().substring(1), field.getType()); method.invoke(o, transforObject(field.getType(), value)); } private static final Object transforObject(Class type, String str) throws ParseException { if (str == null || str.trim().length() == 0) { return null; } Object value = str; if (type.equals(Integer.class) || type.equals(int.class)) { value = Integer.valueOf(str); } else if (type.equals(Timestamp.class)) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = sdf.parse(str); value = new Timestamp(date.getTime()); } else if (type.equals(boolean.class)) { value = Boolean.valueOf(str); } return value; }}
personImport.java 就是个bean
public class PersonImport { private String personCode; private String name; private String deptCode; private String gender; private String IDType; private String identityId; private String birthday; private String phone; private String pinyin; private String address; private String englishName; private String email; private String inaugurationDate; private String leaveJobDate; private String educational; private String people;//民族 private String remark;}
这里就是使用了导入的功能
String className = PersonImport.class.getName(); String[] fieldNames = { "personCode", "name", "deptCode", "gender", "IDType", "identityId", "birthday", "phone", "pinyin", "address", "englishName", "email", "inaugurationDate", "leaveJobDate", "educational", "people", "remark" }; List<PersonImport> personImportList = FileImportUtil.parseExcel(excel, className, fieldNames);
action 层的操作
public void importPersonFromExcel(MultipartFile multExcel) throws IllegalStateException, IOException { ActionResult result = new ActionResult(); result.setSuccess(true); File excel = new File(FileUploadUtils.getUploadFileTempPath() + File.separator + SysCodeUtil.generateByUUID()); multExcel.transferTo(excel); OperateResult operResult = new OperateResult(); if (excel == null) { result.setSuccess(false); result.setMessage("请选择要上传的文件!"); } else { try { operResult = personInfoService.saveBatchImportPersonInfo(this.findCurrentUserId(), excel); result.setSuccess(operResult.isResult()); result.setMessage(operResult.getMsg()); } catch (Exception e) { LogUtils.logException(e); result.setSuccess(false); result.setMessage(ConstParamWebservice.SYS_ERROR); } } }
0 0
- poi和jxl的区别
- jxl和POI的区别
- jxl 和POI的区别
- jxl和POI的区别
- Jxl和POI的区别
- POI与JXL的区别
- POI和jxl的比较
- java中POI与JXL的区别
- POI与JXL操作Excel的区别
- poi与jxl的区别分析
- jxl 与poi 区别
- jxl 与poi 区别
- jxl和poi生成excel的对比
- 简单区分Jxl和POI的不同之处
- POI&JXL用法及区别
- jxl与poi两个jar包的区别
- POI和jxl写入效率
- jxl和poi操作Excel
- 使用递归进行插入排序
- 有容云AppSoar容器健康检查与调度策略
- 使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
- Reverse Vowels of a String 交换元音字母
- 一个Windows 系统究竟有多复杂?
- jxl和POI的区别
- networkx库:AttributeError: 'module' object has no attribute 'graphviz_layout'
- n个骰子的点数和
- 理解Javascript中的Function
- Hive外部表学习
- CSS基础学习
- CocoaPods忽略更新版本
- 数组与可变参数
- Codeforces C2. Brain Network (medium)