• java 导出到excel的几种方式你要知道


    java导出excel常用的方式使用poi apache开源方式导入导出,很多公司自己研发导出组件对于常用的导入导出其实都使用开源组件。

    介绍常用的excel导出方式:

    1,poi 方式

    上图一个我之前写的很老的导出,代码比较麻烦,但是也是比较稳定的一个版本:

    pom依赖:

    1. org.apache.poi
    2. poi
    3. 3.9
    4. org.apache.poi
    5. poi-ooxml-schemas
    6. 3.9
    7. org.apache.poi
    8. poi-ooxml
    9. 3.9

    定义一个下载抽象接口:

    1. package com.bootdo.common.service;
    2. import javax.servlet.http.HttpServletResponse;
    3. import java.util.Map;
    4. public interface DownExcelService {
    5. void downexcel(HttpServletResponse response, Map params) throws Exception;
    6. }

    定义一个抽象接口实现类

    1. package com.bootdo.common.controller.detail;
    2. import com.bootdo.common.service.DownExcelService;
    3. import javax.servlet.http.HttpServletResponse;
    4. import java.util.Map;
    5. public abstract class AbstractDetaliCallBackServiceImpl implements DownExcelService {
    6. @Override
    7. public void downexcel(HttpServletResponse response, Map params) throws Exception {
    8. }
    9. }

    定义实现类集成抽象接口:

    1. package com.bootdo.common.downInterface;
    2. import com.bootdo.common.config.Constant;
    3. import com.bootdo.common.config.WorkflowConfigCodeConstants;
    4. import com.bootdo.common.domain.AdvanceDO;
    5. import com.bootdo.common.service.AdvanceService;
    6. import com.bootdo.common.utils.CommonMethod;
    7. import org.apache.commons.lang3.ObjectUtils;
    8. import org.apache.commons.lang3.StringUtils;
    9. import org.springframework.beans.factory.annotation.Autowired;
    10. import org.springframework.stereotype.Service;
    11. import javax.servlet.http.HttpServletResponse;
    12. import java.util.HashMap;
    13. import java.util.List;
    14. import java.util.Map;
    15. @Service(WorkflowConfigCodeConstants.ADVANCE_DOWNXECEL)
    16. public class AdvaceDownExcelServiceImpl extends AbstractExcelCallBackServiceImpl {
    17. @Autowired
    18. private AdvanceService advanceService;
    19. @Override
    20. public void downexcel(HttpServletResponse response, Map params) throws Exception {
    21. String date = params.get("date");
    22. String name = params.get("name");
    23. String proparentId = params.get("proparentId");
    24. Map map = new HashMap();
    25. if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("date"), ""))) {
    26. String[] dateBteetn = CommonMethod.getDate(params.get("date").toString());
    27. map.put("startDate" , dateBteetn[0]);
    28. map.put("stopDate" , dateBteetn[1]);
    29. }
    30. if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("name"), ""))) {
    31. map.put("name" , params.get("name").toString());
    32. }
    33. if (StringUtils.isNotBlank(ObjectUtils.toString(params.get("proparentId"), ""))) {
    34. map.put("proparentId" , params.get("proparentId").toString());
    35. }
    36. String typeNames = CommonMethod.typeNameExcel(date, name, proparentId);
    37. List list = advanceService.list(map);
    38. CommonMethod.downexcel(response, list, Constant.EXCELE_ADVANCE_STATUS, typeNames, 6);
    39. }
    40. }

    常量策略类:

    1. package com.bootdo.common.config;
    2. /**
    3. * Excel 下载接口
    4. *
    5. * @author yangchang
    6. */
    7. public interface WorkflowConfigCodeConstants {
    8. /**
    9. * 工人总账目
    10. */
    11. String ADVANCE_SUM_DOWNXECEL = "advacnsum_downexcel";
    12. /**
    13. * 工人借支
    14. */
    15. String ADVANCE_DOWNXECEL = "advacn_downexcel";
    16. /**
    17. * 材料
    18. */
    19. String METATLE_DOWNXECEL = "metail_downexcel";
    20. /**
    21. * 公司生活费
    22. */
    23. String COMPANYREMITTANCE_DOWNXECEL = "companyremittance_downexcel";
    24. /**
    25. * 突击信息
    26. */
    27. String ASSAULT_DOWNXECEL = "assault_downexcel";
    28. }

     控制器的调用方式:

    1. package com.bootdo.common.controller;
    2. import com.bootdo.common.annotation.Log;
    3. import com.bootdo.common.config.ApplicationContextProvider;
    4. import com.bootdo.common.service.DownExcelService;
    5. import org.apache.shiro.authz.annotation.RequiresPermissions;
    6. import org.springframework.stereotype.Controller;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import org.springframework.web.bind.annotation.RequestParam;
    9. import javax.servlet.http.HttpServletResponse;
    10. import java.util.Map;
    11. @Controller
    12. @RequestMapping("/common/excel")
    13. public class DownExcelController extends BaseController {
    14. @Log("下载到Excel")
    15. @RequestMapping("/downexcel")
    16. @RequiresPermissions("common:downexcel:downexcel")
    17. public void downexcel(HttpServletResponse response, @RequestParam Map params) {
    18. String type = params.get("type");
    19. try {
    20. DownExcelService workflowCallBackService = (DownExcelService) ApplicationContextProvider.getBean(type);
    21. workflowCallBackService.downexcel(response, params);
    22. } catch (Exception e) {
    23. e.printStackTrace();
    24. }
    25. }
    26. }

    工具类导出:

    1. /**
    2. * 下载excel
    3. *
    4. * @param response
    5. * @param list
    6. * @param status
    7. * @param typename
    8. * @throws Exception
    9. */
    10. public static void downexcel(HttpServletResponse response, List list, String status, String title, int size) throws Exception {
    11. response.setContentType("application/vnd.ms-excel");
    12. DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    13. double money = 0.0;
    14. List colist = null;
    15. String fileName = URLEncoder.encode(title + "信息表" , "utf-8");
    16. response.setHeader("Content-disposition" , "attachment;fileName=" + fileName + ".xls");
    17. // 创建一个输出流
    18. // 创建一个输出流
    19. OutputStream out = response.getOutputStream();
    20. // 创建一个excel工作簿,将输出流给我们的workbook
    21. WritableWorkbook wb = Workbook.createWorkbook(out);
    22. // 创建一个sheet(sheet名,位置)
    23. WritableSheet sheet = wb.createSheet(title, 200);
    24. // 设置样式
    25. // sheet.
    26. sheet.getSettings().setDefaultColumnWidth(15);// 设置列宽
    27. sheet.getSettings().setDefaultRowHeight(500);// 设置行高
    28. // 设置字体(字体,大小,粗细)只创建了字体,但是没有应用这个字体
    29. WritableFont font = new WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD);
    30. // 将字体应用到单元格
    31. WritableCellFormat format = new WritableCellFormat(font);
    32. // 设置边框
    33. format.setBorder(Border.ALL, BorderLineStyle.THIN);
    34. // 设置对齐方式
    35. format.setAlignment(Alignment.CENTRE);
    36. // 如果内容超出列宽,自动换行
    37. format.setWrap(true);
    38. // 将内容写入
    39. WritableFont font2 = new WritableFont(WritableFont.ARIAL, 30, WritableFont.BOLD);
    40. WritableCellFormat format2 = new WritableCellFormat(font2);
    41. format2.setAlignment(Alignment.CENTRE);
    42. sheet.mergeCells(0, 0, size, 0);
    43. // 创建一个标签,存放标题title(存放的同时,将title的位置和格式都存好了)
    44. Label label = new Label(0, 0, title, format2);
    45. // 将标题放入到sheet中
    46. sheet.addCell(label);
    47. switch (status) {
    48. case "1":
    49. // 小标题
    50. sheet.addCell(new Label(0, 1, "编号" , format));
    51. sheet.addCell(new Label(1, 1, "工作内容" , format));
    52. sheet.addCell(new Label(2, 1, "工作进度" , format));
    53. sheet.addCell(new Label(3, 1, "工作名称" , format));
    54. sheet.addCell(new Label(4, 1, "工作记录人" , format));
    55. sheet.addCell(new Label(5, 1, "工作计划开始时间" , format));
    56. sheet.addCell(new Label(6, 1, "工作计划结束时间" , format));
    57. sheet.addCell(new Label(7, 1, "分类" , format));
    58. List dictList = (List) list;
    59. for (int i = 0; i < dictList.size(); i++) {
    60. sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
    61. sheet.addCell(new Label(1, (i + 2), dictList.get(i).getWorkmessage() + "" , format));
    62. sheet.addCell(new Label(2, (i + 2), dictList.get(i).getWorkprogress(), format));
    63. sheet.addCell(new Label(3, (i + 2), dictList.get(i).getWorkname() + "" , format));
    64. sheet.addCell(new Label(4, (i + 2), dictList.get(i).getWorkthis() + "" , format));
    65. sheet.addCell(new Label(5, (i + 2), formatDate(dictList.get(i).getWorkstartdate()) + "" , format));
    66. sheet.addCell(new Label(6, (i + 2), formatDate(dictList.get(i).getWorkstopdate()) + "" , format));
    67. sheet.addCell(new Label(7, (i + 2), getProjectname(dictList.get(i).getProparentid().toString()), format));
    68. }
    69. break;
    70. case "2":
    71. // 小标题
    72. sheet.addCell(new Label(0, 1, "编号" , format));
    73. sheet.addCell(new Label(1, 1, "打款金额" , format));
    74. sheet.addCell(new Label(2, 1, "打款日期" , format));
    75. sheet.addCell(new Label(3, 1, "汇款人名称" , format));
    76. sheet.addCell(new Label(4, 1, "备注" , format));
    77. sheet.addCell(new Label(5, 1, "公司名称" , format));
    78. sheet.addCell(new Label(6, 1, "分类" , format));
    79. List companyadvanceDOS = (List) list;
    80. double companyMoney = 0.0;
    81. for (int i = 0; i < companyadvanceDOS.size(); i++) {
    82. companyMoney += companyadvanceDOS.get(i).getCompanyMoney();
    83. sheet.addCell(new Label(0, (i + 2), i + 1 + "" , format));
    84. sheet.addCell(new Label(1, (i + 2), companyadvanceDOS.get(i).getCompanyMoney() + "" , format));
    85. sheet.addCell(new Label(2, (i + 2), companyadvanceDOS.get(i).getCompanyDate(), format));
    86. sheet.addCell(new Label(3, (i + 2), companyadvanceDOS.get(i).getCmpname(), format));
    87. sheet.addCell(new Label(4, (i + 2), companyadvanceDOS.get(i).getDecptions() + "" , format));
    88. sheet.addCell(new Label(5, (i + 2), companyadvanceDOS.get(i).getCompanyName() + "" , format));
    89. sheet.addCell(new Label(6, (i + 2), getProjectname(companyadvanceDOS.get(i).getProparentId().toString()), format));
    90. }
    91. WritableCellFormat format3 = new WritableCellFormat(font2);
    92. // 追加最后一行
    93. Label label2 = new Label(0, companyadvanceDOS.size() + 2, "合计:" + String.valueOf(companyMoney) + "\n\n\n\n\n" , format3);
    94. sheet.addCell(label2);
    95. break;
    96. WritableCellFormat processFormat = new WritableCellFormat(font2);
    97. // 追加最后一行
    98. Label processLabel = new Label(0, node, "合计:" + formatNumber(win).toString(), processFormat);
    99. sheet.addCell(processLabel);
    100. break;
    101. }
    102. if (status.equals("7")) {
    103. WritableCellFormat format3 = new WritableCellFormat(font2);
    104. // 追加最后一行
    105. Label label2 = new Label(0, colist.size() + 2, "合计:" + String.valueOf(money), format3);
    106. sheet.addCell(label2);
    107. }
    108. // 关闭资源
    109. wb.write();
    110. wb.close();
    111. out.flush();
    112. out.close();
    113. }

     这种方式代码其实是比较冗余的,这也是比较老的一种方式,当然代码肯定还有优化的程度,封装公共的列名导出。

    2,easypoi-base 方式

    1. package com.volvo.admin.charging.provider.service.impl;
    2. import org.apache.poi.ss.usermodel.BorderStyle;
    3. import org.apache.poi.ss.usermodel.CellStyle;
    4. import org.apache.poi.ss.usermodel.FillPatternType;
    5. import org.apache.poi.ss.usermodel.Font;
    6. import org.apache.poi.ss.usermodel.HorizontalAlignment;
    7. import org.apache.poi.ss.usermodel.IndexedColors;
    8. import org.apache.poi.ss.usermodel.VerticalAlignment;
    9. import org.apache.poi.ss.usermodel.Workbook;
    10. import cn.afterturn.easypoi.excel.export.styler.AbstractExcelExportStyler;
    11. import cn.afterturn.easypoi.excel.export.styler.IExcelExportStyler;
    12. /**
    13. * @Description: 自定义报表导出样式,可以修改表头颜色,高度等
    14. **/
    15. public class ExcelExportMyStylerImpl extends AbstractExcelExportStyler implements IExcelExportStyler {
    16. public ExcelExportMyStylerImpl(Workbook workbook) {
    17. super.createStyles(workbook);
    18. }
    19. @Override
    20. public CellStyle getTitleStyle(short color) {
    21. CellStyle titleStyle = workbook.createCellStyle();
    22. Font font = workbook.createFont();
    23. font.setBold(true);// 加粗
    24. titleStyle.setFont(font);
    25. titleStyle.setAlignment(HorizontalAlignment.CENTER);// 居中
    26. titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
    27. titleStyle.setFillForegroundColor(IndexedColors.AQUA.index);// 设置颜色
    28. titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    29. titleStyle.setBorderRight(BorderStyle.THIN);
    30. titleStyle.setWrapText(true);
    31. return titleStyle;
    32. }
    33. @SuppressWarnings("deprecation")
    34. @Override
    35. public CellStyle stringSeptailStyle(Workbook workbook, boolean isWarp) {
    36. CellStyle style = workbook.createCellStyle();
    37. style.setAlignment(CellStyle.ALIGN_CENTER);
    38. style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    39. style.setDataFormat(STRING_FORMAT);
    40. if (isWarp) {
    41. style.setWrapText(true);
    42. }
    43. return style;
    44. }
    45. @Override
    46. public CellStyle getHeaderStyle(short color) {
    47. CellStyle titleStyle = workbook.createCellStyle();
    48. Font font = workbook.createFont();
    49. font.setBold(true);// 加粗
    50. font.setColor(IndexedColors.RED.index);
    51. font.setFontHeightInPoints((short) 11);
    52. titleStyle.setFont(font);
    53. titleStyle.setAlignment(HorizontalAlignment.CENTER);// 居中
    54. titleStyle.setFillForegroundColor(IndexedColors.WHITE.index);// 设置颜色
    55. titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直居中
    56. titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    57. titleStyle.setBorderRight(BorderStyle.THIN);
    58. titleStyle.setWrapText(true);
    59. return titleStyle;
    60. }
    61. @SuppressWarnings("deprecation")
    62. @Override
    63. public CellStyle stringNoneStyle(Workbook workbook, boolean isWarp) {
    64. CellStyle style = workbook.createCellStyle();
    65. style.setAlignment(CellStyle.ALIGN_CENTER);
    66. style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
    67. style.setDataFormat(STRING_FORMAT);
    68. if (isWarp) {
    69. style.setWrapText(true);
    70. }
    71. return style;
    72. }
    73. }

    导出工具类: 

    1. package com.volvo.admin.charging.provider.utils;
    2. import cn.afterturn.easypoi.excel.ExcelExportUtil;
    3. import cn.afterturn.easypoi.excel.entity.ExportParams;
    4. import com.volvo.admin.charging.provider.service.impl.ExcelExportMyStylerImpl;
    5. import org.apache.poi.ss.usermodel.Workbook;
    6. import javax.servlet.ServletOutputStream;
    7. import javax.servlet.http.HttpServletResponse;
    8. import java.net.URLEncoder;
    9. import java.text.SimpleDateFormat;
    10. import java.util.Collection;
    11. import java.util.Date;
    12. /**
    13. * @Version 1.0
    14. **/
    15. public class MyExcelExportUtil {
    16. /**
    17. * Excel文件导出,导出的文件名默认为:headTitle+当前系统时间
    18. * @param listData 要导出的list数据
    19. * @param pojoClass 定义excel属性信息
    20. * @param headTitle Excel文件头信息
    21. * @param sheetName Excel文件sheet名称
    22. * @param response
    23. */
    24. public static void exportExcel(Collection listData,Class pojoClass, String headTitle, String sheetName, HttpServletResponse response) {
    25. ExportParams params = new ExportParams(headTitle, sheetName);
    26. params.setHeight((short) 8);
    27. params.setStyle(ExcelExportMyStylerImpl.class);
    28. try {
    29. Workbook workbook = ExcelExportUtil.exportExcel(params, pojoClass, listData);
    30. String fileName = headTitle + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
    31. fileName = URLEncoder.encode(fileName, "UTF8");
    32. response.setContentType("application/vnd.ms-excel;chartset=utf-8");
    33. response.setHeader("Content-Disposition", "attachment;filename="+fileName + ".xls");
    34. ServletOutputStream out=response.getOutputStream();
    35. workbook.write(out);
    36. out.flush();
    37. out.close();
    38. } catch (Exception e) {
    39. e.printStackTrace();
    40. }
    41. }
    42. }

    service直接导出:

    MyExcelExportUtil.exportExcel(orderResult.getRecords(),ChargeOrderBO.class,"充电订单","充电订单",response);
    

    3,easyexcel 方式

    pom包:

    1. com.alibaba
    2. easyexcel
    3. 3.1.1

    service方式:

    1. try {
    2. try (ExcelWriter excelWriter = EasyExcelFactory.write(getOutputStream(fileName, response), ChargingPileVO.class).build()) {
    3. WriteSheet writeSheet = EasyExcel.writerSheet("家充桩安装记录").build();
    4. excelWriter.write(result, writeSheet);
    5. }
    6. } catch (Exception e) {
    7. log.info("家充桩安装导出excle数据异常:{}", e.getMessage());
    8. }
    9. /**
    10. * 构建输出流
    11. *
    12. * @param fileName:文件名称
    13. * @param response:
    14. * @return
    15. * @throws Exception
    16. */
    17. private OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
    18. fileName = URLEncoder.encode(fileName, "UTF-8");
    19. response.setContentType("application/vnd.ms-excel");
    20. response.setCharacterEncoding("UTF-8");
    21. response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
    22. return response.getOutputStream();
    23. }

    实体注解:

    1. import com.alibaba.excel.annotation.ExcelProperty;
    2. import com.fasterxml.jackson.annotation.JsonFormat;
    3. import lombok.Data;
    4. import java.util.Date;
    5. @Data
    6. public class ChargingPileVO {
    7. /**
    8. * 订单号
    9. */
    10. @ExcelProperty(value = "订单编号",index = 0)
    11. private String orderCode;
    12. /**
    13. * 联系人
    14. */
    15. @ExcelProperty(value = "联系人",index = 1)
    16. private String userName;
    17. /**
    18. * 联系号码
    19. */
    20. @ExcelProperty(value = "联系电话",index = 2)
    21. private String phone;
    22. /**
    23. * 省份
    24. */
    25. @ExcelProperty(value = "省份",index = 3)
    26. private String province;
    27. /**
    28. * 城市
    29. */
    30. @ExcelProperty(value = "城市",index = 4)
    31. private String city;
    32. /**
    33. * 地区
    34. */
    35. @ExcelProperty(value = "地区",index = 5)
    36. private String district;
    37. /**
    38. * 地区
    39. */
    40. @ExcelProperty(value = "安装详细地址",index = 6)
    41. private String contactAddress;
    42. /**
    43. * 地区
    44. */
    45. @ExcelProperty(value = "派单供应商",index = 7)
    46. private String operatorId;
    47. /**
    48. * 地区
    49. */
    50. @ExcelProperty(value = "订单创建时间",index = 8)
    51. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    52. private Date createTimes;
    53. /**
    54. * 地区
    55. */
    56. @ExcelProperty(value = "安装完成时间",index = 9)
    57. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    58. private Date installEndTime;
    59. /**
    60. * 地区
    61. */
    62. @ExcelProperty(value = "桩类型",index = 10)
    63. private String pileType;
    64. /**
    65. * 地区
    66. */
    67. @ExcelProperty(value = "状态",index = 11)
    68. private String orderStatus;
    69. }

    4,前端导出

    bootstrap导出

      showExport: true,开启 自动可以支持多种格式导出

    showExport: true,
     exportDataType: 'all',
     exportTypes:[ 'csv', 'txt', 'sql', 'doc', 'excel'],  //导出文件类型
     exportOptions:{
         ignoreColumn: [0],
         fileName: '工人借支',
     },
    1. function load() {
    2. $('#exampleTable')
    3. .bootstrapTable(
    4. {
    5. method: 'get', // 服务器数据的请求方式 get or post
    6. url: prefix + "/list", // 服务器数据的加载地址
    7. showRefresh : true,
    8. showToggle : true,
    9. showColumns : true,
    10. iconSize: 'outline',
    11. toolbar: '#exampleToolbar',
    12. striped: true, // 设置为true会有隔行变色效果
    13. dataType: "json", // 服务器返回的数据类型
    14. pagination: true,
    15. // queryParamsType : "limit",
    16. // //设置为limit则会发送符合RESTFull格式的参数
    17. singleSelect: false, // 设置为true将禁止多选
    18. // contentType : "application/x-www-form-urlencoded",
    19. // //发送到服务器的数据编码类型
    20. pageSize: 10, // 如果设置了分页,每页数据条数
    21. pageNumber: 1, // 如果设置了分布,首页页码
    22. // search : true, // 是否显示搜索框
    23. sidePagination: "server", // 设置在哪里进行分页,可选值为"client" 或者
    24. // showFooter: true, //开启底部
    25. showExport: true,
    26. //showFooter: true, //开启底部
    27. /* showExport: true,
    28. exportDataType: 'all',
    29. exportTypes:[ 'csv', 'txt', 'sql', 'doc', 'excel'], //导出文件类型
    30. exportOptions:{
    31. ignoreColumn: [0],
    32. fileName: '工人借支',
    33. },*/
    34. queryParams: function (params) {
    35. return {
    36. // 说明:传入后台的参数包括offset开始索引,limit步长,sort排序列,order:desc或者,以及所有列的键值对
    37. limit: params.limit,
    38. offset: params.offset,
    39. davacename: $('#searchName').val().replace(/(^\s*)|(\s*$)/g, ""),
    40. years: years,
    41. proparentId: proparentId,
    42. };
    43. },
    44. // //请求服务器数据时,你可以通过重写参数的方式添加一些额外的参数,例如 toolbar 中的参数 如果
    45. // queryParamsType = 'limit' ,返回参数必须包含
    46. // limit, offset, search, sort, order 否则, 需要包含:
    47. // pageSize, pageNumber, searchText, sortName,
    48. // sortOrder.
    49. // 返回false将会终止请求
    50. columns: [
    51. {
    52. field: 'id1',
    53. checkbox: true,
    54. align: 'left'/*,
    55. footerFormatter: function (value) {
    56. return "合计";
    57. }*/
    58. },
    59. {
    60. field: 'id',
    61. title: '编号',
    62. align: 'center',
    63. formatter:function(value,row,index){
    64. //return index+1; //序号正序排序从1开始
    65. var pageSize=$('#exampleTable').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
    66. var pageNumber=$('#exampleTable').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
    67. return pageSize * (pageNumber - 1) + index + 1; //返回每条的序号: 每页条数 * (当前页 - 1 )+ 序号
    68. },
    69. },
    70. {
    71. field: 'davacename',
    72. title: '借支名称',
    73. align: 'center',
    74. },
    75. {
    76. field: 'years',
    77. title: '年份',
    78. align: 'center',
    79. },
    80. {
    81. field: 'advacedate',
    82. title: '添加时间',
    83. align: 'center',
    84. },
    85. {
    86. field: 'sumadvance',
    87. title: '借支总金额',
    88. align: 'left',
    89. formatter: function (value, row, index) {
    90. if (row.davaceprice != undefined && row.davaceprice != "") {
    91. return '<span class="label label-success' + '">' + row.davaceprice + ' span>';
    92. }
    93. }/*,
    94. footerFormatter: function (value) {
    95. var count = 0;
    96. for (var i in value) {
    97. if (value[i].davaceprice != null) {
    98. count += value[i].davaceprice;
    99. }
    100. }
    101. return '<span class="label label-danger' + '">' + count.toFixed(2) + ' span>';
    102. }*/
    103. },
    104. {
    105. field: 'worknote',
    106. title: '备注',
    107. align: 'center',
    108. },
    109. {
    110. field: 'typename',
    111. title: '分类',
    112. align: 'center',
    113. formatter: function (value, row, index) {
    114. return '<span class="label label-danger' + '">' + row.typename + ' span>';
    115. }
    116. },
    117. {
    118. field: 'id2',
    119. title: '操作',
    120. align: 'center',
    121. formatter: function (value, row, index) {
    122. var e = '<a class="btn btn-primary btn-sm ' + s_edit_h + '" href="#" mce_href="#" title="编辑" onclick="edit(\''
    123. + row.id
    124. + '\')"><i class="fa fa-edit">i>a> ';
    125. var d = '<a class="btn btn-warning btn-sm ' + s_remove_h + '" href="#" title="删除" mce_href="#" onclick="remove(\''
    126. + row.id
    127. + '\')"><i class="fa fa-remove">i>a> ';
    128. return e + d;
    129. },
    130. }],
    131. onLoadSuccess: function (data) { //加载成功时执行
    132. var sum_1 = 0;
    133. for (var o in data.rows) {
    134. var money1=(data.rows[o].davaceprice==null ||data.rows[o].davaceprice==undefined)? 0:data.rows[o].davaceprice;
    135. sum_1 = parseFloat(sum_1) + parseFloat(money1);
    136. }
    137. //设计我自己的统计html代码,改成gird形式!不怕宽度不够带来麻烦!
    138. var myfooter = "<div id='total'> <div class='col-12 text-center ' >" +
    139. "合计:" + sum_1.toFixed(2)+
    140. "" +
    141. "div>div>";
    142. if (!$("div.fixed-table-footer").text()) //判断是不是给隐藏了,在手机模式下style是style="display: none;"同时text是空
    143. {
    144. $("div.fixed-table-footer").removeAttr("style"); //取消隐藏
    145. $("div.fixed-table-footer").attr("style", "word-break:break-all;height: auto");
    146. }
    147. //把自己的html写到div.fixed-table-footer里面
    148. $("div.fixed-table-footer").html(myfooter);
    149. },
    150. onLoadError: function () { //加载失败时执行
    151. layer.msg("加载数据失败", {time: 1500, icon: 2});
    152. }
    153. });
    154. }

    采用easyexcel 方式导出方式比较简单,直接用,通过注解方式实现导出。

    至于导出特别大的数据量其实程序本身都是有性能瓶颈的,因为几百万的数据直接内存溢出了,所以对于大数据量的导出,可以采用异步下载,异步上传,通过下载中心去导出是最好的。

    一件事不管做的怎么样,好与坏至少你都在做,把事情做到精细化那么你就是专家。 

  • 相关阅读:
    性价比高一点的蓝牙耳机有哪几款?高性价比蓝牙耳机推荐
    WinServer 2019 AD 组策略 禁止U盘访问
    Kafka Eagle 3.0.1功能预览
    ZCMU--5129: 校园寻宝(C语言)
    通过nodeJS如何实现爬虫功能
    Python-公共操作与推导式
    python基于协同过滤推荐算法的电影观后感推荐管理系统的设计
    Spring-AOP
    数据库技术基础--数据模型
    以太坊知识攻略:新手必看的25个专业术语
  • 原文地址:https://blog.csdn.net/qq_39751120/article/details/126773033