zoukankan      html  css  js  c++  java
  • Java 用jxl读取excel并保存到数据库(此方法存在局限,仅限本地电脑操作,放在服务器上的项目,需要把文件上传到服务器,详细信息,见我的别的博客)

    项目中涉及到读取excel中的数据,保存到数据库中,用jxl做起来比较简单。

    基本的思路:

    把excel放到固定盘里,然后前段页面选择文件,把文件的名字传到后台,再利用jxl进行数据读取,把读取到的数据存到list中,通过遍历list,得到map,存到数据库中。

    首先导入jar包:在网上都有,

    代码:

    页面:

    新模excel导入<input type="file" name="excel" id="xinmu">
            <input type="button" id="newmj" value="导入">

    js

    //通过ajax进行操作
    $(function(){ $("#newmj").click(function(){ alert("haha"); $.ajax({ url:'${pageContext.request.contextPath}/UploadExcelServlet?type=xinmu&filename='+$("#xinmu").val(), type:'get', success:function(result){ //alert("haha"); alert(result); var json= eval('(' + result + ')');               } }) }) });

    servlet

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //request.setCharacterEncoding("utf-8");
            System.out.println("jinru");
            String type=request.getParameter("type");
            String filename=request.getParameter("filename");
            //System.out.println(filename);
            File file = new File("D:\"+filename);// 表格存储的位置
            JSONObject jsonObject = new JSONObject();
            //记录一下文件是否存在
            if (file.exists()) {
                jsonObject.put("exist", "文件存在");
                List<Map<String, String>>list=ReadExcel.readExcel(file);
                MuJUService mjService = new MuJUService();
                for (Map<String, String> map : list) {
                    jsonObject = mjService.addNewMuJu(map);
                }
                
            } else {
                jsonObject.put("exist", "文件不存在");
                System.out.println("文件不存在");
            }
            
        
        }

    jxl处理类

    import java.io.File;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    
    public class ReadExcel {
        
        public static List<Map<String,String>> readExcel(File file){
            List<Map<String, String>>list =new ArrayList<Map<String,String>>();
            try {
                // 判断文件是否存在
                    // 创建工作簿
                    Workbook workbook = Workbook.getWorkbook(file);
                    // 获得第一个工作表sheet1
                    Sheet sheet = workbook.getSheet(0);
                    // 获得数据
                    for (int i = 1; i < sheet.getRows(); i++) {// sheet.getRows():获得表格文件行数
                        Map<String, String>map = new HashMap<String, String>();
                        for (int j = 0; j < sheet.getColumns(); j++) {// sheet.getColumns():获得表格文件列数
                            Cell cell = sheet.getCell(j, i);
                        //    System.out.print(cell.getContents() + " ");
                            map.put(sheet.getCell(j,0).getContents(), cell.getContents());
                            //(列,行)
                        }
                        //System.out.println("");// 换行
                        list.add(map);
                    }
                    //调用方法进行数据库的操作
                    //.......
                    System.out.println(list);
                    workbook.close();// 关闭
                } catch (Exception e) {
                    e.printStackTrace();
                }
            return list;
        }
    }

    如此就能完成了,但是值得注意的是,我现在写的这段代码,无法自由选择文件路径进行读取,excel必须放在固定盘里。excel后缀必须是.xls,所以wps的excel不可用,而且文件名字不可以是中文。

    服务器项目:http://www.cnblogs.com/stepbystepwhx/p/7782872.html

  • 相关阅读:
    sencha touch 扩展篇之将sencha touch打包成安装程序(上)- 使用sencha cmd打包安装程序
    sencha touch 扩展篇之使用sass自定义主题样式 (下)通过css修改官方组件样式以及自定义图标
    一个不错的android组件的网站
    sencha touch 扩展篇之使用sass自定义主题样式 (上)使用官方的api修改主题样式
    sencha touch 入门系列 (九) sencha touch 布局layout
    面试题总结
    国外接活网站Elance, Freelancer和ScriptLance的介绍和对比
    sencha touch 入门系列 扩展篇之sencha touch 项目打包压缩
    Android Design Support Library——Navigation View
    设计模式——命令模式
  • 原文地址:https://www.cnblogs.com/stepbystepwhx/p/7693298.html
Copyright © 2011-2022 走看看