2017-07-04 64 views
2

我寫了一個讀取Excel模板的程序。其中第一列是隱藏的。現在,我有一個代碼以編程方式取消隱藏excel列(所以Column從A1開始)。有什麼辦法可以使用POI將水平滾動條移動到左側的工作表一列?

我正在使用Apache POI 3.16版本。

當我打開一個文件時,它應該向我展示一個來自A1的列,而不是它從B1列顯示我。 當我爲XLS編寫代碼時,它工作正常,但不適用於XLSX格式。

sheet.showInPane(0, 0); 

我需要手動移動水平滾動條來查看我的第一列。我應該如何通過編程實現自動滾動到XLSX格式的第一列?

這是我的完整代碼。

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; 
    } 
+1

見https://stackoverflow.com/questions/34974778/set-top-row-and-left-column-for-an-xssfsheet/ –

+0

感謝@Axel裏希特 –

回答

2

這是我的問題的答案。請參考本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; 
    } 
+1

它的工作我。 – jalpa

相關問題