Frage

I'm creating a web app which requires uploading of excel files from the user and the data from that file will get stored in the database. I'm using postgre sql and spring, poi api. I have a program which extracts data from xls and xlsx and displays it on the myeclipse console but what should i do to store that data to database.

public class ReadExcelData {

    public static void main(String args[])
    {
        ReadExcelData read = new ReadExcelData();
        try 
        {
            System.out.println("**********Reading XLS File**************");
            //reading xls file
            read.readXlsFile();

            System.out.println("");

            System.out.println("**********Reading XLSX File**************");
            //Reading xlsx file
            read.readXlsxFile();
        }
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }

    /**
     * This method will read the Excel file with .xls format (before MS Excel 2003 Ver.)
     * @throws IOException
     */
    private void readXlsFile() throws IOException
    {
        InputStream file = new FileInputStream("\\manage_user_info.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(file);

        //Getting sheet number 1
        HSSFSheet sheet = workbook.getSheetAt(0);

        HSSFRow row; 
        HSSFCell cell;

        //Creating the Row Iterator Object
        Iterator<?> rows = sheet.rowIterator();

        //Iterating over the Rows of a Sheet
        while (rows.hasNext())
        {
            row=(HSSFRow) rows.next();
            Iterator<?> cells = row.cellIterator();

            //Iterating over the Columns of a Row
            while (cells.hasNext())
            {
                cell=(HSSFCell) cells.next();

                //For string type data
                if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
                {
                    System.out.print(cell.getStringCellValue()+" ");
                }
                //for Numeric data
                else if(cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC)
                {
                    System.out.print(cell.getNumericCellValue()+" ");
                }
            }

        }

    }

    /**
     * This method will read the Excel file with .xlsx format
     * @throws IOException
     */
    private void readXlsxFile() throws IOException
    {
        InputStream xlsxFile = new FileInputStream("C:\\Users\\Ashwani\\Desktop\\manage_user_info.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(xlsxFile);

        //Getting sheet number 1
        XSSFSheet sheet = workbook.getSheetAt(0);

        XSSFRow row;
        XSSFCell cell;

        //Creating the Row Iterator Object
        Iterator rows = sheet.rowIterator();

        //Iterating over the Rows of a Sheet
        while (rows.hasNext())
        {
            row=( XSSFRow) rows.next();
            Iterator cells = row.cellIterator();
            //Iterating over the Columns of a Row
            while (cells.hasNext())
            {
                cell=(XSSFCell) cells.next();

                if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING)
                {
                    System.out.print("--string-- ");
                }
                else if(cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC)
                {
                    System.out.print("--number--");
                }
            }System.out.println();
        }
    }

}
War es hilfreich?

Lösung

This is a very general question so it's difficult to give a specific answer, you might want to consider the following as guidance though:

  1. Design a domain object/model to hold whatever data you are reading from the xls file. Your read method should return a list of this object.
  2. Create a Spring manager class to act as your service layer, this might have a method called save which takes a list of objects from step one.
  3. Create a data access object which handles the mapping of objects passed through from step two. You might consider using an ORM tool (Hibernate for example) for this step.

I hope this points you in the right direction.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top