0

Java POIを使用してExcelシートから値を読み取っていて、データベースに挿入する必要があります.Excelシートから文字列、数値、および日付の値があり、最初のフィールドは列ヘッダーです。

public class SimpleExcelReadExample {
  static Connection con1 = null;
  static Connection con3 = null;
  static PreparedStatement preparedstatement = null;
  static ResultSet resultset = null;
  int j = 0;

  public static void main(String[] args) {

        String fileName = "D:/Excel/Report.xls";
        Cleartables.table_daily_report();
        Vector dataHolder = read(fileName);
        saveToDatabase(dataHolder);
  }

  public static Vector read(String fileName) {
        Vector cellVectorHolder = new Vector();
        try {
              FileInputStream myInput = new FileInputStream(fileName);
              POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
              HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
              HSSFSheet mySheet = myWorkBook.getSheetAt(0);
              Iterator rowIter = mySheet.rowIterator();
              while (rowIter.hasNext()) {
                    HSSFRow myRow = (HSSFRow) rowIter.next();
                    Iterator cellIter = myRow.cellIterator();
                    Vector cellStoreVector = new Vector();
                    while (cellIter.hasNext()) {
                          HSSFCell myCell = (HSSFCell) cellIter.next();
                          //System.out.println("read method"+myCell);
                          cellStoreVector.addElement(myCell);
                    }
                    cellVectorHolder.addElement(cellStoreVector);
              }
        } catch (Exception e) {
              e.printStackTrace();
        }
        return cellVectorHolder;
  }

  private static void saveToDatabase(Vector dataHolder)
  {

                    for (int i=0;i<dataHolder.size(); i++)
                    {
                       Vector cellStoreVector=(Vector)dataHolder.elementAt(i);
                       for (int j=0; j < cellStoreVector.size();j++)
                    {
                             System.out.println("show.....");
                            HSSFCell myCell = (HSSFCell)cellStoreVector.elementAt(j);

                           Please help me here....How to get the each column values  ?
                    }
4

6 に答える 6

2
try {

        FileInputStream file = new FileInputStream(new File("E://Imp/Details.xlsx"));
        XSSFWorkbook workbook = new XSSFWorkbook(file);
        XSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();
        rowIterator.next();
        while(rowIterator.hasNext())
        {
            Row row = rowIterator.next();
            //For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();

            while(cellIterator.hasNext())
            {
                Cell cell = cellIterator.next();
                //This will change all Cell Types to String
                cell.setCellType(Cell.CELL_TYPE_STRING);
                switch(cell.getCellType()) 
                {
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.println("boolean===>>>"+cell.getBooleanCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_NUMERIC:

                        break;
                    case Cell.CELL_TYPE_STRING:

                       list.add(cell.getStringCellValue());

                                                 break;
                }


            }
            name=row.getCell(0).getStringCellValue();
            empid=row.getCell(1).getStringCellValue();
            add=row.getCell(2).getStringCellValue();
            mobile=row.getCell(3).getStringCellValue();
            System.out.println(name+empid+add+mobile);
            ex.InsertRowInDB(name,empid,add,mobile);
            System.out.println("");


        }
        file.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
    public void InsertRowInDB(String name,String empid,String add,String mobile) throws SQLException{

        Statement stmt=db.con.createStatement();
        PreparedStatement ps=null;
        String sql="Insert into Employee(Name,EmployeeId,Address,ContactInfo) values(?,?,?,?)";
        ps=db.con.prepareStatement(sql);
        ps.setString(1, name);
        ps.setString(2, empid);
        ps.setString(3, add);
        ps.setString(4, mobile);
    ps.executeUpdate();
    System.out.println("Values Inserted Successfully");
    }
}
于 2013-12-10T09:50:10.107 に答える