最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot  Excel文件導(dǎo)出下載實(shí)現(xiàn)代碼

 更新時(shí)間:2018年11月20日 11:56:32   作者:從今天起做個(gè)幸福的人  
這篇文章帶領(lǐng)我們直接實(shí)現(xiàn)Excel文件的直接導(dǎo)出下載,后續(xù)開發(fā)不需要開發(fā)很多代碼,直接繼承已經(jīng)寫好的代碼,增加一個(gè)Xml配置就可以直接導(dǎo)出。具體實(shí)現(xiàn)代碼大家跟隨小編一起通過本文學(xué)習(xí)吧

Spring Boot Excel 文件導(dǎo)出

目標(biāo):

實(shí)現(xiàn)Excel文件的直接導(dǎo)出下載,后續(xù)開發(fā)不需要開發(fā)很多代碼,直接繼承已經(jīng)寫好的代碼,增加一個(gè)Xml配置就可以直接導(dǎo)出。

實(shí)現(xiàn):

1、抽象類 BaseExcelView 繼承 webmvc 的  AbstractXlsxStreamingView 抽象類, AbstractXlsxStreamingView 是webmvc繼承了最頂層View接口,是可以直接大量數(shù)據(jù)導(dǎo)出的不會造成內(nèi)存泄漏問題,即 SXSSFWorkbook 解決了內(nèi)存問題, 導(dǎo)出只支持xlsx類型文件。

抽象類代碼 BaseExcelView :

public abstract class BaseExcelView extends AbstractXlsxStreamingView {
  private static final Logger logger = LoggerFactory.getLogger(BaseExcelView.class);
  /**
   * 獲取導(dǎo)出文件名
   *
   * @return
   */
  abstract protected String getFileName();
  /**
   * 獲取表單名稱
   *
   * @return
   */
  abstract protected String getSheetName();
  /**
   * 獲取標(biāo)題欄名稱
   *
   * @return
   */
  abstract protected String[] getTitles();
  /**
   * 獲取列寬
   *
   * @return
   */
  abstract protected short[] getColumnWidths();
  /**
   * 構(gòu)造內(nèi)容單元格
   *
   * @param sheet
   */
  abstract protected void buildContentCells(Sheet sheet);
  @Override
  protected void buildExcelDocument(
      Map<String, Object> model, Workbook workbook, HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    // 構(gòu)造標(biāo)題單元格 SXSSFWorkbook
    Sheet sheet = buildTitleCells(workbook);
    // 構(gòu)造內(nèi)容單元格
    buildContentCells(sheet);
    // 設(shè)置響應(yīng)頭
    setResponseHead(request, response);
  }
  /**
   * 設(shè)置響應(yīng)頭
   *
   * @param response
   * @throws IOException
   */
  protected void setResponseHead(HttpServletRequest request,
                  HttpServletResponse response) throws IOException {
    // 文件名
    String fileName = getFileName();
    String userAgent = request.getHeader("user-agent").toLowerCase();
    logger.info("客戶端請求頭內(nèi)容:");
    logger.info("user-agent\t值: {}", userAgent);
    if (userAgent != null) {
      if (userAgent.contains("firefox")) {
        // firefox有默認(rèn)的備用字符集是西歐字符集
        fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
      } else if (userAgent.contains("webkit") && (userAgent.contains("chrome") || userAgent.contains("safari"))) {
        // webkit核心的瀏覽器,主流的有chrome,safari,360
        fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");
      } else {
        // 新老版本的IE都可直接用URL編碼工具編碼后輸出正確的名稱,無亂碼
        fileName = URLEncoder.encode(fileName, "UTF-8");
      }
    }
    //響應(yīng)頭信息
    response.setCharacterEncoding("UTF-8");
    response.setContentType("application/ms-excel; charset=UTF-8");
    response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");
  }
  /**
   * 構(gòu)造標(biāo)題單元格
   *
   * @param
   * @return
   */
  protected Sheet buildTitleCells(Workbook workbook) {
    // 表單名稱
    String sheetName = getSheetName();
    // 標(biāo)題名稱
    String[] titles = getTitles();
    // 列寬
    short[] colWidths = getColumnWidths();
    // 創(chuàng)建表格
    Sheet sheet = workbook.createSheet(sheetName);
    // 標(biāo)題單元格樣式
    CellStyle titleStyle = getHeadStyle(workbook);
    // 默認(rèn)內(nèi)容單元格樣式
    CellStyle contentStyle = getBodyStyle(workbook);
    // 標(biāo)題行
    Row titleRow = sheet.createRow(0);
    // 創(chuàng)建標(biāo)題行單元格
    for (int i = 0; i < titles.length; i++) {
      // 標(biāo)題單元格
      Cell cell = titleRow.createCell((short) i);
      cell.setCellType(CellType.STRING);
      cell.setCellValue(new XSSFRichTextString(titles[i]));
      cell.setCellStyle(titleStyle);
      // 設(shè)置列寬
      sheet.setColumnWidth((short) i, (short) (colWidths[i] * 256));
      // 設(shè)置列默認(rèn)樣式
      sheet.setDefaultColumnStyle((short) i, contentStyle);
    }
    return sheet;
  }
  /**
   * 設(shè)置表頭的單元格樣式
   */
  public CellStyle getHeadStyle(Workbook workbook) {
    // 創(chuàng)建單元格樣式
    CellStyle cellStyle = workbook.createCellStyle();
    // 設(shè)置單元格的背景顏色為淡藍(lán)色
    cellStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.index);
    // 設(shè)置填充字體的樣式
    cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    // 設(shè)置單元格居中對齊
    cellStyle.setAlignment(HorizontalAlignment.CENTER);
    // 設(shè)置單元格垂直居中對齊
    cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    // 創(chuàng)建單元格內(nèi)容顯示不下時(shí)自動換行
    cellStyle.setWrapText(true);
    // 設(shè)置單元格字體樣式
    Font font = workbook.createFont();
    // 字號
    font.setFontHeightInPoints((short) 12);
    // 加粗
    font.setBold(true);
    // 將字體填充到表格中去
    cellStyle.setFont(font);
    // 設(shè)置單元格邊框?yàn)榧?xì)線條(上下左右)
    cellStyle.setBorderLeft(BorderStyle.THIN);
    cellStyle.setBorderBottom(BorderStyle.THIN);
    cellStyle.setBorderRight(BorderStyle.THIN);
    cellStyle.setBorderTop(BorderStyle.THIN);
    return cellStyle;
  }
  /**
   * 設(shè)置表體的單元格樣式
   */
  public CellStyle getBodyStyle(Workbook workbook) {
    // 創(chuàng)建單元格樣式
    CellStyle cellStyle = workbook.createCellStyle();
    // 設(shè)置單元格居中對齊
    cellStyle.setAlignment(HorizontalAlignment.CENTER);
    // 設(shè)置單元格居中對齊
    cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
    // 創(chuàng)建單元格內(nèi)容不顯示自動換行
    cellStyle.setWrapText(true);
    //設(shè)置單元格字體樣式字體
    Font font = workbook.createFont();
    // 字號
    font.setFontHeightInPoints((short) 10);
    // 將字體添加到表格中去
    cellStyle.setFont(font);
    // 設(shè)置單元格邊框?yàn)榧?xì)線條
    cellStyle.setBorderLeft(BorderStyle.THIN);
    cellStyle.setBorderBottom(BorderStyle.THIN);
    cellStyle.setBorderRight(BorderStyle.THIN);
    cellStyle.setBorderTop(BorderStyle.THIN);
    return cellStyle;
  }
}

Excel導(dǎo)出實(shí)現(xiàn) 1: 可以直接繼承 BaseExcelView  實(shí)現(xiàn)定義的方法 eg:

public class CheckExcelView extends BaseExcelView {
  private List<T> vo;
  public CheckExcelView(List<T> vo) {
   this.vo= vo;
  }
  @Override
  protected String getFileName() {
   String time = DateUtils.getLocalFullDateTime14();
   return "導(dǎo)出文件" + time;
  }
  @Override
  protected String getSheetName() {
    return "報(bào)表";
  }
  @Override
  protected String[] getTitles() {
   return new String[] { "申請時(shí)間"};
  }
  @Override
  protected short[] getColumnWidths() {
   return new short[] { 20};
  }
  @Override
  protected void buildContentCells(Sheet sheet) {
   DecimalFormat df = new DecimalFormat("0.00");
   int rowNum = 1;
   for (T o : vO) {
     Row crow = sheet.createRow(rowNum++);
     crow.createCell(0).setCellValue(o.getApplicationDate()));
   }
  }
}

導(dǎo)出實(shí)現(xiàn) 2: XML配置導(dǎo)出 

1、需要定義XML的配置 export-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <table id="demo" name="測試">
    <columns>
      <column id="name" name="名稱" width="40"></column>
    </columns>
  </table>
</configuration>

2、XMl解析配置   

@Root
public class Export {
  @ElementList(entry = "table", inline = true)
  private List<Table> table;
  public List<Table> getTable() {
    return table;
  }
  public void setTable(List<Table> table) {
    this.table = table;
  }
  public static class Table {
    @Attribute
    private String id;
    @Attribute
    private String name;
    @ElementList(entry = "column")
    private List<Column> columns;
    public String getId() {
      return id;
    }
    public void setId(String id) {
      this.id = id;
    }
    public String getName() {
      return name;
    }
    public void setName(String name) {
      this.name = name;
    }
    public List<Column> getColumns() {
      return columns;
    }
    public void setColumns(List<Column> columns) {
      this.columns = columns;
    }
  }
  public static class Column {
    @Attribute
    private String id;
    @Attribute
    private String name;
    @Attribute
    private short width;
    @Attribute(required = false)
    private String mapping;
    public String getId() {
      return id;
    }
    public void setId(String id) {
      this.id = id;
    }
    public String getName() {
      return name;
    }
    public void setName(String name) {
      this.name = name;
    }
    public String getMapping() {
      return mapping;
    }
    public void setMapping(String mapping) {
      this.mapping = mapping;
    }
    public short getWidth() {
      return width;
    }
    public void setWidth(short width) {
      this.width = width;
    }
  }
}

3、解析XMl方法配置

@Service
public class IExportService {
  private Export tables;
  private Map<String, Export.Table> tableMap;
  @SuppressWarnings("rawtypes")
  @PostConstruct
  public void init() throws Exception {
    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("export-config.xml");
    Serializer serializer = new Persister();
    tables = serializer.read(Export.class, inputStream);
    tableMap = new HashMap<>();
    for (Export.Table table : tables.getTable()) {
      tableMap.put(table.getId(), table);
    }
  }
  public Export.Table getTable(String key) {
    return tableMap.get(key);
  }
}

4、導(dǎo)出基礎(chǔ)  ExcelExportView 代碼實(shí)現(xiàn)

public class ExcelExportView extends BaseExcelView {
  private String[] titles;
  private short[] columnWidths;
  List<Map<String, Object>> results;
  private Export.Table table;
  private IExportService iExportService;
  @Override
  protected String getFileName() {
    return table.getName();
  }
  @Override
  protected String getSheetName() {
    return table.getName();
  }
  @Override
  protected String[] getTitles() {
    return this.titles;
  }
  @Override
  protected short[] getColumnWidths() {
    return this.columnWidths;
  }
  public ExcelExportView() {
    this.iExportService = ApplicationContextProvider.getBean(IExportService.class);
  }
  @Override
  protected void buildContentCells(Sheet sheet) {
    int dataIndex = 1;
    if(CollectionUtils.isEmpty(results)){
      return;
    }
    for (Map<String, Object> data : results) {
      Row row = sheet.createRow(dataIndex++);
      for (int i = 0; i < table.getColumns().size(); i++) {
        Export.Column column = table.getColumns().get(i);
        Cell cell = row.createCell(i);
        Object value = data.get(column.getId());
        if (value == null) {
          value = "";
        }
        cell.setCellValue(new XSSFRichTextString(value.toString()));
      }
    }
  }
  public void exportExcel(String key, List<Map<String, Object>> results) {
    this.table = iExportService.getTable(key);
    if (null == table) {
      return;
    }
    this.results = results;
    this.titles = new String[table.getColumns().size()];
    this.columnWidths = new short[table.getColumns().size()];
    for (int i = 0; i < table.getColumns().size(); i++) {
      Export.Column column = table.getColumns().get(i);
      titles[i] = column.getName();
      columnWidths[i] = column.getWidth();
    }
  }
}

最后:導(dǎo)出Controller代碼實(shí)現(xiàn) 

@RequestMapping(path = "/export", method = RequestMethod.GET, produces = "application/octet-stream;charset=UTF-8")
public @ResponseBody
ModelAndView export(){
  Long loginComId = loginContext.getCompany().getId();
  List<T> list = new ArrayList<>();
  ExcelExportView exportView = new ExcelExportView();
  exportView.exportExcel("XMl中表的ID", BeanUtils.objectToMapList(list));
  return new ModelAndView(exportView);
<em id="__mceDel"><em id="__mceDel">}</em></em>

總結(jié)

以上所述是小編給大家介紹的Spring Boot  Excel文件導(dǎo)出下載實(shí)現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • springcloud整合gateway實(shí)現(xiàn)網(wǎng)關(guān)全局過濾器功能

    springcloud整合gateway實(shí)現(xiàn)網(wǎng)關(guān)全局過濾器功能

    本文主要介紹了springcloud整合gateway實(shí)現(xiàn)網(wǎng)關(guān)全局過濾器功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • SpringBoot生成和操作PDF的代碼詳解

    SpringBoot生成和操作PDF的代碼詳解

    本文主要介紹了在SpringBoot項(xiàng)目下,通過代碼和操作步驟,詳細(xì)的介紹了如何操作PDF,希望可以幫助到準(zhǔn)備通過JAVA操作PDF的你,項(xiàng)目框架用的SpringBoot,但在JAVA中代碼都是通用的,需要的朋友可以參考下
    2025-01-01
  • Java中Collections.sort()排序方法舉例詳解

    Java中Collections.sort()排序方法舉例詳解

    很多時(shí)候都需要對一些數(shù)據(jù)進(jìn)行排序的操作,這篇文章主要給大家介紹了關(guān)于Java中Collections.sort()方法舉例詳解的相關(guān)資料,使用Collections.sort()可以使用其sort()方法來對List、Set等集合進(jìn)行排序,需要的朋友可以參考下
    2024-02-02
  • 為什么阿里要慎重使用ArrayList中的subList方法

    為什么阿里要慎重使用ArrayList中的subList方法

    這篇文章主要介紹了為什么要慎重使用ArrayList中的subList方法,subList是List接口中定義的一個(gè)方法,該方法主要用于返回一個(gè)集合中的一段、可以理解為截取一個(gè)集合中的部分元素,他的返回值也是一個(gè)List。,需要的朋友可以參考下
    2019-06-06
  • Java線程之程安全與不安全代碼示例

    Java線程之程安全與不安全代碼示例

    這篇文章主要介紹了Java線程之程安全與不安全代碼示例,還是比較不錯(cuò)的,這里分享給大家,供需要的朋友參考。
    2017-11-11
  • mybatis plus代碼生成工具的實(shí)現(xiàn)代碼

    mybatis plus代碼生成工具的實(shí)現(xiàn)代碼

    這篇文章主要介紹了mybatis plus代碼生成工具的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2021-04-04
  • SpringBoot自定義Starter與自動配置實(shí)現(xiàn)方法詳解

    SpringBoot自定義Starter與自動配置實(shí)現(xiàn)方法詳解

    在Spring Boot官網(wǎng)為了簡化我們的開發(fā),已經(jīng)提供了非常多場景的Starter來為我們使用,即便如此,也無法全面的滿足我們實(shí)際工作中的開發(fā)場景,這時(shí)我們就需要自定義實(shí)現(xiàn)定制化的Starter
    2023-02-02
  • Java實(shí)現(xiàn)數(shù)據(jù)脫敏的方法詳細(xì)講解

    Java實(shí)現(xiàn)數(shù)據(jù)脫敏的方法詳細(xì)講解

    這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)數(shù)據(jù)脫敏的相關(guān)資料,數(shù)據(jù)脫敏是指對某些敏感信息通過脫敏規(guī)則進(jìn)行數(shù)據(jù)的變形,實(shí)現(xiàn)敏感隱私數(shù)據(jù)的可靠保護(hù),需要的朋友可以參考下
    2023-06-06
  • JNDI在JavaEE中的角色_動力節(jié)點(diǎn)Java學(xué)院整理

    JNDI在JavaEE中的角色_動力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要介紹了JNDI在JavaEE中的角色,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • 如何解決Java多線程死鎖問題

    如何解決Java多線程死鎖問題

    死鎖是一個(gè)很嚴(yán)重的、必須要引起重視的問題,本文主要介紹了死鎖的定義,解決方法和面試會遇到的問題,感興趣的可以了解一下
    2021-05-05

最新評論

从化市| 高阳县| 灌阳县| 伽师县| 缙云县| 广安市| 铜梁县| 德昌县| 大埔县| 玉环县| 溧水县| 竹溪县| 南陵县| 石渠县| 嘉峪关市| 奈曼旗| 九江县| 宿迁市| 泸水县| 宁都县| 娄底市| 永顺县| 昌黎县| 玉环县| 舒兰市| 措美县| 永安市| 丹阳市| 长治市| 辽宁省| 茌平县| 朔州市| 岱山县| 泰和县| 财经| 绥芬河市| 宜良县| 惠东县| 兴义市| 台南市| 徐汇区|