Using Java we can read the data from Excel sheets by creating type1 driver , but here we are taking help of JExcelapi we can able to read data directly from the Excel sheets.
Steps to be follow:
Step 1:
download JExcel api from here
Step2:
after downloading extract the zip file.
make jxl.jar available to your class path.
Step 3:
create a Excel sheet with some data
Step 4:
in this step we are reading data from the Excel sheet.
use the below java code:
Steps to be follow:
Step 1:
download JExcel api from here
Step2:
after downloading extract the zip file.
make jxl.jar available to your class path.
Step 3:
create a Excel sheet with some data
Step 4:
in this step we are reading data from the Excel sheet.
use the below java code:
package com.vaani.excel.jexcel;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import jxl.*;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.Number;
public class ReadWriteInExcel {
public static void readExcelFile() throws IOException, BiffException {
Workbook workbook = Workbook.getWorkbook(
new File("---/././file.xls"));
// my excel sheet name is contacts, iam given the complete path.// iam reading data from the sheet1
Sheet sheet = workbook.getSheet(0);
// here iam reading the data of 1st column data up three cells
Cell a1 = sheet.getCell(0,0);
Cell b2 = sheet.getCell(0,1);
Cell c2 = sheet.getCell(0,2);
//getting the data from cells
String stringa1 = a1.getContents();
String stringb2 = b2.getContents();
String stringc2 = c2.getContents();
//printing the data
System.out.println("a1–>"+stringa1);
System.out.println("b2–>"+stringb2);
System.out.println("c3–>"+stringc2);
}
public static void writeInExcelFile(String args[]) throws IOException, WriteException
{
// STEP 1:
// the first step is to create a writable workbook using the factory method on the Workbook class. WritableWorkbook workbook = Workbook.createWorkbook(
new File("/some file.xls"));
//test.xls is my work book
// STEP 2:
//// sheet name WritableSheet sheet = workbook.createSheet("First Sheet",0);
//STEP 3:
//adding(inserting) name to the sheet at location (0,2)
Label label = new Label(0,2,"A label record");
sheet.addCell(label);
//adding number to the sheet at location (1,2)
Number number = new Number(1,2,3.1459);
sheet.addCell(number);
//Step 4:
//close the all opened connections
workbook.write();
workbook.close();
}
}
No comments:
Post a Comment