easyexcel 設置標題_使用easyexcel完成復雜表頭及標題的導出功能(自定義樣式)

如需客戶端指定excel版本,只需要判斷后綴名然后在controller中的.excelType(ExcelTypeEnum.XLS)做指定輸出內容格式即可

***(注意表格行高列寬統一設置是在實體類的類名注解上,如果需要對表格進行精細的寬高設置需要刪除掉這兩個注解,可以在攔截器使用row的方法進行設置)

1. ## 引入依賴

com.alibaba

easyexcel

2.1.4

2.實體類(注解法)

package com.jpxx.admin.pilebody.service.api.dto;

import com.alibaba.excel.annotation.ExcelIgnore;

import com.alibaba.excel.annotation.ExcelProperty;

import com.alibaba.excel.annotation.write.style.ColumnWidth;

import com.alibaba.excel.annotation.write.style.ContentRowHeight;

import com.alibaba.excel.annotation.write.style.HeadRowHeight;

import com.alibaba.excel.util.StringUtils;

import lombok.Data;

import lombok.NoArgsConstructor;

import lombok.experimental.Accessors;

@Data

@NoArgsConstructor

@Accessors(chain = true)

@ContentRowHeight(45)

@HeadRowHeight(50)

public class PilebodycheckMonthDto {

@ExcelIgnore

private String id;

@ExcelIgnore

private String cityid;

@ExcelIgnore

private String districtid;

@ExcelProperty(value = {"序號","序號"},index = 0)

@ColumnWidth(10)

private String orderNum;

@ExcelProperty(value = {"堆體名稱","堆體名稱"},index = 1)

@ColumnWidth(15)

private String name;

@ExcelProperty(value = {"具體位置","具體位置"},index = 3)

@ColumnWidth(30)

private String address;

@ExcelProperty(value = {"占地面積(平方)","占地面積(平方)"},index = 4)

@ColumnWidth(15)

private String areastr;

@ExcelProperty(value = {"堆體高度(米)","堆體高度(米)"},index = 5)

@ColumnWidth(10)

private String heightstr;

@ExcelProperty(value = {"建筑垃圾堆存量(萬方)","建筑垃圾堆存量(萬方)"},index = 6)

@ColumnWidth(15)

private String stocknum;

@ExcelIgnore

@Dict(dicCode = "governway")

private String governway;

@ExcelProperty(value = {"治理方式","治理方式"},index = 7)

@ColumnWidth(20)

private String governwayname;

@ExcelProperty(value = {"如需外運,計劃外運時間","如需外運,計劃外運時間"},index = 8)

@ColumnWidth(15)

private String outwardtransporttime;

@ExcelProperty(value = {"截止目前累計治理量(萬方)","截止目前累計治理量(萬方)"},index = 13)

@ColumnWidth(15)

private String governnum;

@ExcelProperty(value = {"治理主體","治理主體"},index = 14)

@ColumnWidth(15)

private String governbody;

@ExcelIgnore

@Dict(dicCode = "typestr")

private String typestr;

@ExcelProperty(value = {"堆體類型","堆體類型"},index = 2)

@ColumnWidth(15)

private String typestrname;

@ExcelIgnore

@Dict(dicCode = "statestr")

private String statestr;

@ExcelIgnore

private String districtname;

@ExcelProperty(value = {"監管單位","監管單位"},index = 15)

@ColumnWidth(15)

private String supervisedepartname;

@ExcelProperty(value = {"監管責任人","監管責任人"},index = 16)

@ColumnWidth(10)

private String supervisepeoname;

@ExcelProperty(value = {"職務","職務"},index = 17)

@ColumnWidth(10)

private String supervisepeoposition;

@ExcelProperty(value = {"聯系方式","聯系方式"},index = 18)

@ColumnWidth(20)

private String supervisepeophone;

@ExcelIgnore

private String residuenum;

@ExcelIgnore

private String governendtime;

@ExcelIgnore

private String governendyearmonth;

@ExcelProperty(value = {"本月治理量(萬方)","外運量"},index = 9)

@ColumnWidth(15)

private String outwardtransportnum;

@ExcelProperty(value = {"本月治理量(萬方)","整理地形綠化量"},index = 10)

@ColumnWidth(15)

private String afforestnum;

@ExcelProperty(value = {"本月治理量(萬方)","臨時覆蓋或綠化量"},index = 11)

@ColumnWidth(15)

private String temporarilynum ;

@ExcelProperty(value = {"本月治理量(萬方)","合計"},index = 12)

private String goverytotal;

@ExcelIgnore

private String qynum;

@ExcelIgnore

@Dict(dicCode = "sourcestr")

private String sourcestr;

@ExcelIgnore

private String createbyname;

}

controller

@postMapping(“pilebodystatisticsmonthexport”)

public WebApiResponse pilebodystatisticsmonthexport (HttpServletResponse response,String month) throws IOException {

List pilebodysList = pilebodycheckService.pilebodystatisticsmonth(sysDepartDto, month);

//設置序號

for (int i = 1;i <= pilebodysList.size();i++){

pilebodysList.get(i-1).setOrderNum(i+"");

}

response.setContentType(“application/vnd.ms-excel”);

response.setCharacterEncoding(“utf-8”);

// 這里URLEncoder.encode可以防止中文亂碼 當然和easyexcel沒有關系

String fileName = URLEncoder.encode(“存量建筑垃圾堆體治理進度月報表”, “UTF-8”);

response.setHeader(“Content-disposition”, “attachment;filename=” + fileName + “.xls”);

//內容樣式策略

WriteCellStyle contentWriteCellStyle = new WriteCellStyle();

//垂直居中,水平居中

contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);

contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);

contentWriteCellStyle.setBorderTop(BorderStyle.THIN);

contentWriteCellStyle.setBorderRight(BorderStyle.THIN);

contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);

//設置 自動換行

contentWriteCellStyle.setWrapped(true);

// 字體策略

WriteFont contentWriteFont = new WriteFont();

// 字體大小

contentWriteFont.setFontHeightInPoints((short) 12);

contentWriteCellStyle.setWriteFont(contentWriteFont);

//頭策略使用默認

WriteCellStyle headWriteCellStyle = new WriteCellStyle();

//excel如需下載到本地,只需要將response.getOutputStream()換成File即可(注釋掉以上response代碼)

EasyExcel.write(response.getOutputStream(), PilebodycheckMonthDto.class)

//設置輸出excel版本,不設置默認為xlsx

.excelType(ExcelTypeEnum.XLS).head(PilebodycheckMonthDto.class)

//設置攔截器或自定義樣式

.registerWriteHandler(new MonthSheetWriteHandler())

.registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle,contentWriteCellStyle))

.sheet("存量建筑垃圾堆體治理進度月報表")

//設置默認樣式及寫入頭信息開始的行數

.useDefaultStyle(true).relativeHeadRowIndex(3)

//這里的addsumColomn方法是個添加合計的方法,可刪除

.doWrite(pilebodycheckService.addSumColomn(pilebodysList));

return new WebApiResponse(200, "生成excel文件成功", null);

}

4. 攔截器

package com.jpxx.admin.pilebody.web.api;

import com.alibaba.excel.write.handler.SheetWriteHandler;

import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;

import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.ss.util.CellRangeAddress;

public class MonthSheetWriteHandler implements SheetWriteHandler {

@Override

public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

}

@Override

public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

Workbook workbook = writeWorkbookHolder.getWorkbook();

Sheet sheet = workbook.getSheetAt(0);

Row row1 = sheet.createRow(0);

row1.setHeight((short) 500);

Cell cell = row1.createCell(0);

//設置單元格內容

cell.setCellValue("附件2");

//設置標題

Row row2 = sheet.createRow(1);

row2.setHeight((short) 800);

Cell cell1 = row2.createCell(0);

cell1.setCellValue("存量建筑垃圾堆體治理進度月報表");

CellStyle cellStyle = workbook.createCellStyle();

cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

cellStyle.setAlignment(HorizontalAlignment.CENTER);

Font font = workbook.createFont();

font.setBold(true);

font.setFontHeight((short) 400);

cellStyle.setFont(font);

cell1.setCellStyle(cellStyle);

sheet.addMergedRegionUnsafe(new CellRangeAddress(1, 1, 0, 17));

//設置填表日期,填報人,聯系方式

Row row3 = sheet.createRow(2);

row3.setHeight((short) 500);

row3.createCell(1).setCellValue("填表日期");

row3.createCell(11).setCellValue("填表人");

row3.createCell(15).setCellValue("聯系方式");

}

}

生成表格如下:

d8d653736426799ff326f91d585c3912.png

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/540066.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/540066.shtml
英文地址,請注明出處:http://en.pswp.cn/news/540066.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mysql distinct兩列_正在檢索兩列,并對MySQL中的每列應用“distinct”

這是一張桌子books----------------------------| author_fname | author_lname |----------------------------| Dan | Harris || Freida | Harris || George | Saunders |----------------------------我知道如果DISTINCT用作SELECT DISTINCT author_fname, author_lname FRO…

mysql笛卡爾積 去重_MySQL入門(函數、條件、連接)

MySQL入門(四)distinct&#xff1a;去重mysql>:create table t1(id int,x int,y int);mysql>: insert into t1 values(1, 1, 1), (2, 1, 2), (3, 2, 2), (4, 2, 2);mysql>: select distinct * from t1; # 全部數據mysql>: select distinct x, y from t1; # 結果 1,…

nmon安裝為什么重啟mysql_Nmon的安裝及使用

一、下載Nmon根據CPU的類型選擇下載相應的版本&#xff1a;二、初始化工具[rootmululu ~]# cd /opt[rootmululu opt]# mkdir nmon[rootmululu opt]# cd nmon[rootmululu nmon]#wget http://sourceforge.net/projects/nmon/files/download/nmon_x86_12a.zip[rootmululu nmon]# u…

mysql join 循環_關于mysql聯表的內嵌循環操作nested loop join中on和where執行順序問題...

mysql的理論依據沒找到&#xff0c;個人理解是先執行where的過濾條件&#xff0c;先關聯再過濾明顯做的是無用功。oracle中倒是能在執行計劃中看到&#xff0c;先執行的是過濾條件(下面代碼中最后一行)。explain plan for SELECT * FROM tmp_t2 t2 LEFT JOIN tmp_t1 t1 ON t2.i…

python非法語句是_python 如何優雅的處理大量異常語句?

bs4的鏈式調用很贊&#xff0c;所以我把soup包裝了一下class MY_SOUP():包裝類def __init__(self,soup):self.soup soupif soup:if soup.string:self.string soup.string.strip()else:self.string Noneelse:self.string Nonedef find(self, *args, **kw):ret self.soup.fi…

Iptables詳解+實例

2019獨角獸企業重金招聘Python工程師標準>>> Iptabels是與Linux內核集成的包過濾防火墻系統&#xff0c;幾乎所有的linux發行版本都會包含Iptables的功能。如果 Linux 系統連接到因特網或 LAN、服務器或連接 LAN 和因特網的代理服務器&#xff0c; 則Iptables有利于…

django ipython shell_通過django的shell_plus編寫ipython腳本

Im writing a shell script which runs a command through ipython with the -c option like this:我正在編寫一個shell腳本&#xff0c;它通過ipython運行一個命令&#xff0c;使用-c選項&#xff0c;如下所示:ipython -c "from blah import myfunct; myfunct()"but…

阿里云服務器安裝onlyoffice_阿里云服務器安裝 JDK 8

歡迎關注“科技毒瘤君”&#xff01;上一期給大家分享了如何申請阿里云的免費云服務器&#xff0c;還沒有看過的小伙伴可以先前往了解 >>阿里云免費服務器<<這一次將會為大家分享如何在服務器上配置 Java環境&#xff0c;這里演示使用的系統為Ubuntu 18.04 64位&am…

js發送請求

1.Chrome控制臺中 net::ERR_CONNECTION_REFUSED js頻繁發送請求&#xff0c;有可能連接被拒絕&#xff0c;可用setTimeout&#xff0c;過幾秒發送&#xff0c;給個緩沖時間 var overlayAnalystService L.supermap.spatialAnalystService(serviceUrl); setTimeout(function () …

據說有99%的人都會做錯的面試題

這道題主要考察了面試者對浮點數存儲格式的理解。另外&#xff0c;請不要討論該題本身是否有意義之類的話題。本題只為了測試面試者相關的知識是否掌握&#xff0c;題目本身并沒有實際的意義。 下面有6個浮點類型變量&#xff0c;其中前三個是float類型的&#xff0c;后三個是d…

php使用mysql5和8的區別_mysql8.0和mysql5.7的區別是什么?

區別&#xff1a;mysql8.0的索引可以被隱藏和顯示&#xff0c;當一個索引隱藏時&#xff0c;他不會被查詢優化器所使用&#xff1b;2、mysql8.0新增了“SET PERSIST”命令&#xff1b;3、從mysql8.0開始&#xff0c;數據庫的缺省編碼將改為utf8mb4&#xff0c;包含了所有emoji字…

mysql pt check sum_percona工具pt-table-checksum

利用pt-table-checksum進行數據庫同步檢查rpm方式#wget percona.com/get/percona-toolkit.rpm源碼方式#wget http://www.percona.com/downloads/percona-toolkit/2.2.1/percona-toolkit-2.2.8.tar.gz#yum install perl perl-CPAN perl-DBD-MySQL perl-Time-HiRes解壓&#xff0…

如何通過BBED找回刪除數據

項目案例&#xff1a;客戶刪除delete了重要數據&#xff0c;無備份&#xff0c;客戶聯系我&#xff0c;要求恢復相應數據。本次通過實驗方式重現客戶現場。備份高于一切&#xff0c;首先備份&#xff0c;再操作 創建表格&#xff1a; create table king(age number,name varcha…

mysql 重置密碼語音_數字語音信號處理學習筆記語音信號的同態處理(2)

5.4 復倒譜和倒譜 定義 設信號x(n)的z變換為X(z) z[x(n)]&#xff0c;其對數為&#xff1a; (1) 那么 的逆z變換可寫成&#xff1a; (2) 取 (1)式則有 (3) 于是式子(2)則可以寫成 (4) 則式子(4)即為信號x(n)的復倒譜 的定義。因為 一般為復數&#xff0c;故稱 為復倒譜。如果對…

NFS 八步神曲

Server:第一步yum install - y nfs*第二步vi /etc/exports第三步/var/testdirs *(rw,all_squash,anonuid99,anongid99,sync)第四步service nfs start第五步chkconfig --level 35 nfs on Client第一步mount 192.168.1.X:/var/www/testdirs /var/www/testdirs第二步vi /et…

mysql權限日志_mysql權限管理、日志管理及常用工具

mysqlbinlog用法如下&#xff1a;mysqbinlog mysql.err 查詢錯誤日志當然可以通過添加參數來查看指定內容,如&#xff1a;mysqlbinlog mysql-bin.000001 -d test 只顯示對test數據庫的二進制日志mysqlbinlog mysql-bin.000001 -o 3 -r result-file 首先忽略前三個操作&…

Juicer.js模板引擎問題

由于jsp中的EL表達式語法和jquery.tmpl十分類似&#xff0c;&#xff0c;所以單純的使用${name}&#xff0c;數據是渲染不上tmpl的. SO.. 要加上轉義: ${${}amount} 或者 \${amount} 轉載于:https://www.cnblogs.com/fighxp/p/7890288.html

python把回車作為輸入_python將回車作為輸入內容的實例

當input輸入內容的時候,許多情況下輸入回車鍵另起一行輸入,但是這時候Pycharm就執行程序,然后結束,導致無法繼續輸入內容。 原因:Python默認遇到回車的時候,輸入結束。所以我們需要更改這個提示符,在遇到其他字符的時候,輸入才結束。 比如有一個任務: 請輸入文件名:憫…

ubuntu下修改時區和時間

applications-Accessories-Time & Date-點下鎖-輸入密碼-把時區改成上海&#xff08;這個要點圖中國與朝鮮之間的彎處才行&#xff0c;寫不生效&#xff09;-Set the time 選Manually-改下時間、日期-直接關閉即可(重啟后依然生效) 注&#xff1a;從電腦上邊的時間處-Time …