创建工作簿时在新工作表中写入元素

时间:2019-10-15 08:40:28

标签: java excel jxl

我实际上正在尝试在创建的工作簿上的工作表中插入Excel文件(功能已经完成并且工作正常)。

我创建了一个小循环,可创建5张纸,但是由于纸内的功能,我不知道如何插入。

这是我在Workbook中创建和插入元素的主要功能。

函数exportSyntheseProjetPvValide:

//Function to create the main Excel file
@GET
@Path("synthese-projet-PvValide/{annee:\\d+}/{mois:\\d+}/{statusProject:\\d+}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response exportSyntheseProjetPvValide(@PathParam("annee") final int annee, @PathParam("mois") final int mois,
        @PathParam("statusProject") final int status, @Context final SecurityContext context) throws Exception {

    // Get data base data
    final List<IndicateursGlobal> dataStaffing = indicateursResource.getIndicateurSynthese(annee, mois, status, context);

    WritableWorkbook copy = null;

    // Excel export
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();

    try {
        final Workbook workbook = Workbook.getWorkbook(new ClassPathResource("exportModel/syntheseGlobalePvValide.xls").getInputStream());
        copy = Workbook.createWorkbook(outStream, workbook, new WorkbookSettings());

        final WritableSheet sheetStaffing = copy.getSheet(0);

        //test to create 5 sheets
        for (int i = 0; i <= 5; i++) {
            copy.createSheet("sheet number " + i + " test", i);
            //Is it here that i can called a function to insert inside for my needs ? 
        }


        sheetStaffing.setName("syntheseGlobalePvValide");

        calculateExcelStaffingSynthesePvValide(dataStaffing, sheetStaffing);
        copy.write();


    } finally {
        if (copy != null) {
            copy.close();
        }
    }

    return Response.ok(new ByteArrayInputStream(outStream.toByteArray())).header(STR_TYPE_HEADER, "attachment; filename=syntheseGlobalePvValide.xls")
            .type(STR_TYPE_DOCUMENT).build();

}

这是我需要用于插入工作表的功能:

/**
 * @param id
 * @param context
 * @param uriInfo
 * @return
 * @throws InstantiationException
 * @throws IllegalAccessException 
 * @throws ClassNotFoundException
 * @throws IOException
 * @throws BiffException
 * @throws IndexOutOfBoundsException
 * @throws WriteException
 */
@GET
//Function that I must insert in sheet on inside the workbook
private ByteArrayOutputStream doExportBilan(final int id, final SecurityContext context, final UriInfo uriInfo) throws InstantiationException,
        IllegalAccessException, ClassNotFoundException, IOException, BiffException, IndexOutOfBoundsException, WriteException {
    final ProjectGroupamaDetailsVo projet = ProjectGroupamaResource.TO_BUSINESS_CONVERTER_DETAILS.apply(repositoryProjet.findOneExpected(id));


    final List<Delivery> listDeliv = repositoryDelivery.findByIdProjet(projet.getId());

    final List<SaisieAnomalieProjetDisplayVo> listAnos = Lists.transform(saisieAnomalieProjetRepository.findByIdProjet(projet.getId()),
            SaisieAnomalieProjetResource.TO_BUSINESS_DISPLAY_CONVERTER);

    final List<SaisieSyntheseVo> listSaisie = saisieProjetResource.getSyntheseSaisie(uriInfo, context, String.valueOf(projet.getId()));

    final List<SaisieDemandeChangement> listDem = saisieDemandeChangementResource.findAll(uriInfo, projet.getId()).getAaData();

    final Calendar cal = Calendar.getInstance();
    final int annee = cal.get(Calendar.YEAR);
    final int mois = cal.get(Calendar.MONTH) + 1;

    final IndicateursGlobal indic = indicateursResource.getIndicateurs(projet.getId(), annee, mois);

    WritableWorkbook copy = null;

    // Excel export
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();

    try {
        final WorkbookSettings ws = new WorkbookSettings();
        ws.setEncoding("Cp1252");

        final Workbook workbook = Workbook.getWorkbook(new ClassPathResource("exportModel/bilanProjet.xls").getInputStream(), ws);
        copy = Workbook.createWorkbook(outStream, workbook, new WorkbookSettings());

        final WritableSheet sheetStaffing = copy.getSheet(0);

        //Name project inside sheet Excel
        final String projetNom = projet.getName();
        sheetStaffing.setName("Projet_" + projetNom);

        calculateExcelBilanProjet(projet, listDeliv, listSaisie, listAnos, indic, listDem, sheetStaffing);

        copy.write();

    } finally {
        if (copy != null) {
            copy.close();
        }
    } 
    return outStream;
}

我将此功能添加到该功能中:

//Acces to the second function thnaks to the PATH
@GET
@Path("bilan-projet/{id:\\d+}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response exportBilanProjet(@PathParam("id") final int id, @Context final SecurityContext context, @Context final UriInfo uriInfo)
        throws Exception {

    // final List<Project> listProject = projectRepository.findAll();
    final ProjectGroupamaDetailsVo projet = ProjectGroupamaResource.TO_BUSINESS_CONVERTER_DETAILS.apply(repositoryProjet.findOneExpected(id));
    final ByteArrayOutputStream outStream = doExportBilan(id, context, uriInfo);

    //Name project when the export is done
    final String projetNom = projet.getName();
    String nomDevis = projet.getNomDevis();
    String statut = "";


    if (projet.getStatutProjet() == 3 || projet.getStatutProjet() == 8 || projet.getStatutProjet() == 9) {
        statut = "PV réception définitive  V1";
    } else {
        statut = "PV fin garantie V1";
    }

    if (nomDevis == null || nomDevis.equals("")) {
        nomDevis = projetNom;
    }

    return Response.ok(new ByteArrayInputStream(outStream.toByteArray())).header(STR_TYPE_HEADER, "attachment; filename=" + nomDevis + "-EXI-" + statut + ".xls")
            .type(STR_TYPE_DOCUMENT).build();
}

因此,当我单击创建Excel文件的按钮时,应具有一个包含内部工作表的主文件Excel(第二个功能),具体取决于我单击下载文件时表格中的元素数量(Javascript部分)。

0 个答案:

没有答案