Java使用Apache.POI中HSSFWorkbook導(dǎo)出到Excel的實(shí)現(xiàn)方法
使用Apache.POI中HSSFWorkbook導(dǎo)出到Excel,具體內(nèi)容如下所示:
1.引入Poi依賴(lài)(3.12)
依賴(lài)如下:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.12</version> </dependency>
2.創(chuàng)建實(shí)體類(lèi)(User.java)
package com.kd.nm.entity.pojo;
/**
* 實(shí)體類(lèi)(User)
*
* author 小辰哥哥
*/
public class User {
// 用戶(hù)編號(hào)
private String userNo;
// 用戶(hù)名稱(chēng)
private String userName;
// 年齡
private String age;
// 無(wú)參構(gòu)造
public User() {
}
// 有參構(gòu)造
public User(String userNo, String userName, String age) {
this.userNo = userNo;
this.userName = userName;
this.age = age;
}
// get與set方法進(jìn)行封裝
public String getUserNo() {
return userNo;
}
public void setUserNo(String userNo) {
this.userNo = userNo;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
// 重新toString方法
@Override
public String toString() {
return "User{" +
"userNo='" + userNo + '\'' +
", userName='" + userName + '\'' +
", age='" + age + '\'' +
'}';
}
}
3.Excel相關(guān)工具類(lèi)(ExcelUtil、ReflectUtil)
package com.kd.nm.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.*;
/**
* Description : Excel相關(guān)工具類(lèi)
*
* @author: 小辰哥哥
*
*/
public class ExcelUtil {
/**
* 生成excel表格
* @param heads 表頭內(nèi)容
* @param data 數(shù)據(jù)內(nèi)容
* @return
*/
public static HSSFWorkbook creatExcel(Map<String, String> heads, List data) {
// 聲明一個(gè)工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一個(gè)表格
HSSFSheet sheet = workbook.createSheet();
// 生成標(biāo)題行樣式
HSSFCellStyle headStyle = creatStyle(workbook, (short) 14);
// 生成表格內(nèi)容樣式
HSSFCellStyle bodyStyle = creatStyle(workbook, (short) 10);
// 標(biāo)題元素
List<String> keys = new ArrayList<String>(heads.keySet());
// 像素單位
short px = 1000;
// 設(shè)置列寬
for (int columnIndex = 0; columnIndex < keys.size(); columnIndex++) {
sheet.setColumnWidth(columnIndex, 6 * px);
}
// 生成表格
for (int rowNum = 0; rowNum <= data.size(); rowNum++) {
// 創(chuàng)建行
HSSFRow row = sheet.createRow(rowNum);
for (int cellNum = 0; cellNum < keys.size(); cellNum++) {
// 創(chuàng)建列
HSSFCell cell = row.createCell(cellNum);
// 標(biāo)題
if (rowNum == 0) {
cell.setCellStyle(headStyle);
cell.setCellValue(heads.get(keys.get(cellNum)));
} else { // 內(nèi)容
cell.setCellStyle(bodyStyle);
// 通過(guò)反射獲取
cell.setCellValue(ReflectUtil.getValue(keys.get(cellNum), data.get(rowNum - 1)));
}
}
}
return workbook;
}
/**
* 生成樣式
* @param workbook
* @param size
* @return
*/
public static HSSFCellStyle creatStyle(HSSFWorkbook workbook, short size) {
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment((HSSFCellStyle.ALIGN_CENTER));
style.setVerticalAlignment((HSSFCellStyle.VERTICAL_CENTER));
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints(size);
font.setFontName("微軟雅黑");
style.setFont(font);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
return style;
}
}
package com.kd.nm.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
/**
* 反射工具包
*
* @author: 小辰哥哥
*/
public class ReflectUtil {
private static final Logger logger = LoggerFactory.getLogger(ReflectUtil.class);
public static String getValue(String key, Object obj) {
String value = "";
try {
// 獲取當(dāng)前屬性
PropertyDescriptor pd = new PropertyDescriptor(key, obj.getClass());
// 獲取get方法
Method getMd = pd.getReadMethod();
value = getMd.invoke(obj).toString();
} catch (Exception e) {
logger.error("獲取內(nèi)容失?。?);
e.printStackTrace();
}
return value;
}
public static void setValue(String key, String value, Object obj) {
try {
// 獲取當(dāng)前屬性
PropertyDescriptor pd = new PropertyDescriptor(key, obj.getClass());
// 獲取set方法
Method writeMd = pd.getWriteMethod();
writeMd.invoke(obj, value);
} catch (Exception e) {
logger.error("設(shè)置內(nèi)容失?。?);
e.printStackTrace();
}
}
}
4.后端控制器代碼
@RequestMapping(value = "/exportExcel",method = RequestMethod.GET,produces = "application/json")
public void exportExcel(HttpServletResponse httpServletResponse) throws IOException {
// 表頭內(nèi)容(可在前端設(shè)置,通過(guò)參數(shù)傳遞進(jìn)來(lái)) Key是實(shí)體類(lèi)的屬性值,value是表頭的lable
Map<String,String> head = new HashMap<>();
head.put("userNo","用戶(hù)編號(hào)");
head.put("userName","用戶(hù)名稱(chēng)");
head.put("age","年齡");
// 表格數(shù)據(jù)內(nèi)容,模擬數(shù)據(jù)庫(kù)查詢(xún)出來(lái)的數(shù)據(jù)
List<User> data = new ArrayList<>();
data.add(new User("1","小辰哥哥","18"));
data.add(new User("2","小豬妹妹","18"));
data.add(new User("3","大豬哥哥","18"));
// 生成工作薄
HSSFWorkbook hssfWorkbook = ExcelUtil.creatExcel(head, data);
// 定義文件名
String fileName = "導(dǎo)出Excel表格";
httpServletResponse.setHeader("Cache-Control", "max-age=0");
httpServletResponse.setContentType("application/vnd.ms-excel");
httpServletResponse.addHeader("Content-disposition", "attachment;filename=" + new String(fileName.getBytes("gb2312"),
"ISO-8859-1") + ".xls");
OutputStream outputStream = httpServletResponse.getOutputStream();
hssfWorkbook.write(outputStream);
outputStream.flush();
outputStream.close();
}
5.訪(fǎng)問(wèn)映射地址
接口訪(fǎng)問(wèn):
http://localhost:9090/FaultTreatment/api/standard/exportExcel


到此這篇關(guān)于Java使用Apache.POI中HSSFWorkbook導(dǎo)出到Excel的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)Apache.POI中HSSFWorkbook導(dǎo)出到Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
CentOS7 LNMP+phpmyadmin環(huán)境搭建 第二篇LNMP環(huán)境搭建教程
這篇文章主要為大家詳細(xì)介紹了CentOS7 LNMP+phpmyadmin環(huán)境搭建,第二篇LNMP環(huán)境搭建教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
樹(shù)莓派搭建nas服務(wù)器的詳細(xì)過(guò)程
這篇文章主要介紹了樹(shù)莓派搭建nas服務(wù)器的教程,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01
apache time_wait連接數(shù)太多問(wèn)題解決方法
這篇文章主要介紹了apache time_wait連接數(shù)太多問(wèn)題解決方法,本文使用調(diào)整內(nèi)核參數(shù)來(lái)解決,需要的朋友可以參考下2014-11-11
SSH遠(yuǎn)程登錄和端口轉(zhuǎn)發(fā)詳解
這篇文章主要介紹了關(guān)于SSH遠(yuǎn)程登錄和端口轉(zhuǎn)發(fā)的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-03-03
vscode遠(yuǎn)程開(kāi)發(fā)使用SSH遠(yuǎn)程連接服務(wù)器的方法「內(nèi)網(wǎng)穿透」
這篇文章主要介紹了vscode遠(yuǎn)程開(kāi)發(fā)使用SSH遠(yuǎn)程連接服務(wù)器?「內(nèi)網(wǎng)穿透」,通過(guò)本文學(xué)習(xí)我們將通過(guò)vscode實(shí)現(xiàn)遠(yuǎn)程開(kāi)發(fā),并做內(nèi)網(wǎng)穿透實(shí)現(xiàn)在公網(wǎng)環(huán)境下的遠(yuǎn)程連接,在外任意地方也可以遠(yuǎn)程連接服務(wù)器進(jìn)行開(kāi)發(fā)寫(xiě)代碼,需要的朋友可以參考下2023-02-02

