美文网首页
CRM项目05

CRM项目05

作者: 建国同学 | 来源:发表于2020-05-23 10:48 被阅读0次

一、 导入导出功能

Apache POI

  • Apache POI 是 Apache 软件基金会的开放源码函式库,POI 提供 API 给 Java 程序对 Microsoft Office 格式档案读和写的功能

  • poi关于excel的概念
    Workbook(对应为一个excel)
    Sheet(excel中的表)
    Row(表中的行)
    Column(表中的列)
    Cell(表中的单元格,由行号和列号组成)

  • 不同的微软office版本选择
    2003 普通版 xls
    2007 增强版 xlsx


<!-- poi读写 -->
       <dependency>
           <groupId>org.apache.poi</groupId>
           <artifactId>poi</artifactId>
           <version>4.1.2</version>
       </dependency>

<!-- poi读写 -->
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>

导出

  • 后台控制器
@RequestMapping("/exportXlsx")
public void exportXlsx(HttpServletResponse response) throws Exception {
    //文件下载的响应头(让浏览器访问资源的的时候以下载的方式打开)
    response.setHeader("Content-Disposition","attachment;filename=employee.xls");
    //创建excel文件
    Workbook wb = employeeService.exportXlsx();
    //把excel的数据输出给浏览器
    wb.write(response.getOutputStream());
}
  • 实现类
@Override
    public Workbook exportXlsx() {
        // 获取所有员工
        List<Employee> employees = employeeMapper.selectAll();
        // 创建一个excel对象
        Workbook wb = new XSSFWorkbook();
        // 创建一页纸
        Sheet sheet = wb.createSheet("员工信息");
        // 创建标题行(索引从0)
        Row  title = sheet.createRow(0);
        title.createCell(0).setCellValue("姓名");
        title.createCell(1).setCellValue("邮箱");
        title.createCell(2).setCellValue("年龄");
        // 遍历每一个员工
        for (int i = 0; i < employees.size(); i++) {
            Employee employee = employees.get(i);
            // 创建行
            Row row = sheet.createRow(i+1);
            // 创建单元格(传入列,索引从0),在单元上写内容
            if (employee.getName() != null) {
                row.createCell(0).setCellValue(employee.getName());
            }
            if (employee.getEmail() != null) {
                row.createCell(1).setCellValue(employee.getEmail());
            }
            if (employee.getAge() != null) {
                row.createCell(2).setCellValue(employee.getAge());
            }
        }
        return wb;
    }

导入

  • 依赖

文件上传

<!--fileupload-->
<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3.1</version>
</dependency>
  • 添加文件上传解析器

mvc.xml

 <!--文件上传解析器 id必须是multipartResolver-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!--最大上传文件大小 10M-->
    <property name="maxUploadSize" value="#{1024*1024*10}"/>
</bean>
  • 模态框
// 事件
$(".btn-import").click(function () {
    $("#importModal").modal('show');
})


<div class="modal fade" id="importModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">导入</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/employee/importXls.do" method="post" id="importForm">
                    <input type="hidden" name="id">
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-3 control-label"></label>
                        <div class="col-sm-6">
                            <input type="file" name="file">
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <div class="col-sm-3"></div>
                        <div class="col-sm-6">
                            <a href="/xlstemplates/employee_import.xls" class="btn btn-success" >
                            <span class="glyphicon glyphicon-download"></span> 下载模板
                            </a>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary btn-submit">保存</button>
            </div>
        </div>
    </div>
</div>
  • 后台控制器
/**
     * 导入
     */
    @RequestMapping("importXlsx")
    @ResponseBody
    public JsonResult importXlsx(MultipartFile file) throws IOException {
        employeeService.importXlsx(file);
        return new JsonResult();
    }
  • 实现类
 @Override
    public void importXlsx(MultipartFile file) throws IOException {
        // 读excel文件
        Workbook wb = new XSSFWorkbook(file.getInputStream());
        // 读哪一页的数据
        Sheet sheet = wb.getSheetAt(0);
        // 获取最后一行的行号
        int lastRowNum = sheet.getLastRowNum();
        System.out.println(lastRowNum);
        // 循环获取数据,标题行不读
        for (int i = 1; i <= lastRowNum; i++) {
            Row row = sheet.getRow(i);
            // 创建员工对象
            Employee employee = new Employee();
            String name = row.getCell(0).getStringCellValue(); // getStringCellValue用来读文本类型单元格
            if (StringUtils.hasLength(name.trim())){// 字符串判空
                employee.setName(name.trim());
            }
            String email = row.getCell(1).getStringCellValue();
            if (StringUtils.hasLength(email.trim())) {
                employee.setEmail(email.trim());
            }
            // 判断单元格类型
            CellType cellType = row.getCell(2).getCellType();
            if (cellType.equals(CellType.NUMERIC)) {
                double value = row.getCell(2).getNumericCellValue();// getNumericCellValue用来读文本类型单元格
                employee.setAge((int)value);
            }else {
                String age = row.getCell(2).getStringCellValue();
                if (StringUtils.hasLength(age.trim())) {
                    employee.setAge(Integer.valueOf(age.trim()));
                }
            }
            employee.setPassword("1");
            employee.setStatus(true);
            // 插入到数据
            this.save(employee,null);
        }
    }

二、数据字典模块

数据字典

像如下这些具有相同类型的配置项,配置到系统的数据字典表中,方便系统维护,由超级管理员统一在后台进行数据字典维护,如果用户需求要增加变更配置项,只需要修改数据字典表记录即可,不需要修改代码。

数据库

分类表(字典目录表)systemdictionary

  • id 主键
  • title 标题
  • sn 编码
  • intro 描述

分类明细表(字典明细表)systemdictionaryitem

  • id 主键
  • parent_id 关联的字典目录主键
  • title 标题
  • sequence 序列

相关文章

  • CRM项目05

    一、 导入导出功能 Apache POI Apache POI 是 Apache 软件基金会的开放源码函式库,PO...

  • 对CRM项目的了解,简述其开发设计思想

    原文来源于:对CRM项目的了解,简述其开发设计思想自己编写于:2017/03/19 15:05 ,先转移到“简书”。

  • crm项目

    项目目的:能一目了然看到项目进展情况、客户进展情况 数据包含:服务行销数据、项目进展相关数据 目前仅能做到服务行销...

  • 如何正确评估CRM项目实施风险?(三)

    越来越多的企业面临着企业数据化建设时,首选建立CRM数据库,实施CRM项目首先要对项目进行评估,如何评估CRM实施...

  • 八骏:CRM调研,企业需要做什么呢?

    CRM调研阶段是CRM实施的第一步,一般都是具有个性化配置需求的CRM项目需要调研。企业在确定CRM厂商之后就会进...

  • CRM实施流程环节_八骏CRM

    CRM客户管理系统的实施流程很重要,成熟的实施流程能大大降低CRM项目失败的风险。八骏CRM客户管理系统的实施流程...

  • CRM项目08

    复习 mybatis里排序用${},是把 ${ } 替换成变量的值,完成的是简单的字符串拼接。参数用#{},预编译...

  • CRM项目02

    一、Bootstrap-Validator验证插件 下载:http://bootstrapvalidator.vo...

  • CRM项目01

    一、Freemarker FreeMarker 是一款免费的模板引擎 对比 JSP 而言,FreeMarker 性...

  • CRM项目06

    复习 List selectByRoleSn(String... sn); 可变参数,可以直接传多个字符串,...

网友评论

      本文标题:CRM项目05

      本文链接:https://www.haomeiwen.com/subject/xfiaohtx.html