要求:
代码:
JSP页面代码:
//这个请求千万不要忘记!!!!我会写在最后,这是动态拼Excel表格样式的代码
${ctx}/teacherManage/exportMXExcel.do" id="queryTeacherForm" name="fm" class="form-horizontal">
JS中代码:
mini.parse();
var ctx = mini.get("ctx").getValue();
var form = new mini.Form("#exportForm");
function resetF() {
form.reset();
}
//津贴明细导出
function checkOutMX() {
var start_date = mini.get("start_date").getText();
var end_date = mini.get("end_date").getText();
if((start_date!=null&&start_date!='') && (end_date != null&&end_date!='')){
if(start_date>end_date){
mini.alert("开始时间不能大于结束时间!");
return;
}
}
if(!exportExcelCheckMX()){
return;
}
$("#queryTeacherForm").submit();
}
//津贴明细导出前校验
function exportExcelCheckMX() {
var flag = false;
var start_date = mini.get("start_date").getText();
var end_date = mini.get("end_date").getText();
$.ajax({
type : 'POST',
async : false,
url : ctx + "/teacherManage/exportExcelCheckMX.do",
data : {
start_date:start_date,
end_date:end_date
},
dataType : "json",
success : function(data) {
if(data.flag == "0"){
flag = true;
}else{
mini.alert(data.msg);
}
},
error : function(e) {
mini.alert("系统异常,请稍后再试!");
}
});
return flag;
}
Controller中代码:
/**
* 讲师津贴明细报表导出前校验
* @param trainee
* @return
* @throws ParseException
*/
@RequestMapping(value = "/exportExcelCheckMX.do")
public @ResponseBody ComResultDTO CReportExportExcelCheckMX(HttpServletRequest request,HttpSession session,PageDTO pageDTO){
logger.info("============讲师津贴明细导出前校验============");
TeacherManagerDto req = new TeacherManagerDto();
String start_date = request.getParameter("start_date");
String end_date = request.getParameter("end_date");
req.setStart_date(start_date);
req.setEnd_date(end_date);
String manage_com =SessionUtil.getUser().getManageCom();
req.setManage_com(manage_com);
pageDTO.setParameters(req);
int startNum = (pageDTO.getPageIndex()-1)*pageDTO.getPageSize();
pageDTO.setStartNum(startNum);
List
ComResultDTO comResultDTO = new ComResultDTO();
if(queryList != null && queryList.size() > 50000){
comResultDTO.setFlag("1");
comResultDTO.setMsg("导出报表上限为50000条,数据量过大,导出失败!");
logger.info("============导出报表上限为50000条,数据量过大,导出失败!============");
}else if(queryList != null && queryList.size() == 0){
comResultDTO.setFlag("1");
comResultDTO.setMsg("导出数据为空,导出失败!");
logger.info("============导出数据为空,导出失败!============");
}else{
comResultDTO.setFlag("0");
}
return comResultDTO;
}
service中代码:
@Override
public List
return teacherManagerMapper.selectTeacherChargeByPageDTO(pageDTO);
}
mapper.xml中代码:
from teacher a LEFT JOIN teacher_charge tc on a.teacher_id&#61;tc.teacher_id where a.created_time >&#61; #{parameters.start_date} and a.created_time <&#61; #{parameters.end_date} and a.manage_com like concat(#{parameters.manage_com},&#39;%&#39;)
GROUP BY a.teacher_id
limit #{startNum,jdbcType&#61;DECIMAL},#{pageSize,jdbcType&#61;DECIMAL}
动态拼Excel表格样式的代码&#xff1a;
Controller中代码:
/**
* 讲师津贴明细报表导出功能
* &#64;param requestDTO
* &#64;param request
* &#64;param response
* &#64;throws Exception
*/
&#64;RequestMapping(value &#61; "/exportMXExcel.do")
public void CReportExportExcelMX(TeacherManagerDto requestDTO,HttpServletRequest request,
HttpServletResponse response,PageDTO pageDTO)
throws Exception {
String manage_com &#61;SessionUtil.getUser().getManageCom();
requestDTO.setManage_com(manage_com);
pageDTO.setParameters(requestDTO);
//封装搜索引擎参数
//Map
/*map &#61;*/
teacherChargeQueryService.reportExcelMX(request,response,pageDTO);
/*ExcelView viewExcel &#61; new ExcelView();
HSSFWorkbook arg1&#61; new HSSFWorkbook();
viewExcel.buildExcelDocument1(map, arg1, request, response);*/
}
Service中代码&#xff1a;
public Map
// 表头
//String[] tableHeader &#61; { "分公司", "中支", "构编编码", "姓名", "人员类别&#xff08;前线、后线&#xff09;","讲师类别&#xff08;专职讲师、督训、兼职讲师)","岗位职级 &#xff08;xx级督训&#xff09;", "研发津贴",
// "授课津贴","助教津贴","项目津贴","金额合计"};
// 表名
//String tableName &#61; "讲师津贴明细报表";
//Map
//map.put("teacherList", teacherList);
//map.put("tableHeader", tableHeader);
//map.put("tableName", tableName);
//return map;
String filename &#61; "TeacherCharge";
// 获得输出流&#xff0c;该输出流的输出介质是客户端浏览器
OutputStream output &#61; response.getOutputStream();
response.reset();
response.setHeader("Content-disposition", "attachment;filename&#61;" &#43; filename &#43; ".xls");
response.setContentType("application/msexcel");
// 创建可写入的Excel工作薄&#xff0c;且内容将写入到输出流&#xff0c;并通过输出流输出给客户端浏览
WritableWorkbook wk &#61; Workbook.createWorkbook(output);
/// 创建可写入的Excel工作表
WritableSheet sheet &#61; wk.createSheet("讲师津贴明细报表", 0);
sheet.setColumnView(0, 10);
sheet.setColumnView(1, 15);
sheet.setColumnView(2, 20);
sheet.setColumnView(3, 13);
sheet.setColumnView(4, 13);
sheet.setColumnView(5, 15);
sheet.setColumnView(6, 15);
sheet.setColumnView(7, 15);
sheet.setColumnView(8, 15);
sheet.setColumnView(9, 15);
sheet.setColumnView(10, 15);
sheet.setColumnView(11, 15);
sheet.setColumnView(12, 15);
sheet.setColumnView(13, 15);
sheet.setColumnView(14, 15);
sheet.setColumnView(15, 15);
sheet.setColumnView(16, 15);
// 把单元格&#xff08;column, row&#xff09;到单元格&#xff08;column1, row1&#xff09;进行合并。
// mergeCells(column, row, column1, row1);
//sheet.mergeCells(0, 0, 5, 0);// 单元格合并方法
// 创建WritableFont 字体对象&#xff0c;参数依次表示黑体、字号12、粗体、非斜体、不带下划线、亮蓝色
WritableFont titleFont0 &#61; new WritableFont(WritableFont.createFont("宋体"), 18, WritableFont.BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableFont titleFont1 &#61; new WritableFont(WritableFont.createFont("宋体"), 12, WritableFont.BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableFont titleFont2 &#61; new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
// 创建WritableCellFormat对象&#xff0c;将该对象应用于单元格从而设置单元格的样式
WritableCellFormat titleFormat0 &#61; new WritableCellFormat();
titleFormat0.setFont(titleFont0);
titleFormat0.setAlignment(Alignment.CENTRE);
titleFormat0.setVerticalAlignment(VerticalAlignment.CENTRE);
titleFormat0.setBorder(Border.ALL, BorderLineStyle.THIN);
titleFormat0.setWrap(true);
WritableCellFormat titleFormat1 &#61; new WritableCellFormat();
// 设置字体格式
titleFormat1.setFont(titleFont1);
// 设置文本水平居中对齐
titleFormat1.setAlignment(Alignment.CENTRE);
// 设置文本垂直居中对齐
titleFormat1.setVerticalAlignment(VerticalAlignment.CENTRE);
// 设置背景颜色
titleFormat1.setBackground(Colour.YELLOW);
titleFormat1.setBorder(Border.ALL, BorderLineStyle.THIN);
// 设置自动换行
titleFormat1.setWrap(true);
WritableCellFormat titleFormat2 &#61; new WritableCellFormat();
titleFormat2.setFont(titleFont2);
titleFormat2.setAlignment(Alignment.CENTRE);
titleFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);
titleFormat2.setWrap(true);
titleFormat2.setBorder(Border.ALL, BorderLineStyle.THIN);
/*ClassInfo classInfo &#61; classInfoMapper.selectByPrimaryKey(Long.valueOf(class_id)); int flag1 &#61; 0 ; if(list3.size() >flag1 ){ // Label lab_00&#61;new Label(0,0,"学员考试成绩一览表",titleFormat1); // 将定义好的Label对象添加到工作表上&#xff0c;这样工作表的第一列第一行的内容为‘学员考试成绩一览表’并应用了titleFormat定义的样式 // sheet.addCell(lab_00); // 定义数字格式 // NumberFormat nf&#61;new NumberFormat("0.00"); // 类似于Label对象&#xff0c;区别Label表示文本数据&#xff0c;Number表示数值型数据 // Number numlab_22&#61;new Number(2,2,78,wcf); // 定义日期格式 // 创建WritableCellFormat对象 // WritableCellFormat datewcf&#61;new WritableCellFormat(df); // 类似于Label对象&#xff0c;区别Label表示文本数据&#xff0c;DateTime表示日期型数据 // DateTime dtLab_42&#61;new DateTime(4,2,new Date(),datewcf); // 将定义的工作表输出到之前指定的介质中&#xff08;这里是客户端浏览器&#xff09; wk.write(); // 操作完成时&#xff0c;关闭对象&#xff0c;释放占用的内存空间 wk.close(); return null; from teacher a LEFT JOIN teacher_charge tc on a.teacher_id&#61;tc.teacher_id where a.created_time >&#61; #{parameters.start_date} and a.created_time <&#61; #{parameters.end_date} and a.manage_com like concat(#{parameters.manage_com},&#39;%&#39;)
List
//String[] tableHeader &#61; { "分公司", "中支", "机构编码", "姓名", "人员类别&#xff08;前线、后线&#xff09;","讲师类别&#xff08;专职讲师、督训、兼职讲师)",
//"岗位职级 &#xff08;xx级督训&#xff09;", "研发津贴",
// "授课津贴","助教津贴","项目津贴","金额合计"};
//sheet.addCell(new Label(0, 0, "讲师津贴明细报表", titleFormat0));
sheet.addCell(new Label(0, 0, "序号", titleFormat1));
sheet.addCell(new Label(1, 0, "分公司", titleFormat1));
sheet.addCell(new Label(2, 0, "中支", titleFormat1));
sheet.addCell(new Label(3, 0, "机构编码", titleFormat1));
sheet.addCell(new Label(4, 0, "姓名", titleFormat1));
sheet.addCell(new Label(5, 0, "人员类别&#xff08;前线、后线&#xff09;", titleFormat1));
sheet.addCell(new Label(6, 0, "讲师类别&#xff08;专职讲师、督训、兼职讲师)", titleFormat1));
sheet.addCell(new Label(7, 0, "岗位职级 &#xff08;xx级督训&#xff09;", titleFormat1));
sheet.mergeCells(8, 0, 9, 0);// 单元格合并方法
sheet.addCell(new Label(8, 0, "研发津贴", titleFormat1));
/*sheet.addCell(new Label(9, 1, "研发津贴", titleFormat1));*/
sheet.mergeCells(10, 0, 11, 0);
sheet.addCell(new Label(10, 0, "授课津贴", titleFormat1));
/*sheet.addCell(new Label(11, 1, "授课津贴", titleFormat1));*/
sheet.mergeCells(12, 0, 13, 0);
sheet.addCell(new Label(12, 0, "助教津贴", titleFormat1));
/*sheet.addCell(new Label(13, 1, "助教津贴", titleFormat1));*/
sheet.mergeCells(14, 0, 15, 0);
sheet.addCell(new Label(14, 0, "项目津贴", titleFormat1));
/*sheet.addCell(new Label(15, 1, "项目津贴", titleFormat1));*/
/*sheet.addCell(new Label(16, 1, "金额合计", titleFormat1));*/
sheet.addCell(new Label(16, 0, "金额合计", titleFormat1));
sheet.addCell(new Label(8, 1, "项目", titleFormat1));
sheet.addCell(new Label(9, 1, "金额", titleFormat1));
sheet.addCell(new Label(10, 1, "项目", titleFormat1));
sheet.addCell(new Label(11, 1, "金额", titleFormat1));
sheet.addCell(new Label(12, 1, "项目", titleFormat1));
sheet.addCell(new Label(13, 1, "金额", titleFormat1));
sheet.addCell(new Label(14, 1, "项目", titleFormat1));
sheet.addCell(new Label(15, 1, "金额", titleFormat1));
sheet.mergeCells(0, 0, 0, 1);
sheet.mergeCells(1, 0, 1, 1);
sheet.mergeCells(2, 0, 2, 1);
sheet.mergeCells(3, 0, 3, 1);
sheet.mergeCells(4, 0, 4, 1);
sheet.mergeCells(5, 0, 5, 1);
sheet.mergeCells(6, 0, 6, 1);
sheet.mergeCells(7, 0, 7, 1);
sheet.mergeCells(16, 0, 16, 1);
List
if(teacherList.size()!&#61;0){
int flag2 &#61; 0 ;
for (int i &#61; 0; i
Long teacher_id &#61;teacherList.get(i).getTeacher_id();
List
for (int j &#61; 0; j
sheet.addCell(new Label(9, 2 &#43; j&#43;flag2, list1.get(j).getJin_e(), titleFormat2));
}
flag1&#61;list1.size();
List
for (int j &#61; 0; j
sheet.addCell(new Label(10, 2 &#43; j&#43;flag2, list2.get(j).getCourse_name(), titleFormat2));
sheet.addCell(new Label(11, 2 &#43; j&#43;flag2, list2.get(j).getJin_e(), titleFormat2));
}
if(list2.size() > flag1){
flag1&#61;list2.size();
}
List
for (int j &#61; 0; j
sheet.addCell(new Label(13, 2 &#43; j&#43;flag2, list3.get(j).getJin_e(), titleFormat2));
}
flag1&#61;list3.size();
}
List
for (int j &#61; 0; j
sheet.addCell(new Label(14, 2 &#43; j&#43;flag2, list4.get(j).getCourse_name(), titleFormat2));
sheet.addCell(new Label(15, 2 &#43; j&#43;flag2, list4.get(j).getJin_e(), titleFormat2));
}
if(list4.size() > flag1){
flag1&#61;list4.size();
}
sheet.mergeCells(0,2&#43;flag2&#43;i, 0, 2&#43;flag2&#43;flag1-1&#43;i);
sheet.mergeCells(1,2&#43;flag2&#43;i, 1, 2&#43;flag2&#43;flag1-1&#43;i);
sheet.mergeCells(2,2&#43;flag2&#43;i, 2, 2&#43;flag2&#43;flag1-1&#43;i);
sheet.mergeCells(3,2&#43;flag2&#43;i, 3, 2&#43;flag2&#43;flag1-1&#43;i);
sheet.mergeCells(4,2&#43;flag2&#43;i, 4, 2&#43;flag2&#43;flag1-1&#43;i);
sheet.mergeCells(5,2&#43;flag2&#43;i, 5, 2&#43;flag2&#43;flag1-1&#43;i);
sheet.mergeCells(6,2&#43;flag2&#43;i, 6, 2&#43;flag2&#43;flag1-1&#43;i);
sheet.mergeCells(7,2&#43;flag2&#43;i, 7, 2&#43;flag2&#43;flag1-1&#43;i);
sheet.mergeCells(16,2&#43;flag2&#43;i,16, 2&#43;flag2&#43;flag1-1&#43;i);
sheet.addCell(new Label(0, 2&#43;flag2, String.valueOf(i &#43; 1), titleFormat2));
sheet.addCell(new Label(1, 2&#43;flag2, teacherList.get(i).getFen_gongsi(), titleFormat2));
sheet.addCell(new Label(2, 2&#43;flag2, teacherList.get(i).getZhong_zhi(), titleFormat2));
sheet.addCell(new Label(3, 2&#43;flag2, teacherList.get(i).getManage_com(), titleFormat2));
sheet.addCell(new Label(4, 2&#43;flag2, teacherList.get(i).getName(), titleFormat2));
sheet.addCell(new Label(5, 2&#43;flag2, teacherList.get(i).getIf_employee(), titleFormat2));
sheet.addCell(new Label(6, 2&#43;flag2, teacherList.get(i).getTeacher_type(), titleFormat2));
sheet.addCell(new Label(7, 2&#43;flag2, teacherList.get(i).getTeacher_grade(), titleFormat2));
sheet.addCell(new Label(16,2&#43;flag2, teacherList.get(i).getZongji(), titleFormat2));
flag2&#61;flag2&#43;(flag1&#61;&#61;0?1:flag1);
}
}
// 添加Label对象&#xff0c;参数依次表示在第一列&#xff0c;第一行&#xff0c;内容&#xff0c;使用的格式
//
// WritableCellFormat cloumnTitleFormat&#61;new WritableCellFormat();
//
// cloumnTitleFormat.setFont(new
// WritableFont(WritableFont.createFont("宋体"),10,WritableFont.BOLD,false));
//
// cloumnTitleFormat.setAlignment(Alignment.CENTRE);
//
// Label lab_01&#61;new Label(0,1,"姓名",cloumnTitleFormat);
//
// Label lab_11&#61;new Label(1,1,"班级",cloumnTitleFormat);
//
// Label lab_21&#61;new Label(2,1,"笔试成绩",cloumnTitleFormat);
//
// Label lab_31&#61;new Label(3,1,"上机成绩",cloumnTitleFormat);
//
// Label lab_41&#61;new Label(4,1,"考试日期",cloumnTitleFormat);
//
// sheet.addCell(lab_01);
//
// sheet.addCell(lab_11);
//
// sheet.addCell(lab_21);
//
// sheet.addCell(lab_31);
//
// sheet.addCell(lab_41);
//
// sheet.addCell(new Label(0,2,"李明"));
//
// sheet.addCell(new Label(1,2,"As178"));
//
// WritableCellFormat wcf&#61;new WritableCellFormat(nf);
//
// sheet.addCell(numlab_22);
//
// sheet.addCell(newNumber(3,2,87,new WritableCellFormat(new
// NumberFormat("#.##") )));
//
// DateFormat df&#61;new DateFormat("yyyy-MM-dd hh:mm:ss");
//
// sheet.addCell(dtLab_42);
// 加下划线这部分代码是B/S模式中采用的输出方式&#xff0c;而不是输出到本地指定的磁盘目录。该代码表示将temp.xls的Excel文件通过应答实体&#xff08;response&#xff09;输出给请求的客户端浏览器&#xff0c;下载到客户端本地&#xff08;保存或直接打开&#xff09;。若要直接输出到磁盘文件可采用下列代码替换加下划线这部分代码
// File file&#61;new File("D://temp.xls");
// WritableWorkbook wwb &#61; Workbook.createWorkbook(file);
}
四个红色下划线需要的SQL语句查询&#xff1a;
GROUP BY a.teacher_id
limit #{startNum,jdbcType&#61;DECIMAL},#{pageSize,jdbcType&#61;DECIMAL}