Java处理excel根据某列的值查询,并将结果显示在其他列中

时间:2019-06-12
本文章向大家介绍Java处理excel根据某列的值查询,并将结果显示在其他列中,主要包括Java处理excel根据某列的值查询,并将结果显示在其他列中使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
package cn.netmis.cmd;

import cn.netmis.cmd.other.Xls;
import cn.netmis.core.util.DBUtils;
import 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 java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
* 操作excel表格
* 读取表格中的工号,同时查询该员工所在部门,填入
*/
public class operExcel {
private static final String XLS = "xls";
private static final String XLSX = "xlsx";

/**
* 检查文件是否存在
* 并检查是否是xls或xlsx文件
* @param file
* @throws IOException
*/
public static void checkFile(File file) throws IOException {
//判断文件是否存在
if(!file.exists()){
throw new FileNotFoundException("文件不存在");
}
//获取文件名
String fileName=file.getName();
//判断文件是否是excel文件
if (!fileName.endsWith(XLS)&&!fileName.endsWith(XLSX)){
throw new IOException(fileName+"不是excel文件");
}
System.out.println("文件存在且格式正确!");
}


/**
* 获取表格
* @param file
* @return
* @throws IOException
*/
public static Workbook getWookBook(File file) throws IOException {
//获取文件名
String fileName=file.getName();
//创建Workbook工作簿对象,表示整个excel
Workbook workbook=null;
//获取Excel文件的io流
InputStream is=new FileInputStream(file);
//根据文件后缀名不同,获得不通的Wookbook实现类对象
if(fileName.endsWith(XLS)){
workbook=new HSSFWorkbook(is);

}else if(fileName.endsWith(XLSX)){
workbook=new XSSFWorkbook(is);
}

return workbook;
}

/**
* 获取一行中所有单元格的内容并存入集合
* @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);
Workbook wb=getWookBook(file);
List<String> handList=new ArrayList<>();
Sheet sheet=wb.getSheetAt(0);
Row row=null;
//循环获取所有员工的消费刷卡记录
//获取到工号并存入集合
for (int j=statIndex;j<endIndex;j++){
row=sheet.getRow(j);

if(row==null){
return handList;
}

Cell cell=row.getCell(3);//获取到表格中所有员工的工号
String cellVal=getCellValue(cell);
System.out.println("-------工号:"+cellVal);
//通过工号查询到该员工的部门
String bmmc=findBm(cellVal,yntv,handList);
System.out.println("-------bmmc:"+bmmc);

Cell setCellVal=row.getCell(1);//要放入部门的列
setCellVal.setCellValue(bmmc);
setCellVal.setCellType(Cell.CELL_TYPE_STRING);
File file1=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;
Statement st = yntv.createStatement();
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";//食堂就餐卡刷卡挂账情况统计表(汉族)
String fileName="qzst.xls";//清真食堂就餐卡刷卡挂账情况统计表
String filePath="/home/ljc/Desktop/st/";//读取时的路径
String descfilePath="/home/ljc/Desktop/st/finishing";//修改后保存的路径
File file=new File(filePath,fileName);
Integer statIndex=3;//行数的开始
Integer endIndex=258;//行数的结束
List<String> cellVal=getTableHanderList(file,yntv,descfilePath,fileName,statIndex,endIndex);
for (String str:cellVal){
System.out.println(str);
}
}


}

原文地址:https://www.cnblogs.com/qfmy07/p/11009678.html