java - 使用循环更新 Excel 文件

标签 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));
        }
    }
}

所以我想更新我在不同单元格中选择的日期。所以如果我选择 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:在 Debug模式下,信息选择正确,问题是当我必须将它发送到Excel工作表时,它只发送最后一个日期4次而不是1个日期4次。

最佳答案

对于那些你需要答案的人来说,这对我来说很好。

    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));

关于java - 使用循环更新 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47636274/

相关文章:

c - 二维数组反转

java - 在 Eclipse RCP 中显示 View 时传递参数

java - weblogic 提供的依赖

java - 相机拍的照片越来越模糊

Java 8 函数 - 不兼容的类型

list - 循环遍历 R 中的数字列表

excel - 基于另一个单元格值的背景颜色和文本的条件格式

excel - 将图像从一个工作簿复制到另一个工作簿

arrays - 获取数组的整行

python - 如何在列表长度达到一定限制后停止追加值?