servlet讀取excel中的數據並添加到sqlserver數據庫


前台上傳文件的jsp頁面(部分):

<form action="importExcel" enctype="multipart/form-data" method="post">
     選擇文件: <input type="file" name="excelfilename"><br>
     請選擇年份:<input  type="text" name="qwe" id="qwe" onClick="WdatePicker({dateFmt:'yyyy'})" /><br>     <!--這里使用的是時間控件-->
     <input type="submit" value="數據入庫"/>
</form>

servlet的配置文件web.xml(部分):

<servlet>
    <servlet-name>importExcel</servlet-name>
    <servlet-class>com.ImportExcel</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>importExcel</servlet-name>
    <url-pattern>/importExcel</url-pattern>
</servlet-mapping>

servlet的處理頁面(部分):

 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        String zhi=null;
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        try {
            List  items = upload.parseRequest(request);
            InputStream is = null;
            Iterator iter = items.iterator();
            while (iter.hasNext()) {
                FileItem item = (FileItem) iter.next();
                if (!item.isFormField()) {
                    is = item.getInputStream();
                }else{
                    if(item.getFieldName().equals("qwe")){
                    zhi=item.getString("gb2312");
                    System.out.println(zhi);
                  }
                }
            }
            Workbook workbook = Workbook.getWorkbook(is);
            Sheet sheet = workbook.getSheet(0);
            int rows=sheet.getColumns();
            Cell ce00=((jxl.Sheet) sheet).getCell(1,14);
            Cell ce01=((jxl.Sheet) sheet).getCell(2,14);
            Cell ce02=((jxl.Sheet) sheet).getCell(3,14);
            Cell ce03=((jxl.Sheet) sheet).getCell(4,14);
            Cell ce04=((jxl.Sheet) sheet).getCell(5,14);
            String cel00=ce00.getContents();
            String cel01=ce01.getContents();
            String cel02=ce02.getContents();
            String cel03=ce03.getContents();
            String cel04=ce04.getContents();
            Connection conn=basedao.openCon(); //調用鏈接數據庫的文件
            PreparedStatement ps=conn.prepareStatement("insert into CD_BI_ZYLSTJB(heji,zzls,jzls,gzls,year) values(?,?,?,?,?)");
            ps.setString(1,cel00);
            ps.setString(2,cel01);
            ps.setString(3,cel02);
            ps.setString(4,cel03);
            ps.setString(5,zhi);
            ps.execute();
            ps.close();
            conn.close();
response.sendRedirect("/test/index.jsp");
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM