• Java导出Excel快速开始


    代码:

     package com.scoremanager.utils;
     
     import org.apache.poi.hssf.usermodel.*;
     
     import javax.servlet.http.HttpServletResponse;
     import java.io.BufferedOutputStream;
     import java.lang.reflect.Field;
     import java.lang.reflect.Method;
     import java.util.Collection;
     import java.util.Iterator;
     
     /*
      * 导出Excel的工具类
      * */
     public class ExportExcelUtils<T> {
         public void exportExcel(String[] headers, Collection<T> dataset, String fileName, HttpServletResponse response) {
             // 声明一个工作薄
             HSSFWorkbook workbook = new HSSFWorkbook();
             // 生成一个表格
             HSSFSheet sheet = workbook.createSheet(fileName);
             // 设置表格默认列宽度为15个字节
             sheet.setDefaultColumnWidth((short) 20);
             // 产生表格标题行
             HSSFRow row = sheet.createRow(0);
             for (short i = 0; i < headers.length; i++) {
                 HSSFCell cell = row.createCell(i);
                 HSSFRichTextString text = new HSSFRichTextString(headers[i]);
                 cell.setCellValue(text);
             }
             try {
                 // 遍历集合数据,产生数据行
                 Iterator<T> it = dataset.iterator();
                 int index = 0;
                 while (it.hasNext()) {
                     index++;
                     row = sheet.createRow(index);
                     T t = (T) it.next();
                     // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
                     Field[] fields = t.getClass().getDeclaredFields();
                     for (short i = 0; i < headers.length; i++) {
                         HSSFCell cell = row.createCell(i);
                         Field field = fields[i];
                         String fieldName = field.getName();
                         String getMethodName = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
                         Class tCls = t.getClass();
                         Method getMethod = tCls.getMethod(getMethodName, new Class[]{});
                         Object value = getMethod.invoke(t, new Object[]{});
                         // 判断值的类型后进行强制类型转换
                         String textValue = null;
                         // 其它数据类型都当作字符串简单处理
                         if (value != null && value != "") {
                             textValue = value.toString();
                         }
                         if (textValue != null) {
                             HSSFRichTextString richString = new HSSFRichTextString(textValue);
                             cell.setCellValue(richString);
                         }
                     }
                 }
                 getExportedFile(workbook, fileName, response);
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
     
         /**
          * 方法说明: 指定路径下生成EXCEL文件
          *
          * @return
          */
         public void getExportedFile(HSSFWorkbook workbook, String name, HttpServletResponse response) throws Exception {
             BufferedOutputStream fos = null;
             try {
                 String fileName = name + ".xls";
                 response.setContentType("application/x-msdownload");
                 response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
                 fos = new BufferedOutputStream(response.getOutputStream());
                 workbook.write(fos);
             } catch (Exception e) {
                 e.printStackTrace();
             } finally {
                 if (fos != null) {
                     fos.close();
                 }
             }
         }
    }
    View Code

    jar:

     官网都可下载,自行百度。

  • 相关阅读:
    Oracle删除用户和表空间
    PLSQL配置教程
    Oracle 11g client 安装
    oracle 11g 安装
    oracle创建用户空间、导出、导入dmp备份文件方法
    c#关键字
    c#运算符重载
    每天一个Linux命令之date
    linux之2>&1
    每天一个Linux命令之crontab
  • 原文地址:https://www.cnblogs.com/daihang2366/p/13292587.html
Copyright © 2020-2023  润新知