Processing the query based on the values Java excel a column, and the results are shown in the other columns

This is the company projects that require employees to help customer inquiries dining sector consumption records where each employee, department and fill this column. (As)

According to the card number of each employee, department where the query, and then fill in the department this column. 
Here is the code for the specific variable value :( requested to be modified according to their needs)


Package cn.netmis.cmd;

Import cn.netmis.cmd.other.Xls;
Import cn.netmis.core.util.DBUtils;
Import the org.apache. poi.hssf.usermodel.HSSFWorkbook;
Import org.apache.poi.ss.usermodel.Cell;
Import org.apache.poi.ss.usermodel.Row;
Import org.apache.poi.ss.usermodel.Sheet;
Import ORG. apache.poi.ss.usermodel.Workbook;
Import org.apache.poi.xssf.usermodel.XSSFWorkbook;

Import the java.io. *;
Import the java.sql.Connection;
Import the java.sql.ResultSet;
Import java.sql.SQLException ;
Import the java.sql.Statement;
Import of java.util.ArrayList;
Import java.util.List;

/ **
* excel spreadsheet operations
* Read the job number in the table, while the employee's department inquiry, fill
* /
public class operExcel {
Private static XLS Final String = "XLS";
Private static XLSX Final String = "XLSX";

/ **
* Check the file whether there
* and check whether xls or xlsx files
* file @param
* @throws IOException
* /
public static void checkFile (file file) throws IOException {
// determine whether a file exists
iF (! File.Exists ()) {
the throw new new FileNotFoundException ( "file does not exist");
}
// get the file name
String fileName = file.getName ();
if // whether a file is excel file
if (fileName.endsWith (XLS) && fileName.endsWith (XLSX)!!) {
throw new IOException (fileName + "is not a excel file");
}
System.out.println ( "present and properly formatted file!");
}


/ **
* Gets table
* @param File
* @return
* @throws IOException
* /
public workbook getWookBook static (file file) throws IOException {
// get the file name
String fileName = file.getName ();
// create a workbook workbook object that represents the entire Excel
workbook workbook = null;
// get the Excel file io stream
InputStream is the FileInputStream new new = (file);
// the different file extension, obtained nowhere Wookbook class object implement
IF (fileName.endsWith (XLS)) {
Workbook = new new HSSFWorkbook (iS);

IF the else} (fileName.endsWith (XLSX)) {
Workbook = new new XSSFWorkbook (IS);
}

return Workbook;
}

/ **
* obtaining contents of each cell in one row and store set
* @param File
* @return
* @ throws IOException
* /
public static List <String> getTableHanderList (File File, Connection yntv, String filePath, String fileName, Integer statIndex, Integer endIndex) throws IOException, SQLException {
checkFile (File);
the Workbook WB = getWookBook (File);
List < String> = handList new new ArrayList <> ();
Sheet Sheet = wb.getSheetAt (0);
Row Row = null;
// cycle to obtain consumer credit card records of all employees
// get the job number and stored in collection
for (int J = statIndex; J <endIndex; J ++) {
Row = sheet.getRow (J);

IF (Row == null) {
return handList;
}

the Cell Cell row.getCell = (. 3); // table obtained All employees work number
String cellVal = getCellValue (the Cell);
System.out.println ( "------- User ID:" + cellVal);
// query to the staff of the department by job number
String bmmc = findBm (cellVal, yntv, handList);
System.out.println ( "------- BMMC:" + BMMC);

the Cell setCellVal row.getCell = (. 1); // for sector into the column
setCellVal. setCellValue (BMMC);
setCellVal.setCellType (Cell.CELL_TYPE_STRING);
File = file1 new new File (filePath, fileName);
OutputStream out=new FileOutputStream(file1);
wb.write(out);

// handList.add(getCellValue(cell));
}
return handList;
}

/**
* 获取单元格的内容
* @param cell
* @return
*/
public static String getCellValue(Cell cell){
String cellValue="";
if(cell==null){
return cellValue;
}
if(cell.getCellType()==cell.CELL_TYPE_NUMERIC){
cell.setCellType(cell.CELL_TYPE_STRING);
}
//判断类型
switch (cell.getCellType()){
case Cell.CELL_TYPE_NUMERIC: //数字
cellValue=String.valueOf(cell.getNumericCellValue()).trim();
break;
case Cell.CELL_TYPE_STRING: //字符串
cellValue=String.valueOf(cell.getStringCellValue()).trim();
break;
}
return cellValue;
}

/**
* 通过工号查询部门并存入集合
* @param cellVal
* @param yntv
* @param handList
* @throws SQLException
*/
public static String findBm(String cellVal,Connection yntv,List<String> handList) throws SQLException {
String sql = "select bmmc from sys_user where userno="+cellVal;
ST = yntv.createStatement the Statement ();
the ResultSet RS = st.executeQuery (SQL);

IF (rs.next ()) {
return rs.getString ( "BMMC");
}
rs.Close ();
st.close () ;

return null;
}

public static void main (String [] args) throws IOException, SQLException {
Connection yntv = DBUtils.createConn ( "config / websql.properties");
// String fileName = "hzst.xls"; // canteen Statistics of dining card credit card losses (Han)
String fileName = "qzst.xls"; // Muslim canteen card swipe Statistics of losses
when // read; String filePath = "/ home / ljc / Desktop / st /" path
String descfilePath = "/ home / ljc / Desktop / st / finishing"; // save the modified path
File File = new new File (filePath, fileName);
Integer statIndex = 3; // start line number
Integer endIndex = 258; // number of rows to the end of
List <String> cellVal = getTableHanderList ( file, yntv, descfilePath, fileName, statIndex , endIndex);
for (String STR: cellVal) {
System.out.println (STR);
}
}


}

Guess you like

Origin www.cnblogs.com/qfmy07/p/11009678.html