欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot實(shí)現(xiàn)excel文件生成和下載

 更新時(shí)間:2021年02月09日 14:05:28   作者:shengshenglalala  
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)excel文件生成和下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

使用SpringBoot實(shí)現(xiàn)excel生成和下載,生成模板如下

controller

@RequestMapping(value = { "/downloadExcelTemplate" }, method = RequestMethod.GET)
 public String downloadExcelTemplate(HttpSession httpSession, HttpServletResponse response) {
 try {
  dealExcelService.downloadExcelTemplate(response);
  return "success";
 } catch (Exception e) {
  logger.error("downloadExcelTemplate_error", e);
  return "failure";
 }
}

service

public void downloadExcelTemplate(HttpServletResponse response) throws Exception {
 //文件名
 SimpleDateFormat format3 = new SimpleDateFormat("yyyyMMddHHmm");
 String fileName = new String(("文件名" + format3.format(new Date()) + "導(dǎo)入模板").getBytes(), "ISO8859_1");
 //配置請求頭
 ServletOutputStream outputStream = response.getOutputStream();
 // 組裝附件名稱和格式
 response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");
 // 創(chuàng)建一個(gè)workbook 對應(yīng)一個(gè)excel應(yīng)用文件
 XSSFWorkbook workBook = new XSSFWorkbook();
 // 在workbook中添加一個(gè)sheet,對應(yīng)Excel文件中的sheet
 XSSFSheet sheet = workBook.createSheet("模板");
 ExportUtil exportUtil = new ExportUtil(workBook, sheet);
 XSSFCellStyle headStyle = exportUtil.getHeadStyle();
 XSSFCellStyle bodyStyle = exportUtil.getBodyStyle2();
 // 構(gòu)建表頭
 XSSFRow headRow = ExportUtil.createRow(sheet, 0);
 XSSFCell cell;
 
 String[] titles = {"表頭一", "表頭二", "表頭三"};
 int index = 0;
 for (String title : titles) {
  cell = ExportUtil.createCell(headRow, index);
  cell.setCellStyle(headStyle);
  cell.setCellValue(title);
  index++;
 }
 
 try {
  workBook.write(outputStream);
  outputStream.flush();
  outputStream.close();
 } catch (IOException e) {
  e.printStackTrace();
 } finally {
  try {
  outputStream.close();
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
}

ExportUtil導(dǎo)出工具類

package com.shengsheng.utils;
 
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
 
/**
 * excel 表格導(dǎo)出工具類
 *
 * @author shengshenglalala
 */
public class ExportUtil {
 private XSSFWorkbook wb;
 
 private XSSFSheet sheet;
 
 /**
 * @param wb
 * @param sheet
 */
 public ExportUtil(XSSFWorkbook wb, XSSFSheet sheet) {
 this.wb = wb;
 this.sheet = sheet;
 }
 
 /**
 * 合并單元格后給合并后的單元格加邊框
 *
 * @param region
 * @param cs
 */
 public void setRegionStyle(CellRangeAddress region, XSSFCellStyle cs) {
 
 int toprowNum = region.getFirstRow();
 for (int i = toprowNum; i <= region.getLastRow(); i++) {
  XSSFRow row = sheet.getRow(i);
  for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) {
  XSSFCell cell = row.getCell(j);
  cell.setCellStyle(cs);
  }
 }
 }
 
 /**
 * 設(shè)置表頭的單元格樣式
 *
 * @return
 */
 public XSSFCellStyle getHeadStyle() {
 // 創(chuàng)建單元格樣式
 XSSFCellStyle cellStyle = wb.createCellStyle();
 // // 設(shè)置單元格的背景顏色為淡藍(lán)色
 cellStyle.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
 cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
 // 設(shè)置單元格居中對齊
 cellStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
 // 設(shè)置單元格垂直居中對齊
 cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
 // 創(chuàng)建單元格內(nèi)容顯示不下時(shí)自動換行
 // cellStyle.setWrapText(true);
 // 設(shè)置單元格字體樣式
 XSSFFont font = wb.createFont();
 // 設(shè)置字體加粗
 font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
 font.setFontName("宋體");
 // font.setFontHeight((short) 200);
 cellStyle.setFont(font);
 // 設(shè)置單元格邊框?yàn)榧?xì)線條
// cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
// cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
// cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
// cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
 return cellStyle;
 }
 
 /**
 * 設(shè)置表體的單元格樣式
 *
 * @return
 */
 public XSSFCellStyle getBodyStyle2() {
 // 創(chuàng)建單元格樣式
 // 創(chuàng)建單元格樣式
 XSSFCellStyle cellStyle = wb.createCellStyle();
 // 創(chuàng)建單元格內(nèi)容顯示不下時(shí)自動換行
 // cellStyle.setWrapText(true);
 // 設(shè)置單元格字體樣式
 XSSFFont font = wb.createFont();
 // 設(shè)置字體加粗
 // font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
 font.setFontName("宋體");
 font.setFontHeight((short) 200);
 font.setColor(HSSFColor.BLACK.index);
 cellStyle.setFont(font);
 // 設(shè)置單元格邊框?yàn)榧?xì)線條
 return cellStyle;
 }
 
 /**
 * 沒有行,就創(chuàng)建行
 *
 * @param sheet
 * @param index
 * @return
 */
 public static XSSFRow createRow(XSSFSheet sheet, Integer index) {
 XSSFRow row = sheet.getRow(index);
 if (row == null) {
  return sheet.createRow(index);
 }
 return row;
 }
 
 /**
 * 如果沒有列,就創(chuàng)建列
 *
 * @param row
 * @param index
 * @return
 */
 public static XSSFCell createCell(XSSFRow row, Integer index) {
 XSSFCell cell = row.getCell(index);
 if (cell == null) {
  return row.createCell(index);
 }
 return cell;
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot項(xiàng)目整合攔截器詳解

    SpringBoot項(xiàng)目整合攔截器詳解

    這篇文章主要介紹了SpringBoot項(xiàng)目整合攔截器詳解,java里的攔截器是動態(tài)攔截Action調(diào)用的對象,它提供了一種機(jī)制可以使開發(fā)者在一個(gè)Action執(zhí)行的前后執(zhí)行一段代碼,攔截器用于在某個(gè)方法或者字段被訪問之前進(jìn)行攔截,然后再之前或者之后加入某些操作,需要的朋友可以參考下
    2023-10-10
  • Java設(shè)計(jì)模式之java外觀模式詳解

    Java設(shè)計(jì)模式之java外觀模式詳解

    這篇文章主要介紹了Java設(shè)計(jì)模式之外觀模式(Facade模式)介紹,外觀模式(Facade)的定義:為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,需要的朋友可以參考下
    2021-09-09
  • tdesign的文件上傳功能實(shí)現(xiàn)(微信小程序+idea的springboot)

    tdesign的文件上傳功能實(shí)現(xiàn)(微信小程序+idea的springboot)

    這篇文章主要介紹了tdesign的文件上傳(微信小程序+idea的springboot)的相關(guān)知識,本文通過圖文實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-09-09
  • Java中double數(shù)值保留兩位小數(shù)的4種實(shí)現(xiàn)方式舉例

    Java中double數(shù)值保留兩位小數(shù)的4種實(shí)現(xiàn)方式舉例

    在Java編程中,我們經(jīng)常遇到需要對double類型的浮點(diǎn)數(shù)進(jìn)行精確截?cái)嗷蛩纳嵛迦氡A魞晌恍?shù)的需求,這篇文章主要給大家介紹了關(guān)于Java中double數(shù)值保留兩位小數(shù)的4種實(shí)現(xiàn)方式,需要的朋友可以參考下
    2024-07-07
  • 基于java web獲取網(wǎng)頁訪問次數(shù)代碼實(shí)例

    基于java web獲取網(wǎng)頁訪問次數(shù)代碼實(shí)例

    這篇文章主要介紹了基于java web獲取網(wǎng)頁訪問次數(shù)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Java多線程按指定順序同步執(zhí)行

    Java多線程按指定順序同步執(zhí)行

    這篇文章主要介紹了java多線程如何按指定順序同步執(zhí)行,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • 一文帶你了解如何正確使用MyBatisPlus

    一文帶你了解如何正確使用MyBatisPlus

    在本篇文章中,我們獎通過?MyBatis?Plus?來對一張表進(jìn)行?CRUD?操作,來看看是如何簡化我們開發(fā)的。文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2022-12-12
  • 詳解JAVA設(shè)計(jì)模式之適配器模式

    詳解JAVA設(shè)計(jì)模式之適配器模式

    這篇文章主要介紹了JAVA設(shè)計(jì)模式之適配器模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解
    2020-06-06
  • SpringBoot整合Retry的詳細(xì)指南

    SpringBoot整合Retry的詳細(xì)指南

    在現(xiàn)代的分布式系統(tǒng)中,服務(wù)間的調(diào)用往往需要處理各種網(wǎng)絡(luò)異常、超時(shí)等問題,重試機(jī)制是一種常見的解決策略,本文將通過一個(gè)具體的使用場景來詳細(xì)介紹如何在 Spring Boot 應(yīng)用中集成和使用 Spring Retry 技術(shù),需要的朋友可以參考下
    2024-12-12
  • spring boot輸入數(shù)據(jù)校驗(yàn)(validation)的實(shí)現(xiàn)過程

    spring boot輸入數(shù)據(jù)校驗(yàn)(validation)的實(shí)現(xiàn)過程

    web項(xiàng)目中,用戶的輸入總是被假定不安全不正確的,在被處理前需要做校驗(yàn)。本文介紹在spring boot項(xiàng)目中實(shí)現(xiàn)數(shù)據(jù)校驗(yàn)的過程,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2021-09-09

最新評論