使用循环更新Excel文件

时间:2017-12-04 15:07:03

标签: java excel loops date xls

首先,感谢你能给我的所有答案。

我的问题似乎很容易,但我是Java的初学者。我必须用一些日期更新我的Excel文件。但是当我在循环中时,所有信息都会在循环结束时发送到excel表。

private void calculateExcelStaffingHistoriqueStockDetail(final List<StockVo> listStock, final WritableSheet sheet, boolean isMois, int jourD, int moisD, int anneeD, int taillePeriode) throws WriteException {

    // Set Excel cell format header
    //final String[] tabDomaine = { "Aucun Domaine", "Contrat/Produit", "Comptabilité", "Rég.Finance", "Sinistre", "Interface" };

    // Create a cell format for Arial 10 point font
    final WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10);
    final WritableCellFormat arial10format = new WritableCellFormat(arial10font);

    arial10format.setBorder(Border.ALL, BorderLineStyle.THIN);

    for (int row = 0; row < listStock.size(); row++) {

        final StockVo stock = listStock.get(row);
        String ch = "";
        int i = 0;
        // Vue Mensuel
        if (isMois) {               
            for (i = moisD; i < taillePeriode + moisD; i++) {
                if (i <= 12) {
                    if (i < 10) {
                        ch = "0" + i + "/" + anneeD;
                    } else {
                        ch = i + "/" + anneeD;
                    }
                } else {
                    i = i - 12;
                    if (i < 10) {
                        ch = "0" + (i - 12) + "/" + (anneeD + 1);
                    } else {
                        ch = (i - 12) + "/" + (anneeD + 1);
                    }
                }
                sheet.addCell(new Label(3 + row, 6, ch, arial10format));
            }
        // Le totaux des entrees, livraisons, stock et en attentes de tout les domaines
        sheet.addCell(new Label(1, 7, "Total", arial10format));
        sheet.addCell(new Label(2, 7, "Total", arial10format));
        sheet.addCell(new Label(2, 8, "Stock", arial10format));
        sheet.addCell(new Label(2, 9, "En Attentes", arial10format));
        sheet.addCell(new Label(2, 10, "Entrées", arial10format));
        sheet.addCell(new Label(2, 11, "Sortie", arial10format));
        sheet.addCell(new Label(3 + row, 7, Integer.toString(stock.getStock() + stock.getAttentes() + stock.getEntrees() + stock.getLivraisons()), arial10format));
        sheet.addCell(new Label(3 + row, 8, Integer.toString(stock.getStock()), arial10format));
        sheet.addCell(new Label(3 + row, 9, Integer.toString(stock.getAttentes()), arial10format));
        sheet.addCell(new Label(3 + row, 10, Integer.toString(stock.getEntrees()), arial10format));
        sheet.addCell(new Label(3 + row, 11, Integer.toString(stock.getLivraisons()), arial10format));
        }
    }
}

所以我想更新我在另一个Cell中选择的日期。所以,如果我选择4个月,我必须有01/01 / 17,01 / 02 / 17,01 / 03 / 17,01 / 04/17。

但实际上我有这个:01/04 / 17,01 / 04 / 17,01 / 04 / 17,01 / 04/17。

你知道为什么“sheet.addCell(new Label(3 + row,6,ch,arial10format));”是在最后更新,而不是每次都选择不同的日期? (PS:在调试模式下,信息正确选择,问题是当我必须将其发送到Excel工作表时,它只发送最后一次日期4次而不是1次日期4次。

1 个答案:

答案 0 :(得分:0)

对于那些您需要回答的人,这对我来说很好用。

    String ch = "";
    for (int row = 0; row < listStock.size(); row++) {

        final StockVo stock = listStock.get(row);

        // Vue Mensuel
        if (isMois) {               
            //for (int i = moisD; i < taillePeriode + moisD; i++) {
                final int mois = (row % 12) + 1;
                final int annee = row / 12;

                    if (mois <= 9) {

                        ch = "0" + mois + "/" + (anneeD + annee);

                    } else  {
                        ch = mois + "/" + (anneeD + annee);
                    }

                sheet.addCell(new Label(3 + row, 6, ch, arial10format));