• EasyExcel示例(阿里巴巴)基于Maven


    首先感谢阿里巴巴提供了easyexcel工具类,github地址:https://github.com/alibaba/easyexcel

    注意!!这里只是一个简单的示例,VC大法即可使用,对于复杂的execl导出可能会出现问题。

    另Execl文件后缀为xlsx。

      1、环境搭建

        jar包依赖

     <!-- excel导入导出插件 -->
     <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>easyexcel</artifactId>
         <version>1.1.2-beat1</version>
     </dependency>

        2、代码

    public class MyExcel {
        //
        @Test
        public void simpleRead() {
            FileInputStream fileInput;
            try {
                fileInput = new FileInputStream("F://javaio文件目录//hss.xlsx");
                List<Object> read = EasyExcelFactory.read(fileInput, new Sheet(0,0));
                System.out.println(read);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            
        }
        //
        @Test
        public void simpleWrite() {
            FileOutputStream fileOut;
            try {
                File file = new File("F://javaio文件目录//hss123.xlsx");
                if (file.exists()) {
                    file.delete();
                }
                fileOut=new FileOutputStream("F://javaio文件目录//hss123.xlsx");
                ExcelWriter writer = EasyExcelFactory.getWriter(fileOut);
                Sheet sheet = new Sheet(1,0);
                Sheet sheet2 = new Sheet(1,0);
                sheet.setSheetName("HelloWord");
                List<List<String>> data2 = new ArrayList<>();
                List<String> list2 = new ArrayList<>();
                List<String> list3 = new ArrayList<>();
                List<String> list4 = new ArrayList<>();
                List<String> list5 = new ArrayList<>();
                List<List<String>> data = new ArrayList<>();
                List<String> list1 = new ArrayList<>();
                for (int i = 0; i < 10; i++) {
                    list1.add("123");
                    list1.add("123");
                    list1.add("123");
                    list1.add("123");
                    list1.add("123");
                    data.add(list1);
                }
                    list2.add("你好1");
                    list3.add("你好2");
                    list4.add("你好3");
                    list5.add("你好4");
                    data2.add(list2);
                    data2.add(list3);
                    data2.add(list4);
                    data2.add(list5);
                sheet2.setHead(data2);
                writer.write0(null, sheet2);
                writer.write0(data, sheet);
                writer.finish();
                fileOut.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

         3.作为Util类使用(使用时调用

    baseExportExcel

    )具体class类型地址 https://github.com/alibaba/easyexcel/blob/master/src/test/java/com/alibaba/easyexcel/test/demo/write/DemoData.java

    /**
     * excel工具类
     */
    @Slf4j
    public class EasyexcelUtil {
    
        protected static FastDateFormat fastDateFormat = FastDateFormat.getInstance("yyyyMMddHH:mm:ss");
    
        /**
         *
         * @param fileName 文件名
         * @param cameraReportStopList 普通vo类
         * @param entityModel 继承BaseRowModel的模型class
         * @param response
         * @param <T>
         */
        public static <T extends BaseRowModel> void baseExportExcel(String fileName , List cameraReportStopList , Class<T> entityModel , HttpServletResponse response) {
            List<BaseRowModel> modelList = new ArrayList<>();
            if (StringUtils.isEmpty(cameraReportStopList)) {
                return;
            }
            cameraReportStopList.forEach(cameraReportStopVo -> {
                try{
                    BaseRowModel cameraReportStopModel = entityModel.newInstance();
                    BeanUtils.copyProperties(cameraReportStopVo , cameraReportStopModel);
                    modelList.add(cameraReportStopModel);
                } catch (Exception e){
                    log.info("excel export exception",e);
                }
            });
    
            try{
                writeExcel(response , modelList , fileName+fastDateFormat.format(new Date()) , "第一页");
            } catch (Exception e){
                log.info("excel export exception",e);
                e.printStackTrace();
            }
        }
    
        /**
         * 导出 Excel :一个 sheet,带表头
         *
         * @param response HttpServletResponse
         * @param list 数据 list,每个元素为一个 BaseRowModel
         * @param fileName 导出的文件名
         * @param sheetName 导入文件的 sheet 名
         */
        public static void writeExcel(HttpServletResponse response,
                                      List<? extends BaseRowModel> list,
                                      String fileName,
                                      String sheetName) throws Exception {
            ExcelWriter writer = new ExcelWriter(getOutputStream(fileName, response), ExcelTypeEnum.XLSX);
            Class clazz = null;
            if (list.size() > 0) {
                clazz = list.get(0).getClass();
            } else {
                clazz = BaseRowModel.class;
            }
            Sheet sheet = new Sheet(1, 0, clazz);
            sheet.setSheetName(sheetName);
            writer.write(list, sheet);
            writer.finish();
        }
    
        /**
         * 导出文件时为Writer生成OutputStream
         * @param fileName
         * @param response
         * @return
         * @throws Exception
         */
        private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
            try {
                fileName = URLEncoder.encode(fileName, "UTF-8");
                response.setContentType("application/vnd.ms-excel");
                response.setCharacterEncoding("utf8");
                response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx");
                response.setHeader("Pragma", "public");
                response.setHeader("Cache-Control", "no-store");
                response.addHeader("Cache-Control", "max-age=0");
                return response.getOutputStream();
            } catch (IOException e) {
                log.info("excel export exception",e);
                ExceptionCast.cast("导出失败!");
            }
            return null;
        }
    
    
    }
  • 相关阅读:
    python 绘制所有线条、散点等 可用的标记符号(marker)
    Maximal InformMaximal Information Coefficient (MIC)最大互信息系数详解与实现 https://blog.csdn.net/FontThrone/article/details/85227239
    python画图,等间距坐标距离表示不等间距数据值
    机器学习数据库 http://archive.ics.uci.edu/ml/datasets.php https://www.openml.org/d/179
    Matplotlib.pyplot.plot图形符号、风格及颜色简写形式速查表https://blog.csdn.net/Treasure99/article/details/106044114/
    Pycharm 2017.3 永久激活教程https://www.bilibili.com/read/cv11643882/
    学习资源http://imada.huel.edu.cn/resource.html# 数据库/机器学习/安全领域顶会论文
    Python的知识点 plt.plot()函数细节
    原因是标题默认输出英文,如果输出中文,要对字体进行调整。需要在程序定义前输入:
    会讲故事助你成功
  • 原文地址:https://www.cnblogs.com/wangshilei/p/11969998.html
Copyright © 2020-2023  润新知