如何使用java将带有多张表的单个excel文档上载到多个表中

bzzcjhmw  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(226)

我正试图上传一个excel文档与多张表,例如文件名:工资单,其中包含表0表1等。。。。
我有一个多数据库表,比如表1,表2等等。。。。。
现在我尝试使用java将表0Map到表1,表2的表1等等
我已经在一张table上用一张纸了
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();
woobm2wo

woobm2wo1#

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

XSSFSheet sheet = book.getSheetAt(0);

可以更新为查看本问题所示的多张图纸

book.getNumberOfSheets();

要获取工作簿中的工作表数,然后分别处理每个工作表,您已经在使用工作表0了

相关问题