layui实现列表EXCEL导出
- 前台设置导出按钮。
<a href="javascript:;" class="layui-btn" id="exportExcel"><span><i class="layui-icon"></i>导出</span></a>
- 设置其click事件调用后台方法
$('#exportExcel').on('click',function(){
window.location.href="${base}/risen/zzgl/trade/exportExcel.do?risentbUuid="+'${risentbUuid}';
});
- service层实现方法
@Override
public RisenTradeBase exportExcel(RisenTradeBase model, HttpServletResponse response) {
List<RisenTradeBase> list = this.getRisenTradeBaseDao().findAllList(model);
model.getObjMap().put("dataList", list);
ExcelUtil.exportExcelTradeBase(model,response);
return null;
}
- 后台调用实现方法如下。
public class ExcelUtil {
private static HSSFWorkbook wb = null;
private static HSSFSheet sheet = null;
private static HSSFRow row = null;
private static HSSFCell cell = null;
public static void init() {
wb = new HSSFWorkbook();
sheet = wb.createSheet("Sheet1");
}
public static void exportExcelTradeBase(RisenTradeBase model, HttpServletResponse response) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String type="";
try {
List<RisenTradeBase> list = (List<RisenTradeBase>) model
.getObjMap().get("dataList");
String fileName = "科普教育基地列表";
init();
OutputStream os = response.getOutputStream();
WritableWorkbook wwb = Workbook.createWorkbook(os);
response.reset();
response.setContentType("application/vnd.ms-excel");
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 18,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
Colour.BLACK);
WritableCellFormat font = new WritableCellFormat(wfont);
font.setAlignment(Alignment.CENTRE);
WritableSheet sheet = wwb.createSheet("科普教育基地列表", 0);
Label label = null;
sheet.mergeCells(0, 0, 8, 0);
label = new Label(0, 0, "科普教育基地列表", font);
sheet.addCell(label);
wfont = new WritableFont(WritableFont.createFont("宋体"), 14,
WritableFont.BOLD);
font = new WritableCellFormat(wfont);
font.setWrap(true);
font.setAlignment(Alignment.CENTRE);
font.setVerticalAlignment(VerticalAlignment.CENTRE);
label = new Label(0, 1, "基地名称", font);
sheet.setColumnView(0, 30);
sheet.addCell(label);
label = new Label(1, 1, "上级组织", font);
sheet.setColumnView(1, 30);
sheet.addCell(label);
label = new Label(2, 1, "基地类型", font);
sheet.setColumnView(2, 30);
sheet.addCell(label);
label = new Label(3, 1, "通讯地址", font);
sheet.setColumnView(3, 30);
sheet.addCell(label);
wfont = new WritableFont(WritableFont.createFont("宋体"), 12);
font = new WritableCellFormat(wfont);
font.setWrap(true);
font.setAlignment(Alignment.CENTRE);
font.setVerticalAlignment(VerticalAlignment.CENTRE);
for (int i = 0; i < list.size(); i++) {
RisenTradeBase tradeBase = list.get(i);
label = new Label(0, (i + 2), tradeBase.getRisentbName(), font);
sheet.addCell(label);
label = new Label(1, (i + 2), tradeBase.getRisentbParentName(), font);
sheet.addCell(label);
if(!StringUtils.isEmpty(tradeBase.getRisentbType())){
if(tradeBase.getRisentbType().equals("1")){
type="普通类";
}else if(tradeBase.getRisentbType().equals("2")){
type="传媒类";
}
}
label = new Label(2, (i + 2),type, font);
sheet.addCell(label);
label = new Label(3, (i + 2), tradeBase.getRisentbAddr(), font);
sheet.addCell(label);
}
sheet.getSettings().setOrientation(PageOrientation.PORTRAIT);
sheet.getSettings().setPaperSize(PaperSize.A4);
sheet.getSettings().setFitHeight(297);
sheet.getSettings().setTopMargin(0.5);
sheet.getSettings().setBottomMargin(0.3);
sheet.getSettings().setLeftMargin(0.8);
sheet.getSettings().setRightMargin(0.1);
sheet.getSettings().setFooterMargin(0.07);
sheet.getSettings().setScaleFactor(85);
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName + ".xls");
wwb.write();
wwb.close();
os.flush();
os.close();
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
}
- 关于前台的部分相关可以参考layui的官网。