从数据库中获取数据导出成Excel
Java中提供了导出的工具包poi,引入依赖,从数据库中获取数据,将数据进行封装,导出到excel中即可。2.读取数据3.导出文件总结
·
数据库文件导出Excel
一、如何将数据导出生成Excel?
Java中提供了导出的工具包poi,引入依赖,从数据库中获取数据,将数据进行封装,导出到excel中即可。
二、核心代码
1.导入依赖
// poi有多种jar包,按需求导入即可
// 最普通的poi,导出为 .xls
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
// 导出为 .xlsx
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
// 导出为 word、ppt、viso、outlook,这种目前没用到过,有需要小伙伴可以自行研究
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>5.0.0</version>
</dependency>
2.读取数据
这里没什么好说的,我用的mybatis-plus,将数据封装为map集合即可,之所以是封装为map,是为了在之后导入excel时,与表头对应
List<Map<String, Object>> mapList = userInfoMapper.selectMaps(wrapper);
3.导出文件
@Data
public class ExcelUtil {
// 各列表头对应字段和名称,例如:<"name","姓名">
private LinkedHashMap<String, String> heads;
// 导出的数据集合
private List<Map<String, Object>> dataList;
// 转换集合,这里是由于我有个字段库中是0,1存储,展示时需要进行转换
private Map<String, Map<String, String>> convertMap;
// 字体大小,可自由发挥
private short fontSize = 12;
// 工作表,数据存入的表名,也可自由发挥
private String sheetName = "sheet1";
// 几种构造方法,相信大家应该都懂
public ExcelUtil() {
}
public ExcelUtil(LinkedHashMap<String, String> heads, List<Map<String, Object>> dataList) {
this.heads = heads;
this.dataList = dataList;
}
public ExcelUtil(LinkedHashMap<String, String> heads, List<Map<String, Object>> dataList, short fontSize, String sheetName) {
this.heads = heads;
this.dataList = dataList;
this.fontSize = fontSize;
this.sheetName = sheetName;
}
public ExcelUtil(LinkedHashMap<String, String> heads, List<Map<String, Object>> dataList, Map<String, Map<String, String>> convertMap, short fontSize, String sheetName) {
this.heads = heads;
this.dataList = dataList;
this.convertMap = convertMap;
this.fontSize = fontSize;
this.sheetName = sheetName;
}
// 具体的导出方法,我是以流的形式响应回去的
public void export(HttpServletResponse response) throws IOException {
// 这里做了个小校验
checkConfig();
// 创建工作簿(即excel)
XSSFWorkbook wb = new XSSFWorkbook();
//创建工作表
XSSFSheet wbSheet = wb.createSheet(this.sheetName);
// 表头
XSSFCellStyle headStyle = wb.createCellStyle();
// 对齐方式,居中
headStyle.setAlignment(HorizontalAlignment.CENTER);
headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 创建第一行
XSSFRow headRow = wbSheet.createRow(0);
XSSFFont font = wb.createFont();
// 字体大小
font.setFontHeightInPoints(this.fontSize);
// 字体加粗
font.setBold(true);
headStyle.setFont(font);
// 首行样式
XSSFCell headRowCell = null;
// 列
int headCellIndex = 0;
for (Map.Entry<String, String> entry : heads.entrySet()) {
// 列
headRowCell = headRow.createCell(headCellIndex);
// 填充数据
headRowCell.setCellValue(entry.getValue());
// 样式
headRowCell.setCellStyle(headStyle);
// 列宽
wbSheet.setColumnWidth(headCellIndex,5000);
headCellIndex++;
}
// 第二行,从0行开始,1代表第二行,大家应该都能理解
int rowIndex = 1;
// 判断导出是否为空,为空的话只导出一个表头
if (CollectionUtils.isNotEmpty(dataList)) {
// 遍历数据
for (Map<String, Object> dataMap : dataList) {
// 创建行
XSSFRow dataRow = wbSheet.createRow(rowIndex);
XSSFCell dataRowCell = null;
int dataCellIndex = 0;
// 插入数据到每一列中
for (Map.Entry<String, String> entry : heads.entrySet()) {
dataRowCell = dataRow.createCell(dataCellIndex);
// 之所以转换数据封装为map,就是为了在这里可以对应上
Object cellValue = dataMap.get(entry.getKey());
String value = "";
// 数据转换
if (CollectionUtils.isNotEmpty(convertMap) && convertMap.get(entry.getKey()) != null) {
value = convertMap.get(entry.getKey()).get(cellValue == null ? "" : cellValue.toString());
} else {
value = cellValue == null ? "" : cellValue.toString();
}
dataRowCell.setCellValue(value);
dataCellIndex++;
}
rowIndex++;
}
}
// 若传递中文文件名,需要转换,将文件名改成自己的即可
//new String ("文件名".getBytes("GBK"),"ISO-8859-1");
response.setHeader("Content-Disposition", "attachment;Filename=" + System.currentTimeMillis() + ".xls");
OutputStream outputStream = response.getOutputStream();
wb.write(outputStream);
outputStream.close();
// 导出到本地也很简单,自定义流就好
//File file= new File("D:/"+"文件名"+ ".xls");
//OutputStream outputStream = new FileOutputStream(file);
//wb.write(outputStream);
//outputStream.close();
}
// 校验
protected void checkConfig() throws IOException {
if (heads == null || heads.size() == 0) {
throw new IOException("列名不能为空或者为NULL");
}
if (fontSize < 0) {
throw new IOException("字体大小不能为负值");
}
}
}
总结
数据导出也是一个挺常见的业务,也比较简单,注释写的也很详细,有不懂的也可以私信,看见就会回,有用点个赞吧!
更多推荐
所有评论(0)