Advertisement
SeleniumETrainR

excel reader class

Dec 21st, 2012
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.19 KB | None | 0 0
  1.  
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.Calendar;
  7.  
  8. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  9. import org.apache.poi.hssf.usermodel.HSSFDateUtil;
  10. import org.apache.poi.hssf.util.HSSFColor;
  11. import org.apache.poi.ss.usermodel.Cell;
  12. import org.apache.poi.ss.usermodel.CellStyle;
  13. import org.apache.poi.ss.usermodel.IndexedColors;
  14. import org.apache.poi.xssf.usermodel.XSSFCell;
  15. import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  16. import org.apache.poi.xssf.usermodel.XSSFCreationHelper;
  17. import org.apache.poi.xssf.usermodel.XSSFFont;
  18. import org.apache.poi.xssf.usermodel.XSSFHyperlink;
  19. import org.apache.poi.xssf.usermodel.XSSFRow;
  20. import org.apache.poi.xssf.usermodel.XSSFSheet;
  21. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  22.  
  23.  
  24. public class Xls_Reader {
  25.      
  26.     public  String path;
  27.     public  FileInputStream fis = null;
  28.     public  FileOutputStream fileOut =null;
  29.     private XSSFWorkbook workbook = null;
  30.     private XSSFSheet sheet = null;
  31.     private XSSFRow row   =null;
  32.     private XSSFCell cell = null;
  33.    
  34.     public Xls_Reader(String path) {
  35.        
  36.         this.path=path;
  37.         try {
  38.             fis = new FileInputStream(path);
  39.             workbook = new XSSFWorkbook(fis);
  40.             sheet = workbook.getSheetAt(0);
  41.             fis.close();
  42.         } catch (Exception e) {
  43.             // TODO Auto-generated catch block
  44.             e.printStackTrace();
  45.         }
  46.        
  47.     }
  48.     // returns the row count in a sheet
  49.     public int getRowCount(String sheetName){
  50.         int index = workbook.getSheetIndex(sheetName);
  51.         if(index==-1)
  52.             return 0;
  53.         else{
  54.         sheet = workbook.getSheetAt(index);
  55.         int number=sheet.getLastRowNum()+1;
  56.         return number;
  57.         }
  58.        
  59.     }
  60.    
  61.     // returns the data from a cell
  62.     public String getCellData(String sheetName,String colName,int rowNum){
  63.         try{
  64.             if(rowNum <=0)
  65.                 return "";
  66.        
  67.         int index = workbook.getSheetIndex(sheetName);
  68.         int col_Num=-1;
  69.         if(index==-1)
  70.             return "";
  71.        
  72.         sheet = workbook.getSheetAt(index);
  73.         row=sheet.getRow(0);
  74.         for(int i=0;i<row.getLastCellNum();i++){
  75.             //System.out.println(row.getCell(i).getStringCellValue().trim());
  76.             if(row.getCell(i).getStringCellValue().trim().equals(colName.trim()))
  77.                 col_Num=i;
  78.         }
  79.         if(col_Num==-1)
  80.             return "";
  81.        
  82.         sheet = workbook.getSheetAt(index);
  83.         row = sheet.getRow(rowNum-1);
  84.         if(row==null)
  85.             return "";
  86.         cell = row.getCell(col_Num);
  87.        
  88.         if(cell==null)
  89.             return "";
  90.         //System.out.println(cell.getCellType());
  91.         if(cell.getCellType()==Cell.CELL_TYPE_STRING)
  92.               return cell.getStringCellValue();
  93.         else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC || cell.getCellType()==Cell.CELL_TYPE_FORMULA ){
  94.              
  95.               String cellText  = String.valueOf(cell.getNumericCellValue());
  96.               if (HSSFDateUtil.isCellDateFormatted(cell)) {
  97.                    // format in form of M/D/YY
  98.                   double d = cell.getNumericCellValue();
  99.  
  100.                   Calendar cal =Calendar.getInstance();
  101.                   cal.setTime(HSSFDateUtil.getJavaDate(d));
  102.                     cellText =
  103.                      (String.valueOf(cal.get(Calendar.YEAR))).substring(2);
  104.                    cellText = cal.get(Calendar.DAY_OF_MONTH) + "/" +
  105.                               cal.get(Calendar.MONTH)+1 + "/" +
  106.                               cellText;
  107.                    
  108.                    //System.out.println(cellText);
  109.  
  110.                  }
  111.  
  112.              
  113.              
  114.               return cellText;
  115.           }else if(cell.getCellType()==Cell.CELL_TYPE_BLANK)
  116.               return "";
  117.           else
  118.               return String.valueOf(cell.getBooleanCellValue());
  119.        
  120.         }
  121.         catch(Exception e){
  122.            
  123.             e.printStackTrace();
  124.             return "row "+rowNum+" or column "+colName +" does not exist in xls";
  125.         }
  126.     }
  127.    
  128.     // returns the data from a cell
  129.     public String getCellData(String sheetName,int colNum,int rowNum){
  130.         try{
  131.             if(rowNum <=0)
  132.                 return "";
  133.        
  134.         int index = workbook.getSheetIndex(sheetName);
  135.  
  136.         if(index==-1)
  137.             return "";
  138.        
  139.    
  140.         sheet = workbook.getSheetAt(index);
  141.         row = sheet.getRow(rowNum-1);
  142.         if(row==null)
  143.             return "";
  144.         cell = row.getCell(colNum);
  145.         if(cell==null)
  146.             return "";
  147.        
  148.       if(cell.getCellType()==Cell.CELL_TYPE_STRING)
  149.           return cell.getStringCellValue();
  150.       else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC || cell.getCellType()==Cell.CELL_TYPE_FORMULA ){
  151.          
  152.           String cellText  = String.valueOf(cell.getNumericCellValue());
  153.           if (HSSFDateUtil.isCellDateFormatted(cell)) {
  154.                // format in form of M/D/YY
  155.               double d = cell.getNumericCellValue();
  156.  
  157.               Calendar cal =Calendar.getInstance();
  158.               cal.setTime(HSSFDateUtil.getJavaDate(d));
  159.                 cellText =
  160.                  (String.valueOf(cal.get(Calendar.YEAR))).substring(2);
  161.                cellText = cal.get(Calendar.MONTH)+1 + "/" +
  162.                           cal.get(Calendar.DAY_OF_MONTH) + "/" +
  163.                           cellText;
  164.                
  165.               // System.out.println(cellText);
  166.  
  167.              }
  168.  
  169.          
  170.          
  171.           return cellText;
  172.       }else if(cell.getCellType()==Cell.CELL_TYPE_BLANK)
  173.           return "";
  174.       else
  175.           return String.valueOf(cell.getBooleanCellValue());
  176.         }
  177.         catch(Exception e){
  178.            
  179.             e.printStackTrace();
  180.             return "row "+rowNum+" or column "+colNum +" does not exist  in xls";
  181.         }
  182.     }
  183.    
  184.     // returns true if data is set successfully else false
  185.     public boolean setCellData(String sheetName,String colName,int rowNum, String data){
  186.         try{
  187.         fis = new FileInputStream(path);
  188.         workbook = new XSSFWorkbook(fis);
  189.  
  190.         if(rowNum<=0)
  191.             return false;
  192.        
  193.         int index = workbook.getSheetIndex(sheetName);
  194.         int colNum=-1;
  195.         if(index==-1)
  196.             return false;
  197.        
  198.        
  199.         sheet = workbook.getSheetAt(index);
  200.        
  201.  
  202.         row=sheet.getRow(0);
  203.         for(int i=0;i<row.getLastCellNum();i++){
  204.             //System.out.println(row.getCell(i).getStringCellValue().trim());
  205.             if(row.getCell(i).getStringCellValue().trim().equals(colName))
  206.                 colNum=i;
  207.         }
  208.         if(colNum==-1)
  209.             return false;
  210.  
  211.         sheet.autoSizeColumn(colNum);
  212.         row = sheet.getRow(rowNum-1);
  213.         if (row == null)
  214.             row = sheet.createRow(rowNum-1);
  215.        
  216.         cell = row.getCell(colNum);
  217.         if (cell == null)
  218.             cell = row.createCell(colNum);
  219.  
  220.         // cell style
  221.         //CellStyle cs = workbook.createCellStyle();
  222.         //cs.setWrapText(true);
  223.         //cell.setCellStyle(cs);
  224.         cell.setCellValue(data);
  225.  
  226.         fileOut = new FileOutputStream(path);
  227.  
  228.         workbook.write(fileOut);
  229.  
  230.         fileOut.close();   
  231.  
  232.         }
  233.         catch(Exception e){
  234.             e.printStackTrace();
  235.             return false;
  236.         }
  237.         return true;
  238.     }
  239.    
  240.    
  241.     // returns true if data is set successfully else false
  242.     public boolean setCellData(String sheetName,String colName,int rowNum, String data,String url){
  243.         //System.out.println("setCellData setCellData******************");
  244.         try{
  245.         fis = new FileInputStream(path);
  246.         workbook = new XSSFWorkbook(fis);
  247.  
  248.         if(rowNum<=0)
  249.             return false;
  250.        
  251.         int index = workbook.getSheetIndex(sheetName);
  252.         int colNum=-1;
  253.         if(index==-1)
  254.             return false;
  255.        
  256.        
  257.         sheet = workbook.getSheetAt(index);
  258.         //System.out.println("A");
  259.         row=sheet.getRow(0);
  260.         for(int i=0;i<row.getLastCellNum();i++){
  261.             //System.out.println(row.getCell(i).getStringCellValue().trim());
  262.             if(row.getCell(i).getStringCellValue().trim().equalsIgnoreCase(colName))
  263.                 colNum=i;
  264.         }
  265.        
  266.         if(colNum==-1)
  267.             return false;
  268.         sheet.autoSizeColumn(colNum); //ashish
  269.         row = sheet.getRow(rowNum-1);
  270.         if (row == null)
  271.             row = sheet.createRow(rowNum-1);
  272.        
  273.         cell = row.getCell(colNum);
  274.         if (cell == null)
  275.             cell = row.createCell(colNum);
  276.            
  277.         cell.setCellValue(data);
  278.         XSSFCreationHelper createHelper = workbook.getCreationHelper();
  279.  
  280.         //cell style for hyperlinks
  281.         //by default hypelrinks are blue and underlined
  282.         CellStyle hlink_style = workbook.createCellStyle();
  283.         XSSFFont hlink_font = workbook.createFont();
  284.         hlink_font.setUnderline(XSSFFont.U_SINGLE);
  285.         hlink_font.setColor(IndexedColors.BLUE.getIndex());
  286.         hlink_style.setFont(hlink_font);
  287.         //hlink_style.setWrapText(true);
  288.  
  289.         XSSFHyperlink link = createHelper.createHyperlink(XSSFHyperlink.LINK_FILE);
  290.         link.setAddress(url);
  291.         cell.setHyperlink(link);
  292.         cell.setCellStyle(hlink_style);
  293.          
  294.         fileOut = new FileOutputStream(path);
  295.         workbook.write(fileOut);
  296.  
  297.         fileOut.close();   
  298.  
  299.         }
  300.         catch(Exception e){
  301.             e.printStackTrace();
  302.             return false;
  303.         }
  304.         return true;
  305.     }
  306.    
  307.    
  308.    
  309.     // returns true if sheet is created successfully else false
  310.     public boolean addSheet(String  sheetname){    
  311.        
  312.         FileOutputStream fileOut;
  313.         try {
  314.              workbook.createSheet(sheetname);  
  315.              fileOut = new FileOutputStream(path);
  316.              workbook.write(fileOut);
  317.              fileOut.close();          
  318.         } catch (Exception e) {        
  319.             e.printStackTrace();
  320.             return false;
  321.         }
  322.         return true;
  323.     }
  324.    
  325.     // returns true if sheet is removed successfully else false if sheet does not exist
  326.     public boolean removeSheet(String sheetName){      
  327.         int index = workbook.getSheetIndex(sheetName);
  328.         if(index==-1)
  329.             return false;
  330.        
  331.         FileOutputStream fileOut;
  332.         try {
  333.             workbook.removeSheetAt(index);
  334.             fileOut = new FileOutputStream(path);
  335.             workbook.write(fileOut);
  336.             fileOut.close();           
  337.         } catch (Exception e) {        
  338.             e.printStackTrace();
  339.             return false;
  340.         }
  341.         return true;
  342.     }
  343.     // returns true if column is created successfully
  344.     public boolean addColumn(String sheetName,String colName){
  345.         //System.out.println("**************addColumn*********************");
  346.        
  347.         try{               
  348.             fis = new FileInputStream(path);
  349.             workbook = new XSSFWorkbook(fis);
  350.             int index = workbook.getSheetIndex(sheetName);
  351.             if(index==-1)
  352.                 return false;
  353.            
  354.         XSSFCellStyle style = workbook.createCellStyle();
  355.         style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
  356.         style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  357.        
  358.         sheet=workbook.getSheetAt(index);
  359.        
  360.         row = sheet.getRow(0);
  361.         if (row == null)
  362.             row = sheet.createRow(0);
  363.        
  364.         //cell = row.getCell();
  365.         //if (cell == null)
  366.         //System.out.println(row.getLastCellNum());
  367.         if(row.getLastCellNum() == -1)
  368.             cell = row.createCell(0);
  369.         else
  370.             cell = row.createCell(row.getLastCellNum());
  371.            
  372.             cell.setCellValue(colName);
  373.             cell.setCellStyle(style);
  374.            
  375.             fileOut = new FileOutputStream(path);
  376.             workbook.write(fileOut);
  377.             fileOut.close();           
  378.  
  379.         }catch(Exception e){
  380.             e.printStackTrace();
  381.             return false;
  382.         }
  383.        
  384.         return true;
  385.        
  386.        
  387.     }
  388.     // removes a column and all the contents
  389.     public boolean removeColumn(String sheetName, int colNum) {
  390.         try{
  391.         if(!isSheetExist(sheetName))
  392.             return false;
  393.         fis = new FileInputStream(path);
  394.         workbook = new XSSFWorkbook(fis);
  395.         sheet=workbook.getSheet(sheetName);
  396.         XSSFCellStyle style = workbook.createCellStyle();
  397.         style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
  398.         XSSFCreationHelper createHelper = workbook.getCreationHelper();
  399.         style.setFillPattern(HSSFCellStyle.NO_FILL);
  400.        
  401.        
  402.    
  403.         for(int i =0;i<getRowCount(sheetName);i++){
  404.             row=sheet.getRow(i);   
  405.             if(row!=null){
  406.                 cell=row.getCell(colNum);
  407.                 if(cell!=null){
  408.                     cell.setCellStyle(style);
  409.                     row.removeCell(cell);
  410.                 }
  411.             }
  412.         }
  413.         fileOut = new FileOutputStream(path);
  414.         workbook.write(fileOut);
  415.         fileOut.close();
  416.         }
  417.         catch(Exception e){
  418.             e.printStackTrace();
  419.             return false;
  420.         }
  421.         return true;
  422.        
  423.     }
  424.   // find whether sheets exists
  425.     public boolean isSheetExist(String sheetName){
  426.         int index = workbook.getSheetIndex(sheetName);
  427.         if(index==-1){
  428.             index=workbook.getSheetIndex(sheetName.toUpperCase());
  429.                 if(index==-1)
  430.                     return false;
  431.                 else
  432.                     return true;
  433.         }
  434.         else
  435.             return true;
  436.     }
  437.    
  438.     // returns number of columns in a sheet
  439.     public int getColumnCount(String sheetName){
  440.         // check if sheet exists
  441.         if(!isSheetExist(sheetName))
  442.          return -1;
  443.        
  444.         sheet = workbook.getSheet(sheetName);
  445.         row = sheet.getRow(0);
  446.        
  447.         if(row==null)
  448.             return -1;
  449.        
  450.         return row.getLastCellNum();
  451.        
  452.        
  453.        
  454.     }
  455.     //String sheetName, String testCaseName,String keyword ,String URL,String message
  456.     public boolean addHyperLink(String sheetName,String screenShotColName,String testCaseName,int index,String url,String message){
  457.         //System.out.println("ADDING addHyperLink******************");
  458.        
  459.         url=url.replace('\\', '/');
  460.         if(!isSheetExist(sheetName))
  461.              return false;
  462.        
  463.         sheet = workbook.getSheet(sheetName);
  464.        
  465.         for(int i=2;i<=getRowCount(sheetName);i++){
  466.             if(getCellData(sheetName, 0, i).equalsIgnoreCase(testCaseName)){
  467.                 //System.out.println("**caught "+(i+index));
  468.                 setCellData(sheetName, screenShotColName, i+index, message,url);
  469.                 break;
  470.             }
  471.         }
  472.  
  473.  
  474.         return true;
  475.     }
  476.     public int getCellRowNum(String sheetName,String colName,String cellValue){
  477.        
  478.         for(int i=2;i<=getRowCount(sheetName);i++){
  479.             if(getCellData(sheetName,colName , i).equalsIgnoreCase(cellValue)){
  480.                 return i;
  481.             }
  482.         }
  483.         return -1;
  484.        
  485.     }
  486.        
  487.     // to run this on stand alone
  488.     public static void main(String arg[]) throws IOException{
  489.        
  490.         //System.out.println(filename);
  491.         Xls_Reader datatable = null;
  492.        
  493.  
  494.              datatable = new Xls_Reader("H:\\Student_Selenium_Workspaces\\Framework_Weekend\\src\\Framework_XL_Files\\Controller.xlsx");
  495.                 for(int col=0 ;col< datatable.getColumnCount("TC5"); col++){
  496.                     System.out.println(datatable.getCellData("TC5", col, 1));
  497.                 }
  498.     }
  499.    
  500.    
  501. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement