乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 用javascript怎么实现把excel中的数据批量导入到...-前台excel导入,excel怎么导入数据

用javascript怎么实现把excel中的数据批量导入到...-前台excel导入,excel怎么导入数据

作者:乔山办公网日期:

返回目录:excel表格制作


上传临时文件 后台取

利用jquery里的ocupload这个copyjs组件,步骤和实现文件上传一样,将你要导入的excel表格进行上传到服务端,然后在工程里导入apache的poi这poi-ooxml以及poi-schemas这三个jar包,接受到文件对象后,利用里面的HSSWorkBook这个对象来解析excel里的每行数据,通过对应的数据类型对象添加到数据库里即可
//从excle文档中,将值导入至list数组
//xlsPath 路径 从前台获取
//Excle导入
public List<TblUser> loadScoreInfo(String xlsPath) throws IOException{
    List temp = new ArrayList();
FileInputStream fileIn = new FileInputStream(xlsPath);
//根据指定的文件输入流导入Excel从而产生Workbook对象
Workbook wb0 = new HSSFWorkbook(fileIn);
//获取Excel文档中的第一个表单
Sheet sht0 = wb0.getSheetAt(0);
//对Sheet中的每一行进行迭代
        for (Row r : sht0) {
        //如果当前行的行号(从0开始)未达到2(第三行)则从新循环
if(r.getRowNum()<1){
continue;
}
//创建实体类
TblUser info=new TblUser();
//取出当前行第1个单元格数据,并封装在info实体stuName属性上
if(r.getCell(0)!=null){
 r.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
     info.setId(Integer.parseInt(r.getCell(0).getStringCellValue()));
}
//同上
if(r.getCell(1)!=null){
     r.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
     info.setUsername(r.getCell(1).getStringCellValue());
}
if(r.getCell(2)!=null){
     r.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
     info.setPassword(r.getCell(2).getStringCellValue());
e799bee5baa6e997aee7ad94e78988e69d83339}
if(r.getCell(3)!=null){
     r.getCell(3).setCellType(Cell.CELL_TYPE_STRING);
     info.setState(r.getCell(3).getStringCellValue());
}
if(r.getCell(4)!=null){
     r.getCell(4).setCellType(Cell.CELL_TYPE_STRING);
     info.setRename(r.getCell(4).getStringCellValue());
}
if(r.getCell(5)!=null){
     r.getCell(5).setCellType(Cell.CELL_TYPE_STRING);
     info.setEmail(r.getCell(5).getStringCellValue());
}
 temp.add(info);
        }
        fileIn.close();    
        return temp;    
    }

 public void ztree()
 {
 
 }
//导出当前页的数据
public void exportPage(){
List<TblUser> list=new ArrayList<TblUser>();
String[] slist=daor.split(",");
//将页面获取到的id集合遍历,循环添加到list中,进行本页数据到导出
for (int i = 0; i < slist.length; i++) {
list.add(tus.findById(java.lang.Integer.parseInt(slist[i])));
}
tus.export(list);
}

/**
 * 导入Excle到数据库
 * @return null 不然有可能报错!
 * @throws IOException
 */

public void importExcle() throws IOException{
//调用导入文件方法并存入数组中
int s=0;//得到成功插入的条数
int i=0;//得到共有多少条
/**
 * 将不符合格式的数据错误信息存入数组中!
 * 格式要求:
 * 用户名,密码不能为空!
 * 用户名不能和已存在的用户名重复,长度在5-18位之间
 * 密码长度在6-18位之间
 */
String errors="";//保存导入失败信息
try {
System.out.println("进入方法!");

lists=this.loadScoreInfo(path);
System.out.println(path);
for (TblUser u : lists) {
i++;
if(u.getUsername()==""){
    errors+="第"+i+"条数据的用户名为空,导入失败!";//^^:分割符
    continue;
}
if(u.getPassword()==""){
errors+="第"+i+"条数据的密码为空,导入失败!";//^^:分割符
continue;
}
if(tus.findByName(u.getUsername())){
errors+="第"+i+"条数据的用户名 已存在,导入失败!";//^^:分割符
continue;
}
if(u.getUsername().length()<5 || u.getUsername().length()>20){
errors+="第"+i+"条数据的用户名格式错误,导入失败!";//^^:分割符
continue;
}
if(u.getPassword().length()<6 || u.getPassword().length()>20){
errors+="第"+i+"条数据的密码格式错误,导入失败!";//^^:分割符
continue;
}
s++;
tus.save(u);//将数组中的数据添加到数据库中!
}

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
System.out.println("错误:"+errors);
}

        
}  


在数据库中有导入的选项

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网