【问题标题】:how to upload single excel doc with multiple sheets to a multiple table using java如何使用java将带有多个工作表的单个excel文档上传到多个表
【发布时间】:2016-03-28 11:07:14
【问题描述】:

我正在尝试上传包含多张工作表的 excel 文档 例如文件名:工资单包含 表 0 表 1 等....

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

现在我正在尝试将地图表 0 映射到表 1 表 2 的表 1 等使用 java

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

例如代码:

/**
 * 
 */
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();

【问题讨论】:

  • 这里的上下文是什么? XSSFWorkbook 类来自什么包?您的代码在底部被剪辑,请将其缩减为显示问题的最小示例。

标签: java


【解决方案1】:

您的问题听起来像是您的代码适用于单张工作表。如果这是真的,那么获得第一张纸的那一行,

XSSFSheet sheet = book.getSheetAt(0);

可以更新以查看多个工作表,如this question 使用

book.getNumberOfSheets();

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

【讨论】:

    猜你喜欢
    • 2010-11-07
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    相关资源
    最近更新 更多