Here I have used JXL API to read Excel file. It can be downloaded from JXL API download
Source Code
Source Code
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcel
{
private String inputFile;
public void setInputFile(String inputFile)
{
this.inputFile = inputFile;
}
public void read() throws IOException
{
File inputWorkbook = new File(inputFile);
Workbook w;
try
{
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
for (int j = 0; j < sheet.getRows(); j++)
{
for (int i = 0; i < sheet.getColumns(); i++)
{
Cell cell = sheet.getCell(i, j);
System.out.print(cell.getContents()+"\t|\t");
// here you can use sql Queries/other code to do insertion of what ever fields you want
}
System.out.println();
}
} catch (BiffException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException
{
ReadExcel test = new ReadExcel();
test.setInputFile("c:\\namelist.xls");
test.read();
}
}
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcel
{
private String inputFile;
public void setInputFile(String inputFile)
{
this.inputFile = inputFile;
}
public void read() throws IOException
{
File inputWorkbook = new File(inputFile);
Workbook w;
try
{
w = Workbook.getWorkbook(inputWorkbook);
// Get the first sheet
Sheet sheet = w.getSheet(0);
for (int j = 0; j < sheet.getRows(); j++)
{
for (int i = 0; i < sheet.getColumns(); i++)
{
Cell cell = sheet.getCell(i, j);
System.out.print(cell.getContents()+"\t|\t");
// here you can use sql Queries/other code to do insertion of what ever fields you want
}
System.out.println();
}
} catch (BiffException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException
{
ReadExcel test = new ReadExcel();
test.setInputFile("c:\\namelist.xls");
test.read();
}
}
No comments:
Post a Comment