3 Replies Latest reply: Oct 2, 2008 5:44 AM by masijade RSS

    How to store the data read from excel into Ms access database

    807589
      Hi,
      I wrote the code to read the data from excel and given below.Now its reading all the values and printing it in Java Console. Cold you explain me where to store the data read from excel and how can i connect it to database?If possible,Please give me a sample code how to store the values read from excel to Ms.access database.


      public class ReadExcelTest
      {
           private String inputFile;
                public void setInputFile(String inputFile)
                {
                     this.inputFile = inputFile;
                }
                public void read() throws IOException, WriteException {
                     File inputWorkbook = new File(inputFile);
                     Workbook w;
                     try {
                               w = Workbook.getWorkbook(inputWorkbook);
                               Sheet sheet = w.getSheet(0);// Get the first sheet
                               // Loop over first 10 column and lines
                               for (int j = 0; j < sheet.getColumns(); j++)
                               {
                                    for (int i = 0; i < sheet.getRows(); i++)
                                    {
                                         Cell cell = sheet.getCell(j, i);
                                         CellType type = cell.getType();
                                         
                                         if (cell.getContents().isEmpty())
                                         {
                                              System.out.println("This column is empty ");
                                         }
                                         if (cell.getType() == CellType.LABEL)
                                         {
                                              System.out.println( "The contents of the excel are "+ cell.getContents());
                                         }
                                         if (cell.getType() == CellType.NUMBER)
                                         {
                                              System.out.println("I got a number "+ cell.getContents());
                                         }
                                         
                                    }

                               }

                          } catch (BiffException e) {
                               e.printStackTrace();
                          }
                     }

           public static void main(String[] args) throws WriteException, IOException {
                          ReadExcel test = new ReadExcel();
                          test.setInputFile("C:/xxx.xls");
                          test.read();
                     }
                }


      Thanks in advance