excel导入导出
两种方式
第一:利用第三方插件JXL实现excel的生成;
第二:利用apache的poi去生成;
POI的方式:
一:倒入jar包;
二:创建实体类;
三:excel工具类;
1.先声明一个工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
2.生成一个表格
HSSFSheet sheet = workbook.createSheet(title);
3. 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
4.生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
5.设置这些样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
6.生成一个字体
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
7.把字体应用到样式中
style.setFont(font);
8.创建表头
HSSFRow row = sheet.createRow(0);
for (short i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
9.创建单元格
- // 遍历集合数据,产生数据行
- Iterator<T> it = dataset.iterator();
- int index = 0;
- while (it.hasNext()) {
- index++;
- row = sheet.createRow(index);
- Object t = it.next(); //这里不要使用泛型强制转换
- // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
- Field[] fields = t.getClass().getDeclaredFields();
- for (short i = 0; i < fields.length; i++) {
- HSSFCell cell = row.createCell(i);
- cell.setCellStyle(style2);
- Field field = fields[i];
- String fieldName = field.getName();
- String getMethodName = "get"
- + fieldName.substring(0, 1).toUpperCase()
- + fieldName.substring(1);
- try {
- Class tCls = t.getClass();
- Method getMethod = tCls.getMethod(getMethodName,
- new Class[] {});
- Object value = getMethod.invoke(t, new Object[] {});
- // 判断值的类型后进行强制类型转换
- String textValue = "";
- if(value == null || "".equals(value)){
- value = "";
- }else if (value instanceof Integer) {
- int intValue = (Integer) value;
- cell.setCellValue(intValue);
- } else if (value instanceof Float) {
- float fValue = (Float) value;
- cell.setCellValue(fValue + "");
- } else if (value instanceof Double) {
- double dValue = (Double) value;
- cell.setCellValue(dValue + "");
- } else if (value instanceof Long) {
- long longValue = (Long) value;
- cell.setCellValue(longValue);
- }
- if (value instanceof Boolean) {
- boolean bValue = (Boolean) value;
- textValue = "男";
- if (!bValue) {
- textValue = "女";
- }
- } else if (value instanceof Date) {
- Date date = (Date) value;
- SimpleDateFormat sdf = new SimpleDateFormat(pattern);
- textValue = sdf.format(date);
- } else {
- // 其它数据类型都当作字符串简单处理
- textValue = value.toString();
- }
- // 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
- if (textValue != null) {
- Pattern p = Pattern.compile("^//d+(//.//d+)?{1}");
- Matcher matcher = p.matcher(textValue);
- if (matcher.matches()) {
- // 是数字当作double处理
- cell.setCellValue(Double.parseDouble(textValue));
- } else {
- HSSFRichTextString richString = new HSSFRichTextString(
- textValue);
- HSSFFont font3 = workbook.createFont();
- font3.setColor(HSSFColor.BLUE.index);
- richString.applyFont(font3);
- cell.setCellValue(richString);
- }
- }
- } catch (SecurityException e) {
- e.printStackTrace();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- } finally {
- // 清理资源
- }
- }
- }
- try {
- //默认导出到E盘下
- FileOutputStream out = new FileOutputStream(E://name.xls);
- workbook.write(out);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
浙公网安备 33010602011771号