日期:2014-05-16 浏览次数:20564 次
private File file; private String fileFileName; private int stdnum; //这条数据是向数据库中增加的常量 public int getStdnum() { return stdnum; } public void setStdnum(int stdnum) { this.stdnum = stdnum; } public File getFile() { return file; } public void setFile(File file) { this.file = file; } public String getFileFileName() { return fileFileName; } public void setFileFileName(String fileFileName) { this.fileFileName = fileFileName; } public String shengcheng() { String root = ServletActionContext.getRequest().getRealPath("/upload"); String result = "success"; stdnum = this.getStdnum(); String user = null; String pwd = null; /** Excel文件的存放位置。注意是正斜线*/ String fileToBeRead = root+"/"+fileFileName; try{ // 创建对Excel工作簿文件的引用 XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileToBeRead)); // 创建对工作表的引用。 // 本例是按名引用(让我们假定那张表有着缺省名"Sheet1") XSSFSheet sheet = workbook.getSheet("stdusers"); int j = 1; //第在excel中读取一条数据就将其插入到数据库中 while(j<sheet.getPhysicalNumberOfRows()) { XSSFRow row = sheet.getRow(j); for(int i = 0 ; i <= 1 ;i++) { XSSFCell cell = row.getCell((short)i); if(i == 0) user = cell.getStringCellValue(); else pwd = cell.getStringCellValue(); } j ++; Connection conn = new DBUtils().getConnection(); String sql = "INSERT into stdcontestuser (userid,password,contestid) values ('"+user+"','"+pwd+"','"+stdnum+"')"; java.sql.Statement sm = conn.createStatement(); sm.execute(sql); } }catch(Exception e) { System.out.println("已运行xlRead() : " + e ); result="fail"; } return result; }