1.SQL数据库的数据直接复制到excel中
可以实现在查询分析器中通过语句查询出所有你需要的数据,然后将其复制到excel中(包括列名),具体步骤如下:1、2000的查询分析器默认是以“表格显示结果”,右键,改为“文本显示结果”;2、执行SELECT语句,结果(包括表头)以文本显示,全选并复制;3、粘贴到Excel中,点击出现在右下角的图标,选择“使用文本导入向导”;4、弹出的对话框可以定制分隔符,因为默认就是空格,所以直接点完成;5、已经按表格的方式排列好,删除多余的两行数据。
具体的你点击参考链接的地址到那里去看,有详细的截图说明。.。
2.如何将数据库的表导出生成Excel
方法一:对于会写查询SQL语句的则是直接写查询语句查询出结果。如下图:
接下来到桌面新建一个空的excel文件。
接下来回到SQLServer查询界面。鼠标右键点击查询结果的左上角空白列,点击“连同标题一起复制”。则已复制好了数据。如下图:
接下来打开之前建好的Excel文件把复制好的数据黏贴到excel即可。如下图:
方法二:对于不会写查询SQL语句的则使用数据库导出方法。
选中要导出数据的数据库,点击右键会弹出一个右键菜单。点击“任务”再点击“导出数据(x)”。如下图:
点击了“导出数据(x)”后会弹出一个界面,并填写好信息。如下图:
点击下一步跳转到如下图1中的界面。在目标中选中excel类型,界面信息则发生改变,并选中导出的excel路径。如下图2。
点击下一步跳转至如下界面,选中第一个选项。如下图:
点击下一步,勾选要导出的表。如下图:
点击下一步,跳转至如下界面:
点击下一步:跳转到如下图1界面,选择下一步在点击完成,则会跳到如下图2界面。证明已经导出成功了。
打开excel就可用看到导出的Excel了。如下图:
3.如何用java导入excel数据到数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 //从excle文档中,将值导入至list数组 //xlsPath 路径 从前台获取 //Excle导入 publicList loadScoreInfo(String xlsPath) throwsIOException{ List temp = newArrayList(); FileInputStream fileIn = newFileInputStream(xlsPath); //根据指定的文件输入流导入Excel从而产生Workbook对象 Workbook wb0 = newHSSFWorkbook(fileIn); //获取Excel文档中的第一个表单 Sheet sht0 = wb0.getSheetAt(0); //对Sheet中的每一行进行迭代 for(Row r : sht0) { //如果当前行的行号(从0开始)未达到2(第三行)则从新循环 if(r.getRowNum()<1){ continue; } //创建实体类 TblUser info=newTblUser(); //取出当前行第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()); } 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(); returntemp; } publicvoidztree() { } //导出当前页的数据 publicvoidexportPage(){ List list=newArrayList(); String[] slist=daor.split(","); //将页面获取到的id集合遍历,循环添加到list中,进行本页数据到导出 for(inti = 0; i < slist.length; i++) { list.add(tus.findById(java.lang.Integer.parseInt(slist[i]))); } tus.export(list); } /** * 导入Excle到数据库 * @return null 不然有可能报错! * @throws IOException */ publicvoidimportExcle() throwsIOException{ //调用导入文件方法并存入数组中 ints=0;//得到成功插入的条数 inti=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); } }。