2 votes

Existe-t-il un moyen de déplacer la barre de défilement horizontale de la feuille d'une colonne vers la gauche en utilisant le POI ?

J'ai écrit un programme qui lit une feuille de modèle Excel. Dans lequel la première colonne était cachée. Maintenant, j'ai un code qui dé-cache la colonne Excel programmatiquement (donc la colonne commence à partir de A1).

J'utilise la version 3.16 de Apache POI.

Lorsque j'ouvre un fichier, il devrait me montrer une colonne de A1, mais il me montre plutôt une colonne de B1. Lorsque j'écris le code ci-dessous pour XLS, il fonctionne correctement mais ne fonctionne pas pour un format XLSX.

sheet.showInPane(0, 0);

Je dois déplacer manuellement la barre de défilement horizontale pour afficher ma première colonne. Comment dois-je procéder par programme pour faire défiler automatiquement la première colonne au format XLSX ?

Voici mon code complet.

public Workbook readWorkBookAndWriteErrors(String bufId,String inputFile, String ext) throws Exception {
        Workbook workBook =null;
        Sheet sheet = null; 
        if(GlobalVariables.EXCEL_FORMAT_XLS.equalsIgnoreCase(ext)){
             // Get the workbook instance for XLS file
            workBook = new HSSFWorkbook(new FileInputStream(inputFile));
        }else{
            // Get the workbook instance for XLSX file
            workBook = new XSSFWorkbook(new FileInputStream(inputFile));
        }
        sheet = workBook.getSheetAt(0);
        Row row = null;
        if(sheet.isColumnHidden(0)){
            sheet.setColumnHidden(0, false);
            sheet.setActiveCell(new CellAddress("A1"));
            sheet.showInPane(0, 0);
            sheet.createFreezePane(0, 1);

             Iterator<Row> rowIterator = sheet.iterator();
                int rowIndex = 1;
                while (rowIterator.hasNext()) {
                    row = rowIterator.next();
                    if(rowIndex == 1){
                        rowIndex++;
                        continue;
                    }
                    Cell cell = row.createCell(0);
                    cell.setCellValue("error message");
                    rowIndex++;
                }
        }
        return workBook;
    }

2voto

michaeln peterson Points 100

Voici la réponse à ma question. Veuillez vous référer à ceci Source :

public Workbook readWorkBookAndWriteErrors(String bufId,String inputFile, String ext) throws Exception {
        Workbook workBook =null;
        Sheet sheet = null; 
        if(GlobalVariables.EXCEL_FORMAT_XLS.equalsIgnoreCase(ext)){
             // Get the workbook instance for XLS file
            workBook = new HSSFWorkbook(new FileInputStream(inputFile));
        }else{
            // Get the workbook instance for XLSX file
            workBook = new XSSFWorkbook(new FileInputStream(inputFile));
        }
        sheet = workBook.getSheetAt(0);
        Row row = null;
        if(sheet.isColumnHidden(0)){
            sheet.setColumnHidden(0, false);

            if(sheet instanceof XSSFSheet){
                CTWorksheet ctWorksheet = null; 
                CTSheetViews ctSheetViews = null; 
                CTSheetView ctSheetView = null; 
                XSSFSheet tempSheet = (XSSFSheet) sheet;
                 // First step is to get at the CTWorksheet bean underlying the worksheet. 
                ctWorksheet = tempSheet.getCTWorksheet(); 
                // From the CTWorksheet, get at the sheet views. 
                ctSheetViews = ctWorksheet.getSheetViews(); 
                // Grab a single sheet view from that array 
                ctSheetView = ctSheetViews.getSheetViewArray(ctSheetViews.sizeOfSheetViewArray() - 1); 
                // Se the address of the top left hand cell. 
                ctSheetView.setTopLeftCell("A1"); 
            }else{
                 sheet.setActiveCell(new CellAddress("A1"));
                 sheet.showInPane(0, 0);
            }

             Iterator<Row> rowIterator = sheet.iterator();
                int rowIndex = 1;
                while (rowIterator.hasNext()) {
                    row = rowIterator.next();
                    if(rowIndex == 1){
                        rowIndex++;
                        continue;
                    }
                    Cell cell = row.createCell(0);
                    cell.setCellValue("error message");
                    rowIndex++;
                }
        }
        return workBook;
    }

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X