Javatpoint Logo

Reading excel file and inserting data into database

By: mahboo*** On: Sat Jun 18 15:09:57 IST 2016     Question Reputation0 Answer Reputation0 Quiz Belt Series Points0  0Blank User
Sir I have to write a java program which reads the data from the excel sheet and insert the data into database .How we map the excel sheet coloumn into database table coloumn .Please help meUp0Down

 
Sir I have to map excel row with database table row?How we mapp it is not described in this soltion.Please describe how to map the row of excel row with database table row?Image Created0Down

By: [email protected] On: Mon Jun 20 09:49:38 IST 2016 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
public class Main {

@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
//
// An excel file name. You can create a file name with a full
// path information.
//
String fileName = "D:/workspace/ReadExlsx/xlsx/SampleData.xls";


// Create an ArrayList to store the data read from excel sheet.
//
List sheetData = new ArrayList();

FileInputStream fis = null;
try {
fis = new FileInputStream(fileName);

//
// Create an excel workbook from the file system.
//
HSSFWorkbook workbook = new HSSFWorkbook(fis);
//
// Get the first sheet on the workbook.
//
HSSFSheet sheet = workbook.getSheetAt(0);

//
// When we have a sheet object in hand we can iterator on
// each sheet's rows and on each row's cells. We store the
// data read on an ArrayList so that we can printed the
// content of the excel to the console.
//
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();

List data = new ArrayList();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
data.add(cell);
}
sheetData.add(data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
}

showExelData(sheetData);
}

private static void showExelData(List sheetData) {
//
// Iterates the data and print it out to the console.
//
for (int i = 0; i < sheetData.size(); i++) {
List list = (List) sheetData.get(i);
//for (int j = 0; j < list.size(); j++) {
HSSFCell employeeid = (HSSFCell) list.get(0);
HSSFCell department = (HSSFCell) list.get(3);
HSSFCell date = (HSSFCell) list.get(5);
System.out.print(employeeid.getRichStringCellValue().getString()+" , ");
System.out.print(department.getRichStringCellValue().getString()+" , ");
System.out.print(date.getRichStringCellValue().getString());
//if (j < list.size() - 1) {
// System.out.print(", ");
//}
// }
System.out.println("");
}
}
}
Image Created0Down

By: [email protected] On: Tue Jun 21 06:40:46 IST 2016 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No
 
Image Created0Down

By: [email protected] On: Thu Apr 13 15:50:13 IST 2017 Question Reputation0 Answer Reputation0 Belt Series Points0 0User Image
Are You Satisfied :0Yes0No