实现excel导出下载功能
This commit is contained in:
@@ -143,6 +143,16 @@
|
|||||||
<artifactId>spring-cloud-starter-eureka</artifactId>
|
<artifactId>spring-cloud-starter-eureka</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.poi</groupId>
|
||||||
|
<artifactId>poi</artifactId>
|
||||||
|
<version>3.17</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.poi</groupId>
|
||||||
|
<artifactId>poi-ooxml</artifactId>
|
||||||
|
<version>3.17</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package com.gzzn.omms.adminapi.controller.fltrs;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.gzzn.omms.adminapi.dto.fltrs.ExcelHeaderDto;
|
||||||
|
import com.gzzn.omms.adminapi.dto.fltrs.ExcelRowDto;
|
||||||
|
import com.gzzn.omms.adminapi.dto.fltrs.ExportToExcelDto;
|
||||||
|
import com.gzzn.omms.adminapi.utils.excel.ExcelData;
|
||||||
|
import com.gzzn.omms.adminapi.utils.excel.ExportExcelUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 航班控制器
|
||||||
|
* @author zhouxiunai
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@CrossOrigin
|
||||||
|
@RestController
|
||||||
|
public class FltrController {
|
||||||
|
|
||||||
|
@PostMapping("/fltrs/toExcel")
|
||||||
|
public void exportToExcel(@RequestBody ExportToExcelDto lsData,
|
||||||
|
HttpServletResponse response) {
|
||||||
|
|
||||||
|
//
|
||||||
|
ExcelData data = new ExcelData();
|
||||||
|
data.setName(lsData.getFileName());
|
||||||
|
|
||||||
|
//生成excel header
|
||||||
|
List<String> titles = new ArrayList();
|
||||||
|
for(ExcelHeaderDto headerData : lsData.getColumns() )
|
||||||
|
{
|
||||||
|
titles.add(headerData.getName());
|
||||||
|
}
|
||||||
|
data.setTitles(titles);
|
||||||
|
|
||||||
|
//
|
||||||
|
List<List<Object>> rows = new ArrayList();
|
||||||
|
for(ExcelRowDto rowData : lsData.getData() )
|
||||||
|
{
|
||||||
|
List<Object> row = new ArrayList();
|
||||||
|
for(ExcelHeaderDto headerData : lsData.getColumns() )
|
||||||
|
{
|
||||||
|
Method method = ReflectionUtils.findMethod(rowData.getClass(), "get" + headerData.getKey());
|
||||||
|
String cellData = (String) ReflectionUtils.invokeMethod(method, rowData);
|
||||||
|
row.add(cellData);
|
||||||
|
}
|
||||||
|
rows.add(row);
|
||||||
|
}
|
||||||
|
data.setRows(rows);
|
||||||
|
|
||||||
|
try {
|
||||||
|
ExportExcelUtils.exportExcel(response,data.getName() + "xlsx",data);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
} //end function
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.gzzn.omms.adminapi.dto.fltrs;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* excel 头 dto
|
||||||
|
* @author zhouxiunai
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ExcelHeaderDto {
|
||||||
|
private String key;//列key
|
||||||
|
private String name;//列名称
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
public void setKey(String key) {
|
||||||
|
this.key = key;
|
||||||
|
}
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,321 @@
|
|||||||
|
package com.gzzn.omms.adminapi.dto.fltrs;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
public class ExcelRowDto {
|
||||||
|
@JsonProperty(value = "ABDG")
|
||||||
|
private String ABDG;
|
||||||
|
|
||||||
|
@JsonProperty(value = "ACFT")
|
||||||
|
private String ACFT;
|
||||||
|
|
||||||
|
@JsonProperty(value = "ACTT")
|
||||||
|
private String ACTT;
|
||||||
|
|
||||||
|
@JsonProperty(value = "ALCD")
|
||||||
|
private String ALCD;
|
||||||
|
|
||||||
|
@JsonProperty(value = "AOTM_A")
|
||||||
|
private String AOTM_A;
|
||||||
|
|
||||||
|
@JsonProperty(value = "AOTM_D")
|
||||||
|
private String AOTM_D;
|
||||||
|
|
||||||
|
@JsonProperty(value = "ARSF")
|
||||||
|
private String ARSF;
|
||||||
|
|
||||||
|
@JsonProperty(value = "BELT")
|
||||||
|
private String BELT;
|
||||||
|
|
||||||
|
@JsonProperty(value = "BOTM")
|
||||||
|
private String BOTM;
|
||||||
|
|
||||||
|
@JsonProperty(value = "CCTM")
|
||||||
|
private String CCTM;
|
||||||
|
|
||||||
|
@JsonProperty(value = "CHKC")
|
||||||
|
private String CHKC;
|
||||||
|
|
||||||
|
@JsonProperty(value = "CHTM_OFF")
|
||||||
|
private String CHTM_OFF;
|
||||||
|
|
||||||
|
@JsonProperty(value = "CHTM_ON")
|
||||||
|
private String CHTM_ON;
|
||||||
|
|
||||||
|
@JsonProperty(value = "CHUT")
|
||||||
|
private String CHUT;
|
||||||
|
|
||||||
|
@JsonProperty(value = "COTM")
|
||||||
|
private String COTM;
|
||||||
|
|
||||||
|
@JsonProperty(value = "CSFT")
|
||||||
|
private String CSFT;
|
||||||
|
|
||||||
|
@JsonProperty(value = "DESF")
|
||||||
|
private String DESF;
|
||||||
|
|
||||||
|
@JsonProperty(value = "ERUT")
|
||||||
|
private String ERUT;
|
||||||
|
|
||||||
|
@JsonProperty(value = "ESTT")
|
||||||
|
private String ESTT;
|
||||||
|
|
||||||
|
@JsonProperty(value = "FLDT")
|
||||||
|
private String FLDT;
|
||||||
|
|
||||||
|
@JsonProperty(value = "FLID")
|
||||||
|
private String FLID;
|
||||||
|
|
||||||
|
@JsonProperty(value = "FLIN")
|
||||||
|
private String FLIN;
|
||||||
|
|
||||||
|
@JsonProperty(value = "FLNO")
|
||||||
|
private String FLNO;
|
||||||
|
|
||||||
|
@JsonProperty(value = "FLTY")
|
||||||
|
private String FLTY;
|
||||||
|
|
||||||
|
@JsonProperty(value = "FTSS")
|
||||||
|
private String FTSS;
|
||||||
|
|
||||||
|
@JsonProperty(value = "GCTM")
|
||||||
|
private String GCTM;
|
||||||
|
|
||||||
|
@JsonProperty(value = "GTDT")
|
||||||
|
private String GTDT;
|
||||||
|
|
||||||
|
@JsonProperty(value = "LACL")
|
||||||
|
private String LACL;
|
||||||
|
|
||||||
|
@JsonProperty(value = "MVIN")
|
||||||
|
private String MVIN;
|
||||||
|
|
||||||
|
@JsonProperty(value = "PADT")
|
||||||
|
private String PADT;
|
||||||
|
|
||||||
|
@JsonProperty(value = "PSST")
|
||||||
|
private String PSST;
|
||||||
|
|
||||||
|
@JsonProperty(value = "RENO")
|
||||||
|
private String RENO;
|
||||||
|
|
||||||
|
@JsonProperty(value = "SODT")
|
||||||
|
private String SODT;
|
||||||
|
|
||||||
|
@JsonProperty(value = "STND")
|
||||||
|
private String STND;
|
||||||
|
|
||||||
|
@JsonProperty(value = "TRML")
|
||||||
|
private String TRML;
|
||||||
|
|
||||||
|
public String getABDG() {
|
||||||
|
return ABDG;
|
||||||
|
}
|
||||||
|
public void setABDG(String aBDG) {
|
||||||
|
ABDG = aBDG;
|
||||||
|
}
|
||||||
|
public String getACFT() {
|
||||||
|
return ACFT;
|
||||||
|
}
|
||||||
|
public void setACFT(String aCFT) {
|
||||||
|
ACFT = aCFT;
|
||||||
|
}
|
||||||
|
public String getACTT() {
|
||||||
|
return ACTT;
|
||||||
|
}
|
||||||
|
public void setACTT(String aCTT) {
|
||||||
|
ACTT = aCTT;
|
||||||
|
}
|
||||||
|
public String getALCD() {
|
||||||
|
return ALCD;
|
||||||
|
}
|
||||||
|
public void setALCD(String aLCD) {
|
||||||
|
ALCD = aLCD;
|
||||||
|
}
|
||||||
|
public String getAOTM_A() {
|
||||||
|
return AOTM_A;
|
||||||
|
}
|
||||||
|
public void setAOTM_A(String aOTM_A) {
|
||||||
|
AOTM_A = aOTM_A;
|
||||||
|
}
|
||||||
|
public String getAOTM_D() {
|
||||||
|
return AOTM_D;
|
||||||
|
}
|
||||||
|
public void setAOTM_D(String aOTM_D) {
|
||||||
|
AOTM_D = aOTM_D;
|
||||||
|
}
|
||||||
|
public String getARSF() {
|
||||||
|
return ARSF;
|
||||||
|
}
|
||||||
|
public void setARSF(String aRSF) {
|
||||||
|
ARSF = aRSF;
|
||||||
|
}
|
||||||
|
public String getBELT() {
|
||||||
|
return BELT;
|
||||||
|
}
|
||||||
|
public void setBELT(String bELT) {
|
||||||
|
BELT = bELT;
|
||||||
|
}
|
||||||
|
public String getBOTM() {
|
||||||
|
return BOTM;
|
||||||
|
}
|
||||||
|
public void setBOTM(String bOTM) {
|
||||||
|
BOTM = bOTM;
|
||||||
|
}
|
||||||
|
public String getCCTM() {
|
||||||
|
return CCTM;
|
||||||
|
}
|
||||||
|
public void setCCTM(String cCTM) {
|
||||||
|
CCTM = cCTM;
|
||||||
|
}
|
||||||
|
public String getCHKC() {
|
||||||
|
return CHKC;
|
||||||
|
}
|
||||||
|
public void setCHKC(String cHKC) {
|
||||||
|
CHKC = cHKC;
|
||||||
|
}
|
||||||
|
public String getCHTM_OFF() {
|
||||||
|
return CHTM_OFF;
|
||||||
|
}
|
||||||
|
public void setCHTM_OFF(String cHTM_OFF) {
|
||||||
|
CHTM_OFF = cHTM_OFF;
|
||||||
|
}
|
||||||
|
public String getCHTM_ON() {
|
||||||
|
return CHTM_ON;
|
||||||
|
}
|
||||||
|
public void setCHTM_ON(String cHTM_ON) {
|
||||||
|
CHTM_ON = cHTM_ON;
|
||||||
|
}
|
||||||
|
public String getCHUT() {
|
||||||
|
return CHUT;
|
||||||
|
}
|
||||||
|
public void setCHUT(String cHUT) {
|
||||||
|
CHUT = cHUT;
|
||||||
|
}
|
||||||
|
public String getCOTM() {
|
||||||
|
return COTM;
|
||||||
|
}
|
||||||
|
public void setCOTM(String cOTM) {
|
||||||
|
COTM = cOTM;
|
||||||
|
}
|
||||||
|
public String getCSFT() {
|
||||||
|
return CSFT;
|
||||||
|
}
|
||||||
|
public void setCSFT(String cSFT) {
|
||||||
|
CSFT = cSFT;
|
||||||
|
}
|
||||||
|
public String getDESF() {
|
||||||
|
return DESF;
|
||||||
|
}
|
||||||
|
public void setDESF(String dESF) {
|
||||||
|
DESF = dESF;
|
||||||
|
}
|
||||||
|
public String getERUT() {
|
||||||
|
return ERUT;
|
||||||
|
}
|
||||||
|
public void setERUT(String eRUT) {
|
||||||
|
ERUT = eRUT;
|
||||||
|
}
|
||||||
|
public String getESTT() {
|
||||||
|
return ESTT;
|
||||||
|
}
|
||||||
|
public void setESTT(String eSTT) {
|
||||||
|
ESTT = eSTT;
|
||||||
|
}
|
||||||
|
public String getFLDT() {
|
||||||
|
return FLDT;
|
||||||
|
}
|
||||||
|
public void setFLDT(String fLDT) {
|
||||||
|
FLDT = fLDT;
|
||||||
|
}
|
||||||
|
public String getFLID() {
|
||||||
|
return FLID;
|
||||||
|
}
|
||||||
|
public void setFLID(String fLID) {
|
||||||
|
FLID = fLID;
|
||||||
|
}
|
||||||
|
public String getFLIN() {
|
||||||
|
return FLIN;
|
||||||
|
}
|
||||||
|
public void setFLIN(String fLIN) {
|
||||||
|
FLIN = fLIN;
|
||||||
|
}
|
||||||
|
public String getFLNO() {
|
||||||
|
return FLNO;
|
||||||
|
}
|
||||||
|
public void setFLNO(String fLNO) {
|
||||||
|
FLNO = fLNO;
|
||||||
|
}
|
||||||
|
public String getFLTY() {
|
||||||
|
return FLTY;
|
||||||
|
}
|
||||||
|
public void setFLTY(String fLTY) {
|
||||||
|
FLTY = fLTY;
|
||||||
|
}
|
||||||
|
public String getFTSS() {
|
||||||
|
return FTSS;
|
||||||
|
}
|
||||||
|
public void setFTSS(String fTSS) {
|
||||||
|
FTSS = fTSS;
|
||||||
|
}
|
||||||
|
public String getGCTM() {
|
||||||
|
return GCTM;
|
||||||
|
}
|
||||||
|
public void setGCTM(String gCTM) {
|
||||||
|
GCTM = gCTM;
|
||||||
|
}
|
||||||
|
public String getGTDT() {
|
||||||
|
return GTDT;
|
||||||
|
}
|
||||||
|
public void setGTDT(String gTDT) {
|
||||||
|
GTDT = gTDT;
|
||||||
|
}
|
||||||
|
public String getLACL() {
|
||||||
|
return LACL;
|
||||||
|
}
|
||||||
|
public void setLACL(String lACL) {
|
||||||
|
LACL = lACL;
|
||||||
|
}
|
||||||
|
public String getMVIN() {
|
||||||
|
return MVIN;
|
||||||
|
}
|
||||||
|
public void setMVIN(String mVIN) {
|
||||||
|
MVIN = mVIN;
|
||||||
|
}
|
||||||
|
public String getPADT() {
|
||||||
|
return PADT;
|
||||||
|
}
|
||||||
|
public void setPADT(String pADT) {
|
||||||
|
PADT = pADT;
|
||||||
|
}
|
||||||
|
public String getPSST() {
|
||||||
|
return PSST;
|
||||||
|
}
|
||||||
|
public void setPSST(String pSST) {
|
||||||
|
PSST = pSST;
|
||||||
|
}
|
||||||
|
public String getRENO() {
|
||||||
|
return RENO;
|
||||||
|
}
|
||||||
|
public void setRENO(String rENO) {
|
||||||
|
RENO = rENO;
|
||||||
|
}
|
||||||
|
public String getSODT() {
|
||||||
|
return SODT;
|
||||||
|
}
|
||||||
|
public void setSODT(String sODT) {
|
||||||
|
SODT = sODT;
|
||||||
|
}
|
||||||
|
public String getSTND() {
|
||||||
|
return STND;
|
||||||
|
}
|
||||||
|
public void setSTND(String sTND) {
|
||||||
|
STND = sTND;
|
||||||
|
}
|
||||||
|
public String getTRML() {
|
||||||
|
return TRML;
|
||||||
|
}
|
||||||
|
public void setTRML(String tRML) {
|
||||||
|
TRML = tRML;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.gzzn.omms.adminapi.dto.fltrs;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ExportToExcelDto {
|
||||||
|
private String fileName; //导出的文件名
|
||||||
|
private List<ExcelHeaderDto> columns;//要导出的列
|
||||||
|
private List<ExcelRowDto> data;//要导出的数据
|
||||||
|
|
||||||
|
|
||||||
|
public List<ExcelHeaderDto> getColumns() {
|
||||||
|
return columns;
|
||||||
|
}
|
||||||
|
public void setColumns(List<ExcelHeaderDto> columns) {
|
||||||
|
this.columns = columns;
|
||||||
|
}
|
||||||
|
public List<ExcelRowDto> getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
public void setData(List<ExcelRowDto> data) {
|
||||||
|
this.data = data;
|
||||||
|
}
|
||||||
|
public String getFileName() {
|
||||||
|
return fileName;
|
||||||
|
}
|
||||||
|
public void setFileName(String fileName) {
|
||||||
|
this.fileName = fileName;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package com.gzzn.omms.adminapi.utils.excel;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ExcelData implements Serializable {
|
||||||
|
private static final long serialVersionUID = 4444017239100620999L;
|
||||||
|
|
||||||
|
// 表头
|
||||||
|
private List<String> titles;
|
||||||
|
|
||||||
|
// 数据
|
||||||
|
private List<List<Object>> rows;
|
||||||
|
|
||||||
|
// 页签名称
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public List<String> getTitles() {
|
||||||
|
return titles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitles(List<String> titles) {
|
||||||
|
this.titles = titles;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<List<Object>> getRows() {
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRows(List<List<Object>> rows) {
|
||||||
|
this.rows = rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,158 @@
|
|||||||
|
package com.gzzn.omms.adminapi.utils.excel;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.apache.poi.ss.usermodel.BorderStyle;
|
||||||
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
|
import org.apache.poi.ss.usermodel.FillPatternType;
|
||||||
|
import org.apache.poi.ss.usermodel.Font;
|
||||||
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
||||||
|
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||||
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
|
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFColor;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||||
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
|
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* excel 相关工具类
|
||||||
|
* @author Administrator
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ExportExcelUtils {
|
||||||
|
public static void exportExcel(HttpServletResponse response, String fileName, ExcelData data) throws Exception {
|
||||||
|
// 告诉浏览器用什么软件可以打开此文件
|
||||||
|
response.setHeader("content-Type", "application/vnd.ms-excel");
|
||||||
|
// 下载文件的默认名称
|
||||||
|
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "utf-8"));
|
||||||
|
exportExcel(data, response.getOutputStream());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void exportExcel(ExcelData data, OutputStream out) throws Exception {
|
||||||
|
|
||||||
|
XSSFWorkbook wb = new XSSFWorkbook();
|
||||||
|
try {
|
||||||
|
String sheetName = data.getName();
|
||||||
|
if (null == sheetName) {
|
||||||
|
sheetName = "Sheet1";
|
||||||
|
}
|
||||||
|
XSSFSheet sheet = wb.createSheet(sheetName);
|
||||||
|
writeExcel(wb, sheet, data);
|
||||||
|
|
||||||
|
wb.write(out);
|
||||||
|
} finally {
|
||||||
|
wb.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void writeExcel(XSSFWorkbook wb, Sheet sheet, ExcelData data) {
|
||||||
|
|
||||||
|
int rowIndex = 0;
|
||||||
|
|
||||||
|
rowIndex = writeTitlesToExcel(wb, sheet, data.getTitles());
|
||||||
|
writeRowsToExcel(wb, sheet, data.getRows(), rowIndex);
|
||||||
|
autoSizeColumns(sheet, data.getTitles().size() + 1);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int writeTitlesToExcel(XSSFWorkbook wb, Sheet sheet, List<String> titles) {
|
||||||
|
int rowIndex = 0;
|
||||||
|
int colIndex = 0;
|
||||||
|
|
||||||
|
Font titleFont = wb.createFont();
|
||||||
|
titleFont.setFontName("simsun");
|
||||||
|
titleFont.setBold(true);
|
||||||
|
// titleFont.setFontHeightInPoints((short) 14);
|
||||||
|
titleFont.setColor(IndexedColors.BLACK.index);
|
||||||
|
|
||||||
|
XSSFCellStyle titleStyle = wb.createCellStyle();
|
||||||
|
titleStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||||
|
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||||
|
titleStyle.setFillForegroundColor(new XSSFColor(new Color(182, 184, 192)));
|
||||||
|
titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
||||||
|
titleStyle.setFont(titleFont);
|
||||||
|
setBorder(titleStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
|
||||||
|
|
||||||
|
Row titleRow = sheet.createRow(rowIndex);
|
||||||
|
// titleRow.setHeightInPoints(25);
|
||||||
|
colIndex = 0;
|
||||||
|
|
||||||
|
for (String field : titles) {
|
||||||
|
Cell cell = titleRow.createCell(colIndex);
|
||||||
|
cell.setCellValue(field);
|
||||||
|
cell.setCellStyle(titleStyle);
|
||||||
|
colIndex++;
|
||||||
|
}
|
||||||
|
|
||||||
|
rowIndex++;
|
||||||
|
return rowIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int writeRowsToExcel(XSSFWorkbook wb, Sheet sheet, List<List<Object>> rows, int rowIndex) {
|
||||||
|
int colIndex = 0;
|
||||||
|
|
||||||
|
Font dataFont = wb.createFont();
|
||||||
|
dataFont.setFontName("simsun");
|
||||||
|
// dataFont.setFontHeightInPoints((short) 14);
|
||||||
|
dataFont.setColor(IndexedColors.BLACK.index);
|
||||||
|
|
||||||
|
XSSFCellStyle dataStyle = wb.createCellStyle();
|
||||||
|
dataStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||||
|
dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||||
|
dataStyle.setFont(dataFont);
|
||||||
|
setBorder(dataStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
|
||||||
|
|
||||||
|
for (List<Object> rowData : rows) {
|
||||||
|
Row dataRow = sheet.createRow(rowIndex);
|
||||||
|
// dataRow.setHeightInPoints(25);
|
||||||
|
colIndex = 0;
|
||||||
|
|
||||||
|
for (Object cellData : rowData) {
|
||||||
|
Cell cell = dataRow.createCell(colIndex);
|
||||||
|
if (cellData != null) {
|
||||||
|
cell.setCellValue(cellData.toString());
|
||||||
|
} else {
|
||||||
|
cell.setCellValue("");
|
||||||
|
}
|
||||||
|
|
||||||
|
cell.setCellStyle(dataStyle);
|
||||||
|
colIndex++;
|
||||||
|
}
|
||||||
|
rowIndex++;
|
||||||
|
}
|
||||||
|
return rowIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void autoSizeColumns(Sheet sheet, int columnNumber) {
|
||||||
|
|
||||||
|
for (int i = 0; i < columnNumber; i++) {
|
||||||
|
int orgWidth = sheet.getColumnWidth(i);
|
||||||
|
sheet.autoSizeColumn(i, true);
|
||||||
|
int newWidth = (int) (sheet.getColumnWidth(i) + 100);
|
||||||
|
if (newWidth > orgWidth) {
|
||||||
|
sheet.setColumnWidth(i, newWidth);
|
||||||
|
} else {
|
||||||
|
sheet.setColumnWidth(i, orgWidth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setBorder(XSSFCellStyle style, BorderStyle border, XSSFColor color) {
|
||||||
|
style.setBorderTop(border);
|
||||||
|
style.setBorderLeft(border);
|
||||||
|
style.setBorderRight(border);
|
||||||
|
style.setBorderBottom(border);
|
||||||
|
style.setBorderColor(BorderSide.TOP, color);
|
||||||
|
style.setBorderColor(BorderSide.LEFT, color);
|
||||||
|
style.setBorderColor(BorderSide.RIGHT, color);
|
||||||
|
style.setBorderColor(BorderSide.BOTTOM, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user