Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WebElement searchbox = driver.findElement(By.name("j_username"));
- WebElement searchbox2 = driver.findElement(By.name("j_password"));
- try {
- FileInputStream file = new FileInputStream(new File("C:\paulo.xls"));
- HSSFWorkbook workbook = new HSSFWorkbook(file);
- HSSFSheet sheet = workbook.getSheetAt(0);
- for (int i=1; i <= sheet.getLastRowNum(); i++){
- String j_username = sheet.getRow(i).getCell(0).getStringCellValue();
- String j_password = sheet.getRow(i).getCell(0).getStringCellValue();
- searchbox.sendKeys(j_username);
- searchbox2.sendKeys(j_password);
- searchbox.submit();
- driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
- }
- workbook.close();
- file.close();
- } catch (FileNotFoundException fnfe) {
- fnfe.printStackTrace();
- } catch (IOException ioe) {
- ioe.printStackTrace();
- Cell cell = sheet.getRow(i).getCell(0);
- cell.setCellType ( Cell.CELL_TYPE_STRING );
- String j_username = cell.getStringCellValue();
- DataFormatter formatter = new DataFormatter(); //creating formatter using the default locale
- Cell cell = sheet.getRow(i).getCell(0);
- String j_username = formatter.formatCellValue(cell); //Returns the formatted value of a cell as a String regardless of the cell type.
- // Create a formatter, do this once
- DataFormatter formatter = new DataFormatter(Locale.US);
- .....
- for (int i=1; i <= sheet.getLastRowNum(); i++) {
- Row r = sheet.getRow(i);
- if (r == null) {
- // empty row, skip
- } else {
- String j_username = formatter.formatCellValue(row.getCell(0));
- String j_password = formatter.formatCellValue(row.getCell(1));
- // Use these
- }
- }
- public static void main(String[] args) throws Exception {
- try {
- Class forName = Class.forName("com.mysql.jdbc.Driver");
- Connection con = null;
- con = DriverManager.getConnection("jdbc:mysql://localhost/tables", "root", "root");
- con.setAutoCommit(false);
- PreparedStatement pstm = null;
- FileInputStream input = new FileInputStream("C:\Users\Desktop\a1.xls");
- POIFSFileSystem fs = new POIFSFileSystem(input);
- Workbook workbook;
- workbook = WorkbookFactory.create(fs);
- Sheet sheet = workbook.getSheetAt(0);
- Row row;
- for (int i = 1; i <= sheet.getLastRowNum(); i++) {
- row = (Row) sheet.getRow(i);
- String name = row.getCell(0).getStringCellValue();
- String add = row.getCell(1).getStringCellValue();
- int contact = (int) row.getCell(2).getNumericCellValue();
- String email = row.getCell(3).getStringCellValue();
- String sql = "INSERT INTO employee (name, address, contactNo, email) VALUES('" + name + "','" + add + "'," + contact + ",'" + email + "')";
- pstm = (PreparedStatement) con.prepareStatement(sql);
- pstm.execute();
- System.out.println("Import rows " + i);
- }
- con.commit();
- pstm.close();
- con.close();
- input.close();
- System.out.println("Success import excel to mysql table");
- } catch (IOException e) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement