1.先看导出目标文件需要的字段都存不存在,
存在继续处理,不存在就添加。
例如,我这里需要在若依的用户表在添加一个银行账户数据,
//银行卡号
private String accountNumber;public String getAccountNumber() {
return accountNumber;
}public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
好,假设,现在需要的字段都有了,然后我们继续下面的步骤。
2.因为我们要做导出,我们可以先去你要添加导出的页面,
把导出的按钮添加上,以及对应的方法地址写好。
<a class="btn btn-warning" οnclick="downWagesbank(yearStr,monthStr,nameStr)">
<i class="fa fa-download"></i> 导出银行报表
</a>
function downWagesbank(year, month, name) {
console.log("导出工资"+name);
if (name == null || name == "undefind" || name == "") {
window.open(ctx + "jh_product/bank/exportbank?year=" + yearStr + "&month=" + monthStr+"&name=" + name, "下载", true);
// alert("请选择公司!");
return;
}
window.open(ctx + "jh_product/bank/exportbank?year=" + year + "&month=" + month + "&name=" + name, "下载", true);
}
我在这解释一下这里面的 紫色地址部分,他们地址在哪来的
window.open(ctx + "jh_product/bank/exportbank?year=" + yearStr + "&month=" + monthStr+"&name=" + name, "下载", true);
它来自于controller的请求访问地址 主地址+方法地址
3.第三步,去写controller,写方法,给出请求接口,好让前端可以请求访问
/**
* 导出企业账号信息列表
*/
@RequiresPermissions("jh_product:bank:export")
@Log(title = "企业账号信息", businessType = BusinessType.EXPORT)
@PostMapping("/export")
@ResponseBody
public AjaxResult export(TBank tBank) {
List<TBank> list = tBankService.selectTBankList(tBank);
ExcelUtil<TBank> util = new ExcelUtil<TBank>(TBank.class);
return util.exportExcel(list, "企业账号信息数据");
}//上面那部分代码,是原始就有的,是导出方法,因为我们要导出,这次做的处理不一样,所以我们再添加一个导出方法,@RequiresPermissions("jh_product:bank:export")这部分不用写
@Log(title = "企业账号信息", businessType = BusinessType.EXPORT)
@GetMapping("/exportbank")
@ResponseBody
public void exportbank(HttpServletResponse response, String name, String year, String month) {//让service去调用导出新写的导出方法
Map map = tBankService.selectTBankListexport(year, month, name);
}
下面这段代码是controller添加的完整代码,但是咱们先不看,一会我会详细讲
@Log(ti下面tle = "企业账号信息", businessType = BusinessType.EXPORT)
@GetMapping("/exportbank")
@ResponseBody
public void exportbank(HttpServletResponse response, String name, String year, String month) {
Map map = tBankService.selectTBankListexport(year, month, name);
String text = "ATNU:";
text+=map.get("atnu").toString()+"\nMICN:";
text+=map.get("micn").toString()+"\nCUNM:";
text+=map.get("cunm").toString()+"\nMIAC:";
text+=map.get("miac").toString()+"\nEYMD:";
text+=map.get("eymd").toString()+"\n";
List<Map> userList= (List<Map>) map.get("usermaps");
text+="COUT:"+userList.size()+"\n";
for(Map user:userList){
text+=user.get("accountNumber").toString()+"|"+user.get("removeTax").toString()+"|"+user.get("user_name").toString()+"| |\n";
}
BufferedOutputStream buff = null;
ServletOutputStream outStr = null;
response.setContentType("application/octet-stream;charset=UTF-8");
try {
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode( year + "年" + month + "月"+name+".txt", "UTF-8"));
outStr = response.getOutputStream();
buff = new BufferedOutputStream(outStr);
buff.write(text.getBytes("UTF-8"));
buff.flush();
buff.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
4.去service写这个方法:
/**
* 查询企业账号信息列表
*
* @param tBank 企业账号信息
* @return 企业账号信息集合
*/
public Map selectTBankListexport(String year, String mouth,String name);
5.对应的要写实现类,实现这个接口:
如果你需要用到其他mapper,那你需要吧mapper引进来
@Autowired
private TAttendRecordMapper tAttendRecordMapper;
好,我们看重点:
/**
* 查询企业账号信息列表
*
* @param tBank 企业账号信息
* @return 企业账号信息
*/
@Override
public Map selectTBankListexport(String year, String mouth, String name) {//我们要去调mapper,查询数据库
Map tBanks = tBankMapper.selectTBankListexport(name);}
下面这段代码是impl添加的完整代码,但是咱们先不看,一会我会详细讲
/**
* 查询企业账号信息列表
*
* @param tBank 企业账号信息
* @return 企业账号信息
*/
@Override
public Map selectTBankListexport(String year, String mouth, String name) {
Map tBanks = tBankMapper.selectTBankListexport(name);
List<Map> usermaps = tBankMapper.selectUsers(year, mouth, name);
for (Map map : usermaps) {
String user_id = map.get("user_id").toString();
String usersarary = map.get("usersarary").toString();
String workdaynum = map.get("workdaynum").toString();//工作日
BigDecimal bigDecimal = new BigDecimal(usersarary);
BigDecimal bigDecimal1 = new BigDecimal(workdaynum);
// 日工资放进map
BigDecimal daysalary = bigDecimal.divide(bigDecimal1, 2);
map.put("daysalary", daysalary);
String sectionAM = map.get("sectionAM").toString();
String sectionPM = map.get("sectionPM").toString();
BigDecimal multiplyAM = new BigDecimal(sectionAM).multiply(new BigDecimal(3.5));//上午小时数
BigDecimal multiplyPM = new BigDecimal(sectionPM).multiply(new BigDecimal(4));//下午小时数
BigDecimal add = multiplyAM.add(multiplyPM);//总小时数
BigDecimal divide = add.divide(new BigDecimal(7.5), 2, BigDecimal.ROUND_HALF_DOWN);//漏刷天数
BigDecimal subtract = new BigDecimal(workdaynum).subtract(divide);//到岗天数
map.put("subtract", subtract);//到岗天数
BigDecimal paidInWages = daysalary.multiply(subtract);//实到工资(到岗工资)
map.put("paidInWages", paidInWages);//到岗工资
TAttendRecord tAttendRecord = new TAttendRecord();
tAttendRecord.setUserId(Long.valueOf(user_id));
tAttendRecord.setYear(Long.valueOf(year));
tAttendRecord.setMonth(Long.valueOf(mouth));
//查询上午打卡时间
List<Map> mapsAm = tAttendRecordMapper.selectClockam(tAttendRecord);
//查询下午打卡时间
List<Map> mapsPm = tAttendRecordMapper.selectClockpm(tAttendRecord);
Long totalMinutes = 0L;//迟到总分钟数?// 循环上午打卡信息
for (Map mapAm : mapsAm) {
// 获取打卡时间
String time = String.valueOf(mapAm.get("att_time"));
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 截取年月日
String strsAm = time.substring(0, 10);//年月日
// 年月日拼接时间点
strsAm += " 8:30:00";//年月日拼接时间
try {
Date parse1 = formatter.parse(time.replace("T", " "));//打卡时间
Date parse = formatter.parse(strsAm);//Sting转date,时间字符串转时间格式 2022-08-01 08:30:00
Long datePoor = getDatePoor(parse1, parse);//因为打卡时间超过8点30,用打卡时间减去8点30,得到迟到分钟数
totalMinutes += datePoor;
} catch (ParseException e) {
System.out.println(e.getMessage());
System.out.println("时间格式转换失败");
}
}
// 循环下午打卡信息
for (Map mapPm : mapsPm) {
// 获取打卡时间
String time = String.valueOf(mapPm.get("att_time"));
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 截取年月日
String strsPm = time.substring(0, 10);//年月日
// 年月日拼接时间点
strsPm += " 18:00:00";//年月日拼接时间
try {
//replace将”T”替换成空格“ ”
Date parse1 = formatter.parse(time.replace("T", " "));//打卡时间
Date parse = formatter.parse(strsPm);//Sting转date,时间字符串转时间格式 2022-08-01 08:30:00
Long datePoor = getDatePoor(parse, parse1);//因为打卡时间小于6:00,用6点减去打卡时间,得到早退分钟数
totalMinutes += datePoor;
} catch (ParseException e) {
System.out.println("时间格式转换失败");
}
}
BigDecimal multiply = new BigDecimal(totalMinutes).divide(new BigDecimal(30), 2).multiply(new BigDecimal(-100));
//放入总迟扣除钱数
map.put("multiply", multiply);
BigDecimal should = paidInWages.subtract(multiply);//应付工资
map.put("should", should);
BigDecimal yanglao = new BigDecimal(map.get("yanglao").toString());
BigDecimal gongshang = new BigDecimal(map.get("gongshang").toString());
BigDecimal shiye = new BigDecimal(map.get("shiye").toString());
BigDecimal yiliao = new BigDecimal(map.get("yiliao").toString());
BigDecimal shengyu = new BigDecimal(map.get("shengyu").toString());
BigDecimal gongjijin = new BigDecimal(map.get("gongjijin").toString());
BigDecimal removeFive = should.subtract(yanglao).subtract(gongshang).subtract(shiye).subtract(yiliao).subtract(shengyu).subtract(gongjijin);
map.put("removeFive", removeFive);//扣除五险一金后
BigDecimal tax = new BigDecimal(map.get("tax").toString());
map.put("removeTax", removeFive.subtract(tax));//实发
}
tBanks.put("usermaps", usermaps);
return tBanks;
}
5.mapper
/**
* 查询企业账号信息列表
*
* @param tBank 企业账号信息
* @return 企业账号信息集合
*/
public List<TBank> selectTBankList(TBank tBank);//下面两条是新添加的查询语句,因为一条语句没法查全,所以写了俩,查什么要跟你需要导出的信息来看
public Map selectTBankListexport(@Param("name") String name);public List<Map>selectUsers(@Param("year") String year,@Param("month") String mouth,@Param("name") String name);
6.mapper.xml 实际查询数据库语句
<select id="selectTBankListexport" resultType="map">
select atnu, micn, cunm, miac, eymd
from t_bank
where cunm like concat("%",#{name},"%")
</select><select id="selectUsers" resultType="map">
SELECT u.user_id,
u.user_name,
ifnull(u.accountNumber,'') as accountNumber,
ifnull((select s.salary
from t_salary s
where s.user_id = u.user_id
and s.effect_date <=
concat(#{year}, '-', #{month}, '-', DAY ( LAST_DAY( CONCAT( #{year}, '-', #{month}, '-01')
)))
order by s.effect_date DESC LIMIT 1 ),0) usersarary,
(SELECT ((SELECT DAY ( LAST_DAY( CONCAT( concat( #{year},'-', #{month} ), '-01' ) ))) - count(*))
FROM
t_rest
WHERE
YEAR = #{year}
AND MONTH = #{month}) AS workdaynum, (
select count(*)
from t_attend_record
where year =#{year}
and month =#{month}
and user_id=u.user_id
and type =4
and section =0
) as sectionAM, (
select count(*)
from t_attend_record
where year =#{year}
and month =#{month}
and user_id=u.user_id
and type =4
and section =1
) as sectionPM,
ifnull(( select individual_insurance_amount
from t_insurance_details
where user_id=u.user_id
and insurance_type=0
and year =#{year}
and month =#{month})
, 0) as yanglao,
ifnull(( select individual_insurance_amount
from t_insurance_details
where user_id=u.user_id
and insurance_type=1
and year =#{year}
and month =#{month}) , 0) as gongshang,
ifnull((
select individual_insurance_amount
from t_insurance_details
where user_id=u.user_id
and insurance_type=2
and year =#{year}
and month =#{month})
, 0) as shiye,
ifnull((
select individual_insurance_amount
from t_insurance_details
where user_id=u.user_id
and insurance_type=3
and year =#{year}
and month =#{month}), 0) as yiliao,
ifnull(( select individual_insurance_amount
from t_insurance_details
where user_id=u.user_id
and insurance_type=4
and year =#{year}
and month =#{month}), 0) as shengyu,
ifnull(( select individual_insurance_amount
from t_insurance_details
where user_id=u.user_id
and insurance_type=5
and year =#{year}
and month =#{month}), 0) as gongjijin,
ifnull((
select individual_tax_amount
from t_personal_income_tax
where user_id=u.user_id
and year =#{year}
and month =#{month}),0) as tax
FROM
sys_user u
</select>
8。要查询的数据我们都获取到了,此时要去impl实现类,对查到的数据做处理
这部分上面已经讲了作用,我们继续看,
@Autowired
private TAttendRecordMapper tAttendRecordMapper;/**
* 查询企业账号信息列表
*
* @param tBank 企业账号信息
* @return 企业账号信息
*/
@Override
public Map selectTBankListexport(String year, String mouth, String name) {
Map tBanks = tBankMapper.selectTBankListexport(name);
继续:
用一个变量接收 mapper.xml查询出来的语句
这是上面mapper.xml的数据:
<select id="selectUsers" resultType="map">
跟据mappper.xml的这部分resultType="map" 得知接参数的类型是什么,也要参考你controller里的参数类型
注释:如果是resultaMap="domain表名" 如果是map,resultaMap要写成resultType,resultType="map"
好,我们用一个参数接受了数据库查到的数据,然后对数据做处理:
List<Map> usermaps = tBankMapper.selectUsers(year, mouth, name);
for (Map map : usermaps) {
String user_id = map.get("user_id").toString();
String usersarary = map.get("usersarary").toString();
String workdaynum = map.get("workdaynum").toString();//工作日
BigDecimal bigDecimal = new BigDecimal(usersarary);
BigDecimal bigDecimal1 = new BigDecimal(workdaynum);
// 日工资放进map
BigDecimal daysalary = bigDecimal.divide(bigDecimal1, 2);
map.put("daysalary", daysalary);
String sectionAM = map.get("sectionAM").toString();
String sectionPM = map.get("sectionPM").toString();
BigDecimal multiplyAM = new BigDecimal(sectionAM).multiply(new BigDecimal(3.5));//上午小时数
BigDecimal multiplyPM = new BigDecimal(sectionPM).multiply(new BigDecimal(4));//下午小时数
BigDecimal add = multiplyAM.add(multiplyPM);//总小时数
BigDecimal divide = add.divide(new BigDecimal(7.5), 2, BigDecimal.ROUND_HALF_DOWN);//漏刷天数
BigDecimal subtract = new BigDecimal(workdaynum).subtract(divide);//到岗天数
map.put("subtract", subtract);//到岗天数
BigDecimal paidInWages = daysalary.multiply(subtract);//实到工资(到岗工资)
map.put("paidInWages", paidInWages);//到岗工资
TAttendRecord tAttendRecord = new TAttendRecord();
tAttendRecord.setUserId(Long.valueOf(user_id));
tAttendRecord.setYear(Long.valueOf(year));
tAttendRecord.setMonth(Long.valueOf(mouth));
//查询上午打卡时间
List<Map> mapsAm = tAttendRecordMapper.selectClockam(tAttendRecord);
//查询下午打卡时间
List<Map> mapsPm = tAttendRecordMapper.selectClockpm(tAttendRecord);
Long totalMinutes = 0L;//迟到总分钟数?// 循环上午打卡信息
for (Map mapAm : mapsAm) {
// 获取打卡时间
String time = String.valueOf(mapAm.get("att_time"));
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 截取年月日
String strsAm = time.substring(0, 10);//年月日
// 年月日拼接时间点
strsAm += " 8:30:00";//年月日拼接时间
try {
Date parse1 = formatter.parse(time.replace("T", " "));//打卡时间
Date parse = formatter.parse(strsAm);//Sting转date,时间字符串转时间格式 2022-08-01 08:30:00
Long datePoor = getDatePoor(parse1, parse);//因为打卡时间超过8点30,用打卡时间减去8点30,得到迟到分钟数
totalMinutes += datePoor;
} catch (ParseException e) {
System.out.println(e.getMessage());
System.out.println("时间格式转换失败");
}
}
// 循环下午打卡信息
for (Map mapPm : mapsPm) {
// 获取打卡时间
String time = String.valueOf(mapPm.get("att_time"));
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// 截取年月日
String strsPm = time.substring(0, 10);//年月日
// 年月日拼接时间点
strsPm += " 18:00:00";//年月日拼接时间
try {
//replace将”T”替换成空格“ ”
Date parse1 = formatter.parse(time.replace("T", " "));//打卡时间
Date parse = formatter.parse(strsPm);//Sting转date,时间字符串转时间格式 2022-08-01 08:30:00
Long datePoor = getDatePoor(parse, parse1);//因为打卡时间小于6:00,用6点减去打卡时间,得到早退分钟数
totalMinutes += datePoor;
} catch (ParseException e) {
System.out.println("时间格式转换失败");
}
}
BigDecimal multiply = new BigDecimal(totalMinutes).divide(new BigDecimal(30), 2).multiply(new BigDecimal(-100));
//放入总迟扣除钱数
map.put("multiply", multiply);
BigDecimal should = paidInWages.subtract(multiply);//应付工资
map.put("should", should);
BigDecimal yanglao = new BigDecimal(map.get("yanglao").toString());
BigDecimal gongshang = new BigDecimal(map.get("gongshang").toString());
BigDecimal shiye = new BigDecimal(map.get("shiye").toString());
BigDecimal yiliao = new BigDecimal(map.get("yiliao").toString());
BigDecimal shengyu = new BigDecimal(map.get("shengyu").toString());
BigDecimal gongjijin = new BigDecimal(map.get("gongjijin").toString());
BigDecimal removeFive = should.subtract(yanglao).subtract(gongshang).subtract(shiye).subtract(yiliao).subtract(shengyu).subtract(gongjijin);
map.put("removeFive", removeFive);//扣除五险一金后
BigDecimal tax = new BigDecimal(map.get("tax").toString());
map.put("removeTax", removeFive.subtract(tax));//实发
}
tBanks.put("usermaps", usermaps);
return tBanks;
}
这就吧你要获取的数据全写出来,有的数据,需要计算,也在impl实现类里面写,
然后,
我们去controller,去做导出文件格式+放置内容处理
9.controller
@Log(title = "企业账号信息", businessType = BusinessType.EXPORT)
@GetMapping("/exportbank")
@ResponseBody
public void exportbank(HttpServletResponse response, String name, String year, String month) {
Map map = tBankService.selectTBankListexport(year, month, name);
String text = "ATNU:";
text+=map.get("atnu").toString()+"\nMICN:";
text+=map.get("micn").toString()+"\nCUNM:";
text+=map.get("cunm").toString()+"\nMIAC:";
text+=map.get("miac").toString()+"\nEYMD:";
text+=map.get("eymd").toString()+"\n";
List<Map> userList= (List<Map>) map.get("usermaps");
text+="COUT:"+userList.size()+"\n";
for(Map user:userList){
text+=user.get("accountNumber").toString()+"|"+user.get("removeTax").toString()+"|"+user.get("user_name").toString()+"| |\n";
}
BufferedOutputStream buff = null;
ServletOutputStream outStr = null;
response.setContentType("application/octet-stream;charset=UTF-8");
try {
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode( year + "年" + month + "月"+name+".txt", "UTF-8"));
outStr = response.getOutputStream();
buff = new BufferedOutputStream(outStr);
buff.write(text.getBytes("UTF-8"));
buff.flush();
buff.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
到这一步,导出就结束了,希望下次遇到类似问题可以有所帮助。