CRM项目05

2020-05-23  本文已影响0人  建国同学

一、 导入导出功能

Apache POI

<!-- 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

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

上一篇下一篇

猜你喜欢

热点阅读