2016-03-28 67 views
0

我试图上传Excel文档多张 如文件名一个Excel文档多张上传到多个表:工资包含 片0 表1和等....如何使用Java

和我有如表1,表2和等一多个数据库表.....

现在我试图在地图片0至表1 片1表2和等用java

我已经在一张桌子上使用单张纸

e.g代码:

/** 
* 
*/ 
private static final long serialVersionUID = 1L; 
public File file; 
private int totalRecords=0; 
private int successRecords=0; 
private int failureRecords=0; 

private String totalMsg="Total No. of records processed :"; 
private String successMsg="No. of records succeeded :"; 
private String failureMsg="No. of records failed:"; 



@SuppressWarnings("deprecation") 
public OutputStream receiveUpload(String filename, String mimeType) { 
    // Create upload stream 
    FileOutputStream fos = null; // Output stream to write to 
    try { 
     // Open the file for writing. 
     file = new File("" + filename); 
     fos = new FileOutputStream(file); 
    } catch (final java.io.FileNotFoundException e) { 
     UI.getCurrent().showNotification(
       "Could not open file<br/>", e.getMessage(), 
       Notification.TYPE_ERROR_MESSAGE); 
     return null; 
    } 
    return fos; // Return the output stream to write to 
} 

public void uploadSucceeded(SucceededEvent event) { 
    // Show the uploaded file in the image viewer 

    try{ 

     Session session = com.systems.payrolladmin.PayrolladminMainUI.sf.openSession(); 
     session.beginTransaction(); 
     //File excel = new File(FILE_PATH); 
     FileInputStream fis = new FileInputStream(file); 
     @SuppressWarnings("resource") 
     XSSFWorkbook book = new XSSFWorkbook(fis); 
     XSSFSheet sheet = book.getSheetAt(0); 
     Row row; 

     ArrayList<Resignee> ErrorDataList2 = new ArrayList<Resignee>(); 

     int LastRowNum=sheet.getLastRowNum(); 
     for(int i=1; i<=LastRowNum; i++){ 
      row = sheet.getRow(i); 

      String vempId1 = row.getCell(1).getStringCellValue(); 
      Date vdor1=row.getCell(3).getDateCellValue(); 
      Date vdorr=row.getCell(4).getDateCellValue(); 
      String vRemark = row.getCell(5).getStringCellValue(); 

      int a=5; 
      int b=5; 

      if(a==b) 
      { 

      Resignee resobj = new Resignee(); 
      resobj.setEmpId(vempId1); 
      resobj.setDOR(vdor1); 
      resobj.setDOReliv(vdorr); 
      resobj.setRemarks(vRemark); 
      session.save(resobj); 

      resobj=null; 
      successRecords++; 

      }else{ 

       Resignee error = new Resignee(); 
       error.setEmpId(vempId1); 
       error.setDOR(vdor1); 
       error.setDOReliv(vdorr); 
       error.setRemarks(vRemark); 
      error.setRemarks(vRemark); 

       ErrorDataList2.add(error); 
       error=null; 
       failureRecords++; 
      } 

      totalRecords++; 

     } 
     session.getTransaction().commit(); 
     session.close(); 
     fis.close(); 
     //write to excel 

     @SuppressWarnings("resource") 
     XSSFWorkbook workbook = new XSSFWorkbook(); 
     XSSFSheet spreadsheet = workbook.createSheet("employe db"); 
     XSSFRow xrow=spreadsheet.createRow(0); 
     XSSFCell cell; 

     cell=xrow.createCell(0); 
     cell.setCellValue("EMPLOYEE ID"); 
     cell=xrow.createCell(1); 

     cell.setCellValue("DOR"); 
     cell=xrow.createCell(2); 
     cell.setCellValue("DORELIEVE"); 
     cell=xrow.createCell(3); 
     cell.setCellValue("REMARKS"); 




     int i=1; 
     for (Resignee nobj : ErrorDataList2) { 

      xrow=spreadsheet.createRow(i); 

      cell=xrow.createCell(0); 
      cell.setCellValue(nobj.getEmpId()); 

      cell=xrow.createCell(1); 
      cell.setCellValue(nobj.getDOR()); 
      cell=xrow.createCell(2); 
      cell.setCellValue(nobj.getDOReliv()); 
      cell=xrow.createCell(3); 
      cell.setCellValue(nobj.getRemarks()); 


      i++; 

     } 

     FileOutputStream out = new FileOutputStream(
     new File("F:\\Error Excel\\ResingeeError.xlsx")); 
     workbook.write(out); 
     out.close(); 
+0

这里的内容是什么? “XSSFWorkbook”类来自哪个包?你的代码被截断在底部,请把它缩小到展示问题的最小例子。 – szym

回答

0

你的问题听起来好像你的代码工作单张纸。如果这是真的,那么得到的第一张行,

XSSFSheet sheet = book.getSheetAt(0); 

可以更新一下多张工作表如图this question使用

book.getNumberOfSheets(); 

获得工作簿张数然后分别处理每个分别是您已经在使用工作表0