zoukankan      html  css  js  c++  java
  • Excel表格读取数据,并且存入数据库

    第一步:读取数据

    1.数据封装实体(根据需求自行改定)

    package com.core;
    /**
     * 读取Excel文件
     * @author Administrator
     *
     */
    public class FileExcel {
    	private Integer id;
    	private String qxName;
    	private String roleName;
    	private String createtime;
    	public Integer getId() {
    		return id;
    	}
    	public void setId(Integer id) {
    		this.id = id;
    	}
    	public String getQxName() {
    		return qxName;
    	}
    	public void setQxName(String qxName) {
    		this.qxName = qxName;
    	}
    	public String getRoleName() {
    		return roleName;
    	}
    	public void setRoleName(String roleName) {
    		this.roleName = roleName;
    	}
    	public String getCreatetime() {
    		return createtime;
    	}
    	public void setCreatetime(String createtime) {
    		this.createtime = createtime;
    	}
    	public String toString() {
    		return "FileExcel [createtime=" + createtime + ", id=" + id
    				+ ", qxName=" + qxName + ", roleName=" + roleName + "]";
    	}
    	
    }
    

    2.html页面读取文件,并封装至数据实体(FileExcel)

    package com.example;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    
    import com.core.FileExcel;
    
    public class ReadExcel {
        
        public static void main(String[] args) {
            String path="D://permiss.xls";
            List<FileExcel> list=new ReadExcel().readExccel(path);
            for (FileExcel fileExcel : list) {
                System.out.println(fileExcel.toString());
            }
        }
        
        public List<FileExcel> readExccel(String path) {
            List<FileExcel> list=new ArrayList();
            try {
                BufferedReader buf=new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
                String line="";
                int i=0;
                while ((line=buf.readLine())!=null) {
                    FileExcel excel=new FileExcel();
                    String[] split=line.split("	");
                    excel.setQxName(split[1]);
                    excel.setRoleName(split[2]);
                    excel.setCreatetime(split[3]);
                    list.add(excel);
                }
            } catch (FileNotFoundException e) {
                list=null;
                System.out.println("找不到Excel");
            } catch (IOException e) {
                list=null;
                System.out.println("没有数据");
            }
            return list;
        }
    
    }

    第三步:插入数据。导入Excel数据功能已完成

  • 相关阅读:
    inndo 表与存储逻辑_1
    msyql master thread
    redo log重做日志缓冲
    redo log 重做日志
    Latex 写算法伪代码
    Just for test
    ASP.NET Web API 2 OData v4教程
    MVC系统过滤器 OutputCacheAttribute
    MVC系统过滤器、自定义过滤器
    .NET如何从配置文件中获取连接字符串
  • 原文地址:https://www.cnblogs.com/2070393244com/p/12775506.html
Copyright © 2011-2022 走看看