文件上传Excel

来源:互联网 发布:淘宝网怎么看卖家电话 编辑:程序博客网 时间:2024/04/28 03:41

前台:       <-input type="file" name="fileName" id="fileName" style="width: 65%" value="Browse"/->

提交方式:

document.getElementByIdx_x_x_x("form1").action = cp

                           + "/auth/apply/ApplyByFileAction!uploadExcel.shtm";

   document.getElementByIdx_x_x_x("form1").submit()

后台:

读取文件: private File fileName;

FileInputStream input = new FileInputStream(fileName);

org.apache.poi.ss.usermodel.Workbook work;

                           work = WorkbookFactory.create(input);

                           Sheet sheet = ((org.apache.poi.ss.usermodel.Workbook) work).getSheetAt(0);

                           int checknum = 0; 

                           int checkFlag = 0; 

                           int flagAll = 0;

                                       if (sheet != null) {

                                                               int lastRow = sheet.getLastRowNum();

                                                               Row row;

                                                               Cell cell;

                                                               String str;

                                                                        row = sheet.getRow(0);

输出文件(如果文件存在则删除):File file = new File(upload_path + map1.get("LINE_ID"));

                                                            if (file.exists() && file.isFile()) {

                                                                        file.delete();

                                                            }

                                                            FileService.createFolder(upload_path);

                                                            int bytesum = 0;

                                            int byteread = 0;

                                                            byte[] buffer = new byte[1024];

                                                           

                                                InputStream input2 = new FileInputStream(fileName);

FileOutputStream fos = new FileOutputStream(upload_path+map1.get("LINE_ID"));

                                                            while ( (byteread = input2.read(buffer)) != -1) {

                               bytesum += byteread;

                               fos.write(buffer, 0, byteread);

                           }

                           fos.close();

 

                           input2.close();

0 0