Advertisement
Raizen

Untitled

May 11th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.20 KB | None | 0 0
  1. //package com.example.tests;
  2. //package br.byiorio.excel;
  3.  
  4.  
  5.      
  6.  
  7. import org.apache.poi.xssf.usermodel.XSSFSheet;
  8. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  9.  
  10.     import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15.  
  16.  
  17.  
  18.     public class DatabaseManager
  19.     {
  20.         public XSSFWorkbook workbook;
  21.         public XSSFSheet sheet;
  22.         public FileInputStream file;
  23.         public  void startExcel(String fileName, String sheetName)
  24.         {  
  25.             try
  26.             {
  27.                 file = new FileInputStream(new File(fileName));
  28.      
  29.                 //Create Workbook instance holding reference to .xlsx file
  30.                 workbook = new XSSFWorkbook(file);
  31.                 sheet = workbook.getSheet(sheetName);
  32.             }
  33.             catch (Exception e)
  34.             {
  35.                 e.printStackTrace();
  36.             }
  37.         }
  38.  
  39.         public void changeTable(String tableName) {
  40.             // TODO Auto-generated method stub
  41.             sheet = workbook.getSheet(tableName);
  42.         }
  43.        
  44.         public String getData(int row, int col)
  45.         {
  46.             return sheet.getRow(row).getCell(col).toString();
  47.            
  48.         }
  49.         public void writeData(String data, int row, int col)
  50.         {
  51.             sheet.getRow(row).getCell(col).setCellValue(data); 
  52.  
  53.  
  54.         }
  55.         public void closeData()
  56.         {
  57.             try {
  58.                 file.close();
  59.             } catch (IOException e) {
  60.                 // TODO Auto-generated catch block
  61.                 e.printStackTrace();
  62.             }
  63.         }
  64.         public void saveData() throws IOException
  65.         {
  66.             FileOutputStream out;
  67.             try {
  68.                 out = new FileOutputStream(new File("C:/EfaFramework/EFAMasterDB.xlsx"));
  69.                 workbook.write(out);
  70.                 out.close();
  71.             } catch (FileNotFoundException e) {
  72.                 // TODO Auto-generated catch block
  73.                 e.printStackTrace();
  74.             }
  75.         }
  76.  
  77.         public String getData(int row, String columnName) {
  78.             // TODO Auto-generated method stub
  79.             int col = getColumnName(columnName);
  80.             return sheet.getRow(row).getCell(col).toString();
  81.         }
  82.         public int getColumnName(String name)
  83.         {
  84.             int col = 0;
  85.             while (getData(0, col) != name)
  86.             {
  87.                 if (getData(0, col).toString() == name)
  88.                     System.out.println("Success!!!");
  89.                 col++;
  90.             }
  91.                
  92.             return col;
  93.         }
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement