简体   繁体   English

Apache-POI编写工作簿后的问题

[英]issue after writing workbook with Apache-POI

I've a problem after inserting the data into the sheet of an excel template with the .xlsm extension. 将数据插入扩展名为.xlsm的excel模板的工作表后,出现问题。 The code works. 该代码有效。 It correctly copies the data in question, but when I try to run a specific macro, which takes the imported data, it does not recognize all rows, only the first two. 它正确地复制了有问题的数据,但是当我尝试运行一个特定的宏(该宏使用导入的数据)时,它无法识别所有行,只能识别前两行。

However, if I copy and paste any cells in the second column, eg if I copy B2 in B2 and execute the macro, it recognizes the total number of inserted rows. 但是,如果我在第二列中复制并粘贴任何单元格,例如,如果我在B2中复制B2并执行宏,它将识别出插入行的总数。 Can you help me? 你能帮助我吗? I hope I was clear. 我希望我很清楚。

Here is my Function Code 这是我的功能代码

public String generarExcelSalida(String codCampaña,JDateChooser fechaParametro,String tfOutFolder){
String fileName="Modelo Caida "+codCampaña+".xlsm";
String exitFilePath=tfOutFolder+"\\"+fileName;
FileInputStream fileIn=null;
FileOutputStream fileOut=null;
SimpleDateFormat dParam=new SimpleDateFormat("dd/MM/yyyy");
String dateParam = dParam.format(fechaParametro.getDate()).toString();
ResultSet rs=DBManager.ConsultaCaida(dateParam, codCampaña);
try {
    // Here I'm opening my template file
    fileIn=new FileInputStream(FileManagement.carpetaTemplate+"\\Template_Caidas.xlsm");

    XSSFWorkbook workbook=new XSSFWorkbook(OPCPackage.open(fileIn));
    XSSFSheet sheet9=workbook.getSheet("BASE_DATOS");

    int i=1; // This parameter is used to getRow(i)
    while(rs.next()){
        XSSFRow row = sheet9.getRow(i);

        XSSFCell cell1=row.getCell(0);
        XSSFCell cell2=row.getCell(1);
        XSSFCell cell3=row.getCell(2);
        XSSFCell cell4=row.getCell(3);
        XSSFCell cell5=row.getCell(4);
        XSSFCell cell6=row.getCell(5);
        //Writing de ResultSet values on Sheet "BASE_DATOS"
        cell1.setCellValue(rs.getString("Producto Comercial"));
        cell2.setCellValue(rs.getInt("Nro Certificados"));
        cell3.setCellValue(rs.getString("DefEstado"));
        cell4.setCellValue(rs.getDate("Año-Mes Venta"));
        cell5.setCellValue(rs.getDate("Año-Mes Inicio Vigencia"));
        cell6.setCellValue(rs.getDate("AM Estado"));
        i++;// next Sheet.row

        }

    fileOut=new FileOutputStream(exitFilePath);
    //writing the workbook in a new file, it is like SAVE AS
    workbook.write(fileOut);
    if(fileOut!=null) fileOut.close();
    if(fileIn!=null) fileIn.close();
} catch (IOException | InvalidFormatException | SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
//return the path of the new file
return exitFilePath;
}

Here is the macro's code 这是宏的代码

Private Sub Base_Datos()
Dim Numero_Registro As Integer
Dim Celda As Range
Dim Columna, Fila As Integer

Sheets("BASE_DATOS").Select

' Busqueda del limite de Filas
Fila = Range("Numero_Registro") + 1

' Busqueda del limite de Columnas
Set Celda = Range("1:1").Find("BAJAS")
Columna = Celda.Column

With Range("a1")
' Copiar Pegar
Range(.Cells(2, Columna), .Cells(2, 100)).Select
Selection.Copy

Range(.Cells(3, Columna), .Cells(Fila, 100)).Select
Selection.PasteSpecial Paste:=xlFormulas

End With
End Sub

So from your comments the issue is about the needed recalculation because the macro relies on a cell value which uses COUNTA to count filled cells. 因此,根据您的评论,问题在于所需的重新计算,因为宏依赖于使用COUNTA来计算填充单元格的单元格值。

Two possible solutions: 两种可能的解决方案:

First: You could use a FormulaEvaluator to let evaluate the formulas from apache poi before saving the workbook: 首先:在保存工作簿之前,您可以使用FormulaEvaluatorapache poi评估公式:

...
    //writing the workbook in a new file, it is like SAVE AS
    XSSFFormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
    evaluator.evaluateAll();
    workbook.write(fileOut);
...

This may fail dependent of the kind of the formulas. 取决于公式的种类,这可能会失败。

If it fails using evaluateAll() you could at least let evaluate the cell with the COUNTA formula. 如果使用evaluateAll()失败,则至少可以使用COUNTA公式评估单元格。 This should not fail since apache poi supports COUNTA function. 这应该不会失败,因为apache poi支持COUNTA功能。

Read Formula Evaluation for how to do. 阅读公式评估以了解操作方法。

Second: You could force Excel to do the recalculation while it is opening the file: 第二:您可以强制Excel在打开文件时进行重新计算:

...
    //writing the workbook in a new file, it is like SAVE AS
    workbook.setForceFormulaRecalculation(true);
    workbook.write(fileOut);
...

This may lead to long time process while opening the file. 在打开文件时,这可能会导致长时间的处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM