Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package com.example.tests;
- //package br.byiorio.excel;
- import org.apache.poi.xssf.usermodel.XSSFSheet;
- import org.apache.poi.xssf.usermodel.XSSFWorkbook;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class DatabaseManager
- {
- public XSSFWorkbook workbook;
- public XSSFSheet sheet;
- public FileInputStream file;
- public void startExcel(String fileName, String sheetName)
- {
- try
- {
- file = new FileInputStream(new File(fileName));
- //Create Workbook instance holding reference to .xlsx file
- workbook = new XSSFWorkbook(file);
- sheet = workbook.getSheet(sheetName);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- public void changeTable(String tableName) {
- // TODO Auto-generated method stub
- sheet = workbook.getSheet(tableName);
- }
- public String getData(int row, int col)
- {
- return sheet.getRow(row).getCell(col).toString();
- }
- public void writeData(String data, int row, int col)
- {
- sheet.getRow(row).getCell(col).setCellValue(data);
- }
- public void closeData()
- {
- try {
- file.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void saveData() throws IOException
- {
- FileOutputStream out;
- try {
- out = new FileOutputStream(new File("C:/EfaFramework/EFAMasterDB.xlsx"));
- workbook.write(out);
- out.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public String getData(int row, String columnName) {
- // TODO Auto-generated method stub
- int col = getColumnName(columnName);
- return sheet.getRow(row).getCell(col).toString();
- }
- public int getColumnName(String name)
- {
- int col = 0;
- while (getData(0, col) != name)
- {
- if (getData(0, col).toString() == name)
- System.out.println("Success!!!");
- col++;
- }
- return col;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement