Advertisement
Guest User

alanwilliamson

a guest
Oct 22nd, 2009
4,706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1. package org.alanwilliamson.openbd.plugin.spreadsheet;
  2.  
  3. import org.apache.poi.ss.usermodel.Cell;
  4. import org.apache.poi.ss.usermodel.Row;
  5. import org.apache.poi.ss.usermodel.Sheet;
  6.  
  7.  
  8. /*
  9.  * Helper functions to aid in the management of sheets
  10.  */
  11. public class SheetUtility extends Object {
  12.  
  13.    
  14.     /**
  15.      * Given a sheet, this method deletes a column from a sheet and moves
  16.      * all the columns to the right of it to the left one cell.
  17.      *
  18.      * Note, this method will not update any formula references.
  19.      *
  20.      * @param sheet
  21.      * @param column
  22.      */
  23.     public static void deleteColumn( Sheet sheet, int columnToDelete ){
  24.         int maxColumn = 0;
  25.         for ( int r=0; r < sheet.getLastRowNum()+1; r++ ){
  26.             Row row = sheet.getRow( r );
  27.            
  28.             // if no row exists here; then nothing to do; next!
  29.             if ( row == null )
  30.                 continue;
  31.            
  32.             // if the row doesn't have this many columns then we are good; next!
  33.             int lastColumn = row.getLastCellNum();
  34.             if ( lastColumn > maxColumn )
  35.                 maxColumn = lastColumn;
  36.            
  37.             if ( lastColumn < columnToDelete )
  38.                 continue;
  39.            
  40.             for ( int x=columnToDelete+1; x < lastColumn + 1; x++ ){
  41.                 Cell oldCell    = row.getCell(x-1);
  42.                 if ( oldCell != null )
  43.                     row.removeCell( oldCell );
  44.                
  45.                 Cell nextCell   = row.getCell( x );
  46.                 if ( nextCell != null ){
  47.                     Cell newCell    = row.createCell( x-1, nextCell.getCellType() );
  48.                     cloneCell(newCell, nextCell);
  49.                 }
  50.             }
  51.         }
  52.  
  53.        
  54.         // Adjust the column widths
  55.         for ( int c=0; c < maxColumn; c++ ){
  56.             sheet.setColumnWidth( c, sheet.getColumnWidth(c+1) );
  57.         }
  58.     }
  59.    
  60.    
  61.     /*
  62.      * Takes an existing Cell and merges all the styles and forumla
  63.      * into the new one
  64.      */
  65.     private static void cloneCell( Cell cNew, Cell cOld ){
  66.         cNew.setCellComment( cOld.getCellComment() );
  67.         cNew.setCellStyle( cOld.getCellStyle() );
  68.        
  69.         switch ( cNew.getCellType() ){
  70.             case Cell.CELL_TYPE_BOOLEAN:{
  71.                 cNew.setCellValue( cOld.getBooleanCellValue() );
  72.                 break;
  73.             }
  74.             case Cell.CELL_TYPE_NUMERIC:{
  75.                 cNew.setCellValue( cOld.getNumericCellValue() );
  76.                 break;
  77.             }
  78.             case Cell.CELL_TYPE_STRING:{
  79.                 cNew.setCellValue( cOld.getStringCellValue() );
  80.                 break;
  81.             }
  82.             case Cell.CELL_TYPE_ERROR:{
  83.                 cNew.setCellValue( cOld.getErrorCellValue() );
  84.                 break;
  85.             }
  86.             case Cell.CELL_TYPE_FORMULA:{
  87.                 cNew.setCellFormula( cOld.getCellFormula() );
  88.                 break;
  89.             }
  90.         }
  91.        
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement