Reading Writing Excel Menggunakan Java


 
1. Componen yang dibutuhkan

2. Write File Excel

// Java Program to Illustrate Writing
// Data to Excel File using Apache POI

// Import statements
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

// Main class
public class GFG {

// Main driver method
public static void main(String[] args)
{

// Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();

// Creating a blank Excel sheet
XSSFSheet sheet
= workbook.createSheet("student Details");

// Creating an empty TreeMap of string and Object][]
// type
Map<String, Object[]> data
= new TreeMap<String, Object[]>();

// Writing data to Object[]
// using put() method
data.put("1",
new Object[] { "ID", "NAME", "LASTNAME" });
data.put("2",
new Object[] { 1, "Pankaj", "Kumar" });
data.put("3",
new Object[] { 2, "Prakashni", "Yadav" });
data.put("4", new Object[] { 3, "Ayan", "Mondal" });
data.put("5", new Object[] { 4, "Virat", "kohli" });

// Iterating over data and writing it to sheet
Set<String> keyset = data.keySet();

int rownum = 0;

for (String key : keyset) {

// Creating a new row in the sheet
Row row = sheet.createRow(rownum++);

Object[] objArr = data.get(key);

int cellnum = 0;

for (Object obj : objArr) {

// This line creates a cell in the next
// column of that row
Cell cell = row.createCell(cellnum++);

if (obj instanceof String)
cell.setCellValue((String)obj);

else if (obj instanceof Integer)
cell.setCellValue((Integer)obj);
}
}

// Try block to check for exceptions
try {

// Writing the workbook
FileOutputStream out = new FileOutputStream(
new File("gfgcontribute.xlsx"));
workbook.write(out);

// Closing file output connections
out.close();

// Console message for successful execution of
// program
System.out.println(
"gfgcontribute.xlsx written successfully on disk.");
}

// Catch block to handle exceptions
catch (Exception e) {

// Display exceptions along with line number
// using printStackTrace() method
e.printStackTrace();
}
}
}



3. Read File Excel

// Java Program to Illustrate Reading
// Data to Excel File Using Apache POI

// Import statements
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;

// Main class
public class GFG {

// Main driver method
public static void main(String[] args)
{

// Try block to check for exceptions
try {

// Reading file from local directory
FileInputStream file = new FileInputStream(
new File("gfgcontribute.xlsx"));

// Create Workbook instance holding reference to
// .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);

// Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);

// Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();

// Till there is an element condition holds true
while (rowIterator.hasNext()) {

Row row = rowIterator.next();

// For each row, iterate through all the
// columns
Iterator<Cell> cellIterator
= row.cellIterator();

while (cellIterator.hasNext()) {

Cell cell = cellIterator.next();

// Checking the cell type and format
// accordingly
switch (cell.getCellType()) {

// Case 1
case Cell.CELL_TYPE_NUMERIC:
System.out.print(
cell.getNumericCellValue()
+ "t");
break;

// Case 2
case Cell.CELL_TYPE_STRING:
System.out.print(
cell.getStringCellValue()
+ "t");
break;
}
}

System.out.println("");
}

// Closing file output streams
file.close();
}

// Catch block to handle exceptions
catch (Exception e) {

// Display the exception along with line number
// using printStackTrace() method
e.printStackTrace();
}
}
}


Output :



Solusi Printing Lama & Lambat Pada Printer Epson Lx300 di Windows 7



 Berikut adalah cara mengatasi cetak lama & lambat pada Printer Epson  Lx300 di Windows 7

1. Buka Printers and Devices 


2. Lalu pilih printer yang mau disetting , klik kanan pilih Pirnter Properties


3. lalu pilih tab port


4. Lepaskan centang “enable bidirectional support” pada bagian port koneksi USB yang digunakan, setelah itu klik OK



Catatan :

biasa juga pada tab advanced : pilih Print directly to the printer langsung  cetak ke printer tanpa ditampung ke spool




Read Excel Menggunakan Java - III [ Read Cell ]



Berikut adalah cara membaca cell pada excel menggunakan java

1. Buat File Excel : EmployeeData.xlsx


Cell yang mau dibaca :



2. Buat Source : ReadCellExcel.java

//reading value of a particular cell  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.IOException;  
import org.apache.poi.ss.usermodel.Cell;  
import org.apache.poi.ss.usermodel.*;  
import org.apache.poi.ss.usermodel.Sheet;  
import org.apache.poi.ss.usermodel.Workbook;  
import org.apache.poi.xssf.usermodel.XSSFWorkbook;  

public class ReadCellExample   
{  
public static void main(String[] args)   
{  
ReadCellExample rc=new ReadCellExample();   //object of the class  
//reading the value of 2nd row and 2nd column  
String vOutput=rc.ReadCellData(2, 2);   
System.out.println(vOutput);  
}  
//method defined for reading a cell  
public String ReadCellData(int vRow, int vColumn)  
{  
String value=null;          //variable for storing the cell value  
Workbook wb=null;           //initialize Workbook null  
try  
{  
//reading data from a file in the form of bytes  
FileInputStream fis=new FileInputStream("C:\\demo\\EmployeeData.xlsx");  
//constructs an XSSFWorkbook object, by buffering the whole stream into the memory  
wb=new XSSFWorkbook(fis);  
}  
catch(FileNotFoundException e)  
{  
e.printStackTrace();  
}  
catch(IOException e1)  
{  
e1.printStackTrace();  
}  
Sheet sheet=wb.getSheetAt(0);   //getting the XSSFSheet object at given index  
Row row=sheet.getRow(vRow); //returns the logical row  
Cell cell=row.getCell(vColumn); //getting the cell representing the given column  
value=cell.getStringCellValue();    //getting cell value  
return value;               //returns the cell value  
}  

}  


Hasil :



Read Excel Menggunakan Java - II [ XLSX ]

 

Berikut adalah cara membaca excel menggunakan java

1. Buat file excel : employee.xslx



2. Buat class : XSSFWorkbook class

import java.io.File;  

import java.io.FileInputStream;  

import java.util.Iterator;  

import org.apache.poi.ss.usermodel.Cell;  

import org.apache.poi.ss.usermodel.Row;  

import org.apache.poi.xssf.usermodel.XSSFSheet;  

import org.apache.poi.xssf.usermodel.XSSFWorkbook;  

public class XLSXReaderExample  

{  

public static void main(String[] args)   

{  

try  

{  

File file = new File("C:\\demo\\employee.xlsx");   //creating a new file instance  

FileInputStream fis = new FileInputStream(file);   //obtaining bytes from the file  

//creating Workbook instance that refers to .xlsx file  

XSSFWorkbook wb = new XSSFWorkbook(fis);   

XSSFSheet sheet = wb.getSheetAt(0);     //creating a Sheet object to retrieve object  

Iterator<Row> itr = sheet.iterator();    //iterating over excel file  

while (itr.hasNext())                 

{  

Row row = itr.next();  

Iterator<Cell> cellIterator = row.cellIterator();   //iterating over each column  

while (cellIterator.hasNext())   

{  

Cell cell = cellIterator.next();  

switch (cell.getCellType())               

{  

case Cell.CELL_TYPE_STRING:    //field that represents string cell type  

System.out.print(cell.getStringCellValue() + "\t\t\t");  

break;  

case Cell.CELL_TYPE_NUMERIC:    //field that represents number cell type  

System.out.print(cell.getNumericCellValue() + "\t\t\t");  

break;  

default:  

}  

}  

System.out.println("");  

}  

}  

catch(Exception e)  

{  

e.printStackTrace();  

}  

}  

}  


Hasil :



Read Excel Menggunakan Java - I [ XLS ]

 

Berikut adalah contoh membaca excel dengan menggunakan Java

1. Persiapan Componen

2.  Buat File excel : student.xls 


3.  Buat file readxls.java

import java.io.File;  

import java.io.FileInputStream;  

import java.io.IOException;  

import org.apache.poi.hssf.usermodel.HSSFSheet;  

import org.apache.poi.hssf.usermodel.HSSFWorkbook;  

import org.apache.poi.ss.usermodel.Cell;  

import org.apache.poi.ss.usermodel.FormulaEvaluator;  

import org.apache.poi.ss.usermodel.Row;  

public class ReadExcelFileDemo  

{  

public static void main(String args[]) throws IOException  

{  

//obtaining input bytes from a file  

FileInputStream fis=new FileInputStream(new File("D:\\student.xls"));  

//creating workbook instance that refers to .xls file  

HSSFWorkbook wb=new HSSFWorkbook(fis);   

//creating a Sheet object to retrieve the object  

HSSFSheet sheet=wb.getSheetAt(0);  

//evaluating cell type   

FormulaEvaluator formulaEvaluator=wb.getCreationHelper().createFormulaEvaluator();  

for(Row row: sheet)     //iteration over row using for each loop  

{  

for(Cell cell: row)    //iteration over cell using for each loop  

{  

switch(formulaEvaluator.evaluateInCell(cell).getCellType())  

{  

case Cell.CELL_TYPE_NUMERIC:   //field that represents numeric cell type  

//getting the value of the cell as a number  

System.out.print(cell.getNumericCellValue()+ "\t\t");   

break;  

case Cell.CELL_TYPE_STRING:    //field that represents string cell type  

//getting the value of the cell as a string  

System.out.print(cell.getStringCellValue()+ "\t\t");  

break;  

}  

}  

System.out.println();  

}  

}  

}  

Hasil :


Membuat Form Input Dengan Oracle Form Builder - II [ Membuat Form ]

 

1. Membuat Form Kategori

1.1. buka ORACLE Form Builder 6i --> klik Form Builder, jika muncul kotak dialog seperti dibawah ini, klik Cancel


1.2. Kemudian klik menu File > klik Connect, lalu klik kanan pada Data Blocks > klik Data Block Wizard 


1.3.  Klik Next


1.4.  Pilih Table or View > klik Next > klik Browse pada bagian Table or View. Kemudian akan muncul kotak dialog seperti dibawah ini, isikan Username : Supermarket,  Password : Manager & Database : produksi


1.5. Kemudian pada kotak dialog Connect, pilih Kategori, lalu klik OK


1.6. Setelah itu,  klik Button  --> klik Next -- > Finish 
1.6. Kemudian akan muncul kotak dialog Layout Wizard --> klik Next --> Next , kemudian klik Button  --> klik Next --> klik Next > pilih Form --> Next > Klik Next --> Finish. Setelah itu akan muncul seperti gambar dibawah ini :


1.7. Setelah itu rapikan menjadi seperti gambar dibawah 


1.8. Kemudian jalan kan


1.9. Untuk mengatur Tanggal dan User muncul secara otomatis maka lakukan tahapan : 

1.9.1. Pengaturan Tanggal
  • Pada Object Navigator, klik kanan pada Triggers yang terletak di bawah FRM_KATEGORI --> klik PL/SQL Editor --> Pilih PRE-INSERT --> klik OK 
  • Lalu akan muncul kotak dialog PL/SQL Editor, kemudian masukan script seperti dibawah ini

Compile > klik Close 
  • Kembali ke Layout Editor FRM_KATEGORI, lalu  klik kanan pada Tanggal Entry

 kemudian klik Property Palette atau bisa juga dengan cara double klik gambar di bawah ini 

  • Pada bagian Data, Data Type, Initial Value dan Format Mask di ubah seperti gambar di bawah ini lalu Close Property Pallete

1.9.2 Pengaturan User

1.9.2.1. Klik kanan pada User Entry   lalu klik Property Palette atau bisa juga dengan cara double klik 

1.9.2.2. Maka akan muncul Property Palette > pada bagian Data ubah berikut ini 

  • Initial Value menjadi yang anda inginkan misalkan Agus 
  • Enable = No 

 1.9.2.3. Kemudian tutup atau Close Property Palette

1.10. Jalankan form dengan mengklik icon  Maka hasil Form yang sudah di atur Tanggal Entry

dan User Entry agar muncul secara otomatis :



1.11. Setelah itu, isi data Form Kategori dan klik Icon
  untuk keluar dari form



2. Membuat Form Produk

2.1. klik menu File --> New Form 

2.3. Klik kanan pada bagian Data Blocks --> Data Block Wizard 

2.4. Maka akan muncul kotak dialog Data Block Wizard , kemudian kilk Next 

2.5. Pilih Table or View --> Next > Browse --> Pilih PRODUK > OK 

2.6. Klik Button --> klik Next --> Finish 

2.7. Kemudian akan muncul kotak dialog Layout Wizard --> klik Next --> Next 

2.8. Klik Button --> klik Next --> klik Next --> pilih Form --> Next --> Klik Next --> Finish.


2.9. Rapikan seperti gambar dibawah :



2.10. Jalankan form dengan mengklik icon Isikan data pada Form Produk > klik Icon  untuk keluar dari form


Membuat Form Input Dengan Oracle Form Builder - I

 

Untuk membuat input form pada Oracle Form Builder diperlukan database pada oracle dan table, maka diperlukan :

  • Database Oracle 
  • Oracle Form Builder

Kemudian buat User : Supermarket dengan Password : Manager lalu buat tabel Kategori dan Produk

1. Membuat Table 

 Kategori  & Product

Struktur table :

Isi table :




2. Membuat TNSName untuk bisa koneksi client

2.1. Buka TNSNAMES, klik Explorer --> klik Orant pada Drive (c) -->  Buka NET 80 --> Admin --> kemudian buka dengan Notepad 

2.2. Copy script dibawah ini dan paste ke bagian paling akhir TNSNAMES 


2.3. Kemudian ubahlah berikut ini : 
  • Example1.world ubah sesuai dengan keinginan, misal Ciska 
  • Pada bagian Host ubah menjadi 127.0.0.1 
  • Pada bagian SID ubah menjadi xe 
Untuk lebih jelas dapat di lihat gambar di bawah ini :