How to Check & Get Specific Excel Cell Values in Java
Below is part of Excel file emp.xlsx:
The task is to traverse all cells in the Excel table to find the one whose value is the specific string and return value of the cell directly on its right. For instance, B1’s value will be returned if the eligible cell we find is A1. If there are multiple cells that meet the specific condition, just return values of the corresponding cells (output from the program).
Suppose the specific string is “James”, we need to find the cell value on the right of the eligible cell. The result is as follows:
Williams
Wilson
It’s convenient to get this done with esProc.
Download esProc DSK edition and free license HERE.
We can execute the following statement with esProc directly from a Java program:
public static void test() { Connection con = null; Statement st; try{ Class.forName(“com.esproc.jdbc.InternalDriver”); con = DriverManager.getConnection(“jdbc:esproc:local://”); st = con.createStatement(); ResultSet rs = st.executeQuery(“=file(\”emp.xlsx\”).xlsimport@w().(~.(if(~==\”James\”,~[+1])).select(~)).conj()”); while(rs.next()) { System.out.println(rs.getObject(1).toString()); } } catch(Exception e) { System.out.println(e); } finally{ // Close database connection if(con != null) { try{ con.close(); } catch(Exception e) { System.out.println(e); } } }}
Read How to Call an SPL Script in Java to learn more about integration of esProc script into a Java program.