Java使⽤Apache.POI中HSSFWorkbook导出到Excel的实
现⽅法
使⽤Apache.POI中HSSFWorkbook导出到Excel,具体内容如下所⽰:
1.引⼊Poi依赖(3.12)
依赖如下:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.12</version>
</dependency>
2.创建实体类(User.java)
package ity.pojo;
/**
* 实体类(User)
*
* author ⼩⾠哥哥
*/
public class User {
// ⽤户编号
private String userNo;
// ⽤户名称
private String userName;
/
/ 年龄
private String age;
// ⽆参构造
public User() {
}
// 有参构造
public User(String userNo, String userName, String age) {
this.userNo = userNo;
this.userName = userName;
this.age = age;
}
java的tostring方法/
/ get与set⽅法进⾏封装
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相关⼯具类(ExcelUtil、ReflectUtil)
package util;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.*;
/**
* Description : Excel相关⼯具类
*
* @author: ⼩⾠哥哥
*
*/
public class ExcelUtil {
/**
* ⽣成excel表格
* @param heads 表头内容
* @param data 数据内容
* @return
*/
public static HSSFWorkbook creatExcel(Map<String, String> heads, List data) {
// 声明⼀个⼯作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// ⽣成⼀个表格
HSSFSheet sheet = ateSheet();
// ⽣成标题⾏样式
HSSFCellStyle headStyle = creatStyle(workbook, (short) 14);
/
/ ⽣成表格内容样式
HSSFCellStyle bodyStyle = creatStyle(workbook, (short) 10);
// 标题元素
List<String> keys = new ArrayList<String>(heads.keySet());
// 像素单位
short px = 1000;
// 设置列宽
for (int columnIndex = 0; columnIndex < keys.size(); columnIndex++) {
sheet.setColumnWidth(columnIndex, 6 * px);
}
// ⽣成表格
for (int rowNum = 0; rowNum <= data.size(); rowNum++) {
// 创建⾏
HSSFRow row = ateRow(rowNum);
for (int cellNum = 0; cellNum < keys.size(); cellNum++) {
// 创建列
HSSFCell cell = ateCell(cellNum);
// 标题
if (rowNum == 0) {
cell.setCellStyle(headStyle);
cell.((cellNum)));
} else { // 内容
cell.setCellStyle(bodyStyle);
// 通过反射获取
cell.(cellNum), (rowNum - 1)));    }
}
}
return workbook;
}
/**
* ⽣成样式
* @param workbook
* @param size
* @return
*/
public static HSSFCellStyle creatStyle(HSSFWorkbook workbook, short size) {  HSSFCellStyle style = ateCellStyle();
style.setAlignment((HSSFCellStyle.ALIGN_CENTER));
style.setVerticalAlignment((HSSFCellStyle.VERTICAL_CENTER));
HSSFFont font = ateFont();
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 util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ReflectionUtils;
import java.beans.PropertyDescriptor;
import flect.Method;
/
**
* 反射⼯具包
*
* @author: ⼩⾠哥哥
*/
public class ReflectUtil {
private static final Logger logger = Logger(ReflectUtil.class); public static String getValue(String key, Object obj) {
String value = "";
try {
// 获取当前属性
PropertyDescriptor pd = new PropertyDescriptor(key, Class());
// 获取get⽅法
Method getMd = pd.getReadMethod();
value = getMd.invoke(obj).toString();
} catch (Exception e) {
<("获取内容失败!");
e.printStackTrace();
}
return value;
}
public static void setValue(String key, String value, Object obj) {
try {
/
/ 获取当前属性
PropertyDescriptor pd = new PropertyDescriptor(key, Class());
// 获取set⽅法
Method writeMd = pd.getWriteMethod();
writeMd.invoke(obj, value);
} catch (Exception e) {
<("设置内容失败!");
e.printStackTrace();
}
}
}
4.后端控制器代码
@RequestMapping(value = "/exportExcel",method = RequestMethod.GET,produces = "application/json")
public void exportExcel(HttpServletResponse httpServletResponse) throws IOException {
// 表头内容(可在前端设置,通过参数传递进来) Key是实体类的属性值,value是表头的lable
Map<String,String> head = new HashMap<>();
head.put("userNo","⽤户编号");
head.put("userName","⽤户名称");
head.put("age","年龄");
// 表格数据内容,模拟数据库查询出来的数据
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 = atExcel(head, data);
// 定义⽂件名
String fileName = "导出Excel表格";
httpServletResponse.setHeader("Cache-Control", "max-age=0");
httpServletResponse.setContentType("application/vnd.ms-excel");
httpServletResponse.addHeader("Content-disposition", "attachment;filename=" + new Bytes("gb2312"),
"ISO-8859-1") + ".xls");
OutputStream outputStream = OutputStream();
hssfWorkbook.write(outputStream);
outputStream.flush();
outputStream.close();
}
5.访问映射地址
接⼝访问:
到此这篇关于Java使⽤Apache.POI中HSSFWorkbook导出到Excel的实现⽅法的⽂章就介绍到这了,更多相关Apache.POI中HSSFWorkbook导出到Excel内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。