js 请求
//登记表输出
$("#btn_djbsc").click(function() {
debugger;
var keys = $("#GzlxstjRwTabGrid").getKeys();
if(keys.length == 0){
$.alert("请至少选择一条数据!");
}else{
//构建form
$.buildForm("downForm",_path+"/gzl/gzlx_gzlOutExcel.html",{"ids":keys.toString()}).submit();
}
return false;
});
Action
/**
*
*@描述:输出Excel 工作量登记表
*@创建人:kwy(1516)
*@创建时间:2019年8月5日下午3:40:50
*@修改人:
*@修改时间:
*@修改描述:
*@return
*/
public String gzlOutExcel(){
ByteArrayOutputStream outzip = null;
ArchiveOutputStream archOuts = null;
List<File> fileList = new ArrayList<File>();
try {
String ids = getRequest().getParameter("ids");
model.setXxmc(this.getCommonBaseService().getXxxx().get("XXMC"));
model.setIds(ids);
//获取文件List
fileList = service.getExportExcelFromGzldjbsc(model);
outzip = new ByteArrayOutputStream();
archOuts = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, outzip);
File tempDir = DirectoryUtils.getRealPath(BaseConstant.TEMP_PATH);
ZipHelper.zipFiles(fileList, archOuts,tempDir.getAbsolutePath() + File.separator);
this.fileName = new String((model.getXxmc()+"工作登记表.zip").getBytes(),"ISO8859-1");
this.inputStream = new ByteArrayInputStream(outzip.toByteArray());
return Result.ZIP;
}catch (Exception e) {
logException(e);
return ERROR;
} finally{
for(int i=0;i<fileList.size();i++){
if(fileList.get(i).exists()){
fileList.get(i).delete();
}
}
IOUtils.closeQuietly(archOuts);
IOUtils.closeQuietly(outzip);
}
}
Service 调用
@Override
public List<File> getExportExcel(Model model) throws Exception {
//定义变量
List<File> fileList = new ArrayList<File>();
WritableWorkbook wwb = null;
File file = null;
//获取结果信息
JsgzlModel tempModel = dao.getJgModel(gzljgb_idTemp);
file = ExportExcelUtils.createExcelFileGzldjbsc(tempModel);
FileOutputStream stream = new FileOutputStream(file);
//创建excel工作表
wwb = Workbook.createWorkbook(stream);
WritableSheet ws = wwb.createSheet("sheet1", 0);
//写入Excel
ExportExcelUtils.writeExcelFileGzldjbsc(tempModel,xttjjxgzlList, jssbjxgzlList,ws);
wwb.write();
wwb.close();
fileList.add(file);
}
return fileList;
}
Util 调取的工具类
/**
*
*@描述:输出Excel
*@创建人:kwy(1516)
*@创建时间:2019年8月6日上午9:53:50
*@修改人:
*@修改时间:
*@修改描述:
*@param tempModel
*@param xttjjxgzlList
*@param jssbjxgzlList
*@param ws
* @throws WriteException
*/
public static void writeExcelFileGzldjbsc(JsgzlModel tempModel,List<JsgzlModel> xttjjxgzlList, List<JsgzlModel> jssbjxgzlList,WritableSheet ws) throws WriteException {
ws.setPageSetup(PageOrientation.PORTRAIT.PORTRAIT, PaperSize.A4, 0.5d, 0.5d);
//设置页间距
ws.getSettings().setBottomMargin(0.5d);
ws.getSettings().setTopMargin(0.5d);
ws.getSettings().setLeftMargin(0.5d);
ws.getSettings().setRightMargin(0.5d);
//设置居中
ws.getSettings().setHorizontalCentre(true);
//设置表头字体
WritableCellFormat wcf = new WritableCellFormat();
WritableFont wf = new WritableFont(WritableFont.createFont("宋体"));
wf.setPointSize(16);
wcf.setFont(wf);
wcf.setAlignment(Alignment.CENTRE);
wcf.setWrap(true);//设置为true自动换行
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);//上下
//表格内容样式
WritableFont font = new WritableFont(WritableFont.createFont("宋体"));
font.setPointSize(10);
WritableFont fontBT = new WritableFont(WritableFont.createFont("宋体"));
fontBT.setPointSize(12);
WritableCellFormat bodyFormat = new WritableCellFormat(font);
bodyFormat.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //增加边框
bodyFormat.setAlignment(Alignment.CENTRE);//水平
bodyFormat.setVerticalAlignment(VerticalAlignment.CENTRE);//上下
bodyFormat.setWrap(true);
WritableCellFormat bodyTopFormat = new WritableCellFormat(fontBT);
bodyTopFormat.setAlignment(Alignment.CENTRE);//水平
bodyTopFormat.setVerticalAlignment(VerticalAlignment.CENTRE);//上下
bodyTopFormat.setWrap(true);
WritableCellFormat bodyFormatLeft = new WritableCellFormat(font);
bodyFormatLeft.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); //增加边框
bodyFormatLeft.setAlignment(Alignment.LEFT);//水平
bodyFormatLeft.setVerticalAlignment(VerticalAlignment.CENTRE);//上下
bodyFormatLeft.setWrap(true);
WritableCellFormat bodyFormatNoTop = new WritableCellFormat(font);
bodyFormatNoTop.setBorder(jxl.format.Border.LEFT, jxl.format.BorderLineStyle.THIN);
bodyFormatNoTop.setBorder(jxl.format.Border.RIGHT, jxl.format.BorderLineStyle.THIN);
bodyFormatNoTop.setBorder(jxl.format.Border.BOTTOM, jxl.format.BorderLineStyle.THIN);
bodyFormatNoTop.setAlignment(Alignment.LEFT);//水平
bodyFormatNoTop.setVerticalAlignment(VerticalAlignment.CENTRE);//上下
bodyFormatNoTop.setWrap(true);
WritableCellFormat bodyFormatNoBottom = new WritableCellFormat(font);
bodyFormatNoBottom.setBorder(jxl.format.Border.LEFT, jxl.format.BorderLineStyle.THIN);
bodyFormatNoBottom.setBorder(jxl.format.Border.RIGHT, jxl.format.BorderLineStyle.THIN);
bodyFormatNoBottom.setBorder(jxl.format.Border.TOP, jxl.format.BorderLineStyle.THIN);
bodyFormatNoBottom.setAlignment(Alignment.LEFT);//水平
bodyFormatNoBottom.setVerticalAlignment(VerticalAlignment.CENTRE);//上下
bodyFormatNoBottom.setWrap(true);
WritableCellFormat bodyFormatNoTopBottom = new WritableCellFormat(font);
bodyFormatNoTopBottom.setBorder(jxl.format.Border.LEFT, jxl.format.BorderLineStyle.THIN);
bodyFormatNoTopBottom.setBorder(jxl.format.Border.RIGHT, jxl.format.BorderLineStyle.THIN);
bodyFormatNoTopBottom.setAlignment(Alignment.LEFT);//水平
bodyFormatNoTopBottom.setVerticalAlignment(VerticalAlignment.CENTRE);//上下
bodyFormatNoTopBottom.setWrap(true);
//设置列宽
ws.setColumnView(0, 16);
ws.setColumnView(1, 8);
ws.setColumnView(2, 15);
ws.setColumnView(3, 10);
ws.setColumnView(4, 7);
ws.setColumnView(5, 10);
ws.setColumnView(6, 10);
ws.setColumnView(7, 10);
ws.setColumnView(9, 10);
ws.setColumnView(10, 10);
ws.setColumnView(11, 10);
ws.setColumnView(12, 18);
ws.setColumnView(13, 8);
//表头信息
GzldjbscTableHead(tempModel, ws, wcf, bodyFormat);
//内容体 起始行
int startRow = 3;
//获取系统统计教学参数个数
int xttjjxgzlSize = xttjjxgzlList.size();
//生成 参数
if(xttjjxgzlSize>0){
//表体内容
for (JsgzlModel xttjjxgzlModel : xttjjxgzlList) {
ws.addCell(new Label(0, startRow, "系统统计\r\n工作量", bodyFormat));
ws.addCell(new Label(1, startRow, xttjjxgzlModel.getKcmc(), bodyFormat));
ws.addCell(new Label(3, startRow, xttjjxgzlModel.getJxbzc(), bodyFormat));
ws.addCell(new Label(5, startRow, xttjjxgzlModel.getYsgzl(), bodyFormat));
ws.addCell(new Label(6, startRow, xttjjxgzlModel.getJxbrs(), bodyFormat));
ws.addCell(new Label(7, startRow, xttjjxgzlModel.getRsxs(), bodyFormat));
ws.addCell(new Label(8, startRow, xttjjxgzlModel.getJxpjdf(), bodyFormat));
ws.addCell(new Label(9, startRow, xttjjxgzlModel.getJxpjxs(), bodyFormat));
ws.addCell(new Label(10, startRow, xttjjxgzlModel.getCfbxs(), bodyFormat));
ws.addCell(new Label(11, startRow, xttjjxgzlModel.getJxmsxs(), bodyFormat));
ws.addCell(new Label(12, startRow, xttjjxgzlModel.getGsms(), bodyFormat));
ws.addCell(new Label(13, startRow, xttjjxgzlModel.getGzl(), bodyFormat));
// 合并单元格处理
ws.mergeCells(0, 3, 0, startRow);
ws.mergeCells(1, startRow, 2, startRow);
ws.mergeCells(3, startRow, 4, startRow);
++startRow;
}
}else {
ws.addCell(new Label(0, startRow, "系统统计\r\n工作量", bodyFormat));
ws.addCell(new Label(1, startRow, "", bodyFormat));
ws.addCell(new Label(3, startRow, "", bodyFormat));
ws.addCell(new Label(5, startRow, "", bodyFormat));
ws.addCell(new Label(6, startRow, "", bodyFormat));
ws.addCell(new Label(7, startRow, "", bodyFormat));
ws.addCell(new Label(8, startRow, "", bodyFormat));
ws.addCell(new Label(9, startRow, "", bodyFormat));
ws.addCell(new Label(10, startRow, "", bodyFormat));
ws.addCell(new Label(11, startRow, "", bodyFormat));
ws.addCell(new Label(12, startRow, "", bodyFormat));
ws.addCell(new Label(13, startRow, "", bodyFormat));
// 合并单元格处理
ws.mergeCells(0, 3, 0, startRow);
ws.mergeCells(1, startRow, 2, startRow);
ws.mergeCells(3, startRow, 4, startRow);
++startRow;
}
//表尾信息
ws.addCell(new Label(0, startRow, "工作量合计", bodyFormat));
ws.addCell(new Label(1, startRow, tempModel.getJxgzl(), bodyFormat));
ws.addCell(new Label(7, startRow, "工作量合计", bodyFormat));
ws.addCell(new Label(10, startRow, tempModel.getKsgzl(), bodyFormat));
// 合并单元格处理
ws.mergeCells(1, startRow, 6, startRow);
ws.mergeCells(7, startRow, 9, startRow);
ws.mergeCells(10, startRow, 13, startRow);
++startRow;
ws.addCell(new Label(0, startRow, "说明:", bodyFormat));
ws.addCell(new Label(1, startRow, "此表签字盖章后,2份原件存档,(部门)复印件1份存档。", bodyFormat));
// 合并单元格处理
ws.mergeCells(1, startRow, 13, startRow);//表头内容
}