Excel导入(ssh)

步骤:
1、首先在ftl页面上添加导入按钮跳转到导入详情页面,再写一个导入详情页面sp_importfile.ftl
2、然后在Action中导入一堆包,excel的,reader的,并写excel导入的方法importfile(),设置上传文件的路径uploadFile,开启新的线程处理SP厂商信息,利用SpReader与ExcelReaderUtil导入厂商信息
3、在SpReader.java中写getRows()的方法,获取service,若第一行为标题行,则直接跳过,若第一行直接开始信息就不跳过,然后读取excel中的每条数据,存入实体表中。
下面以SP厂商为例。
excel导入的代码:

// excel导入
        public String importfile() {
            return "importfile";
        }
        public String importfilesave()
        {
            if (excel == null) {
                addActionError("请导入excel");
                return ERROR;
            }
            try {
                String filePath = FileUtil.copyFile(
                        ServletActionContext.getServletContext(),
                        excel,
                        excelFileName.substring(excelFileName.lastIndexOf(".")+1, excelFileName.length()));
                String newfilePath = Tools.class.getClassLoader().getResource("/").getPath();
                newfilePath = newfilePath.substring(0, newfilePath.indexOf("/WEB-INF/classes"));
                final String finalPath = newfilePath + filePath;
                File uploadFile = new File(finalPath);
                String excelFilePath = CommonConstant.FTP.READ_FTP_FILE_PATH_FOR_EXCEL + "/" + uploadFile.getName();
                System.out.println(excelFilePath);

                // 开启新的线程处理入库
                new Thread() {
                    public void run() {
                        try {
                                // 执行处理
                                // 导入SP厂商信息
                                SpReader reader = new SpReader();
                                String path = finalPath;
                                ExcelReaderUtil.readExcel(reader, path);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            } catch (Exception e) {
                e.printStackTrace();
                addActionError(e.getMessage());
                return ERROR;
            }
            redirectUrl = "sp!list.action";
        return SUCCESS;
        }

下面是SpReader的代码:

public class SpReader implements IRowReader {

    private static Integer count = 1;
    /*
     * 业务逻辑实现方法
     * 
     * @see com.eprosun.util.excel.IRowReader#getRows(int, int, java.util.List)
     */
    public void getRows(int sheetIndex, int curRow, List<Integer> collist, List<String> rowlist) {
        // 获取service
        SpService spService = (SpService) SpringUtil
                .getBean("spServiceImpl");
        // 若第一行为标题行,则直接跳过
        if (curRow == 0) {
            return;
        }
        try {
            // 处理空值列
            rowlist = operateEmptyCol(collist,rowlist);
            Sp sp = new Sp(); //一定要每条记录就new一次
            sp.setName(rowlist.get(0));
            spService.save(sp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 处理空值列
    private List<String> operateEmptyCol(List<Integer> collist, List<String> rowlist){
        // 转化行数据,将空值填入
        List<String> newList = new ArrayList<String>();
        // 根据excel的列数初始化
        for(int i = 0; i < count; i++){
            newList.add("");
        }
        // 根据有值的列覆盖list
        for(int i = 0; i < collist.size(); i++){
            newList.set(collist.get(i), rowlist.get(i));
        }
        return newList;
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Navicat12手册是一本详细介绍Navicat12数据库管理软件使用方法和功能的参考手册。它包含了软件的安装步骤、界面介绍、数据库创建与连接、数据表的创建与修改、SQL查询、数据导入导出等一系列操作指南。 首先,Navicat12手册提供了软件的安装步骤,帮助用户顺利安装软件并完成必要的配置,以便开始使用数据库管理功能。 其次,手册详细介绍了Navicat12的界面布局和各个功能模块的使用方法。通过手册,用户可以了解软件的整体架构,方便操作和快速上手。 手册还解释了数据库连接的方法,包括本地连接和远程连接,以及如何通过SSH隧道连接到远程数据库。这些方法可以帮助用户快速连接到各种类型的数据库服务器。 此外,手册讲解了如何创建和修改数据库、数据表和字段,并提供了在操作过程中的注意事项和常见问题解答。 手册中还介绍了SQL查询的基本语法和常用的查询操作,包括条件查询、排序、分组、联结等。这些知识有助于用户进行高效的数据查询和报表生成。 最后,手册还详细介绍了数据的导入和导出方法,包括从其他数据库软件或CSV文件导入数据,以及将数据导出为各种格式,如CSV、Excel、XML等。 综上所述,Navicat12手册是一本非常有用的参考资料,对于使用Navicat12进行数据库管理的用户来说十分重要。通过仔细阅读手册,用户可以充分了解软件的各项功能和操作步骤,提高数据库管理的效率和准确性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值