使用Java編寫(xiě)導(dǎo)出不確定行數(shù)列數(shù)數(shù)據(jù)的工具類(lèi)
前言
這是導(dǎo)出不確定行數(shù)列數(shù)數(shù)據(jù)的導(dǎo)出工具類(lèi),導(dǎo)出的文件直接在瀏覽器下載,直接全部復(fù)制使用,下面有導(dǎo)出的數(shù)據(jù)樣式
一、導(dǎo)出的文件樣式
表頭不是固定的,可以根據(jù)需求,增加表頭和與之對(duì)應(yīng)的列數(shù)據(jù),詳情代碼看下面

二、工具類(lèi)的入?yún)⒃斍?/h2>
這是表頭

這是具體的每一行數(shù)據(jù)

三、具體代碼(直接復(fù)制使用)
這個(gè)是業(yè)務(wù)層 使用可以copy到你的業(yè)務(wù)層方法內(nèi),然后進(jìn)行修修改改。
public void export(ExportExcelDto dto, HttpServletResponse response) {
XSSFWorkbook wb = null;
try {
//假設(shè)這一是查詢(xún)數(shù)據(jù)庫(kù)拿到的數(shù)據(jù)
List<MonitorDataImportQueryVo> vos = new ArrayList<>();
if (CollectionUtils.isEmpty(vos)) return;
// 文件名根據(jù)時(shí)間戳生成
String fileName = DateUtil.currentSeconds() + ".xlsx";
// 表頭
ArrayList<String> header = Lists.newArrayList(READ_HEAD1, READ_HEAD2, READ_HEAD3, READ_HEAD4);
//時(shí)間的動(dòng)態(tài)表頭
List<String> collect = vos.stream()
.map(e -> e.getMonitorTime())
.distinct()
.sorted()
.collect(Collectors.toList());
//放到表頭list后面
header.addAll(collect);
// 創(chuàng)建一個(gè)導(dǎo)出的數(shù)據(jù)list
List<List> dataList = Lists.newArrayList();
// 根據(jù)查詢(xún)的數(shù)據(jù)進(jìn)行分組戰(zhàn)后(根據(jù)你的業(yè)務(wù)去處理)
Map<String, List<MonitorDataImportQueryVo>> map = vos.stream()
.collect(Collectors.groupingBy(e -> e.getMonitorNumber()));
// 遍歷放入導(dǎo)出數(shù)據(jù)list里面(根據(jù)你的業(yè)務(wù)去處理)
for (String s : keySet) {
List<MonitorDataImportQueryVo> singleData = map.get(s);
List<String> data = new ArrayList<>();
data.add(singleData.get(0).getMonitorNumber());
data.add(singleData.get(0).getMileage());
data.add(singleData.get(0).getLink());
data.add(singleData.get(0).getInitialValue());
for (String thisValue : singleData.stream()
.map(e -> e.getThisValue())
.collect(Collectors.toList())) {
data.add(thisValue);
}
dataList.add(data);
}
// 調(diào)用工具類(lèi)
wb = ExportExcelUtil.getXSSFWorkbook("監(jiān)測(cè)數(shù)據(jù)", header, dataList);
ExportExcelUtil.buildExcelDocument(fileName, wb, response);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != wb) {
try {
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
具體的工具類(lèi),無(wú)需修改,也可根據(jù)需求改改
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
/**
* @Description: 導(dǎo)出工具類(lèi)
* @Param:
* @return:
* @Author: 楊永卓
* @Date: 2021/9/26 15:47
*/
public class ExportExcelUtil {
/**
* @Description: [導(dǎo)出不確定行數(shù)列數(shù)監(jiān)測(cè)數(shù)據(jù)]
* @Param: [sheetName:sheet頁(yè)名稱(chēng), headers:第一行表頭, data:list里面是每一行數(shù)據(jù)]
* @return: org.apache.poi.XSSf.usermodel.XSSFWorkbook
* @Author: 楊永卓
* @Date: 2021/9/26 16:11
*/
public static XSSFWorkbook getXSSFWorkbook(String sheetName, List<String> headers, List<List> dataList) {
XSSFWorkbook workbook = new XSSFWorkbook();
//新建工作區(qū)并賦名
XSSFSheet sheet = workbook.createSheet(sheetName);
XSSFRow row = sheet.createRow(0);
//設(shè)置樣式
XSSFCellStyle cellStyle = workbook.createCellStyle();
//靠左
cellStyle.setAlignment(XSSFCellStyle.ALIGN_LEFT);
//垂直居中
cellStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
//設(shè)置邊框
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN);
//還需要設(shè)置其他樣式可在此添加
//找出最大列數(shù):對(duì)數(shù)據(jù)進(jìn)行排序,獲取最長(zhǎng)那一行的size
int max = 0;
for (List l : dataList) {
if (l.size() > max) {
max = l.size();
}
}
XSSFCell cell = null;
//設(shè)置列名
for (int i = 0; i < max; i++) {
cell = row.createCell(i);
cell.setCellValue(headers.get(i));
cell.setCellStyle(cellStyle);
}
for (int i = 0; i < dataList.size(); i++) {
row = sheet.createRow(i + 1);
//以max作判斷-->實(shí)現(xiàn)空的單元格也有邊框
for (int j = 0; j < max; j++) {
cell = row.createCell(j);
cell.setCellStyle(cellStyle);
//防止下標(biāo)越界
if (j < dataList.get(i).size()) {
//單元格數(shù)據(jù)類(lèi)型可自行改變,我這里全部字符串化了
cell.setCellValue(dataList.get(i).get(j).toString());
}
}
}
//列寬自適應(yīng)
for (int columnNum = 0; columnNum <= max; columnNum++) {
int columnWidth = sheet.getColumnWidth(columnNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
XSSFRow currentRow;
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(columnNum) != null) {
XSSFCell currentCell = currentRow.getCell(columnNum);
if (currentCell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
sheet.setColumnWidth(columnNum, columnWidth * 256);
}
return workbook;
}
/**
* @Description: [導(dǎo)出到瀏覽器]
* @Param: [fileName, wb, response]
* @return: void
*
* @Author: 楊永卓
* @Date: 2021/9/26 15:47
*/
public static void buildExcelDocument(String fileName, Workbook wb, HttpServletResponse response) {
try {
// response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
// 定義輸出類(lèi)型
response.setContentType("application/octet-stream");
// 可自行定義編碼格式
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
//清除jsp編譯html文件的空白,防止excel出現(xiàn)空行
response.flushBuffer();
//寫(xiě)出
wb.write(response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
}到此這篇關(guān)于使用Java編寫(xiě)導(dǎo)出不確定行數(shù)列數(shù)數(shù)據(jù)的工具類(lèi)的文章就介紹到這了,更多相關(guān)Java導(dǎo)出數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java使用easyExcel導(dǎo)出excel數(shù)據(jù)案例
- Java大批量導(dǎo)出Excel數(shù)據(jù)的優(yōu)化過(guò)程
- Java樹(shù)形結(jié)構(gòu)數(shù)據(jù)生成導(dǎo)出excel文件方法記錄
- 詳解Java如何實(shí)現(xiàn)百萬(wàn)數(shù)據(jù)excel導(dǎo)出功能
- 使用java實(shí)現(xiàn)百萬(wàn)級(jí)別數(shù)據(jù)導(dǎo)出excel的三種方式
- 使用Java實(shí)現(xiàn)百萬(wàn)Excel數(shù)據(jù)導(dǎo)出
相關(guān)文章
mybatis-flex與springBoot整合的實(shí)現(xiàn)示例
Mybatis-flex提供了簡(jiǎn)單易用的API,開(kāi)發(fā)者只需要簡(jiǎn)單的配置即可使用,本文主要介紹了mybatis-flex與springBoot整合,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
解決springboot+activemq啟動(dòng)報(bào)注解錯(cuò)誤的問(wèn)題
這篇文章主要介紹了解決springboot+activemq啟動(dòng)報(bào)注解錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
IDEA Debug啟動(dòng)tomcat報(bào)60659端口占用錯(cuò)誤的解決
工作中將開(kāi)發(fā)工具由Eclipse轉(zhuǎn)為IntelliJ IDEA,在使用過(guò)程中遇到許多問(wèn)題,其中60659端口占用錯(cuò)誤對(duì)于不熟悉IDEA的開(kāi)發(fā)者來(lái)說(shuō)或許會(huì)比較頭痛,本文就來(lái)解決一下這個(gè)問(wèn)題2018-11-11
SpringBoot進(jìn)行參數(shù)校驗(yàn)的方法詳解
在日常的接口開(kāi)發(fā)中,為了防止非法參數(shù)對(duì)業(yè)務(wù)造成影響,經(jīng)常需要對(duì)接口的參數(shù)進(jìn)行校驗(yàn)。本文通過(guò)示例詳細(xì)講解了SpringBoot如何進(jìn)行參數(shù)校驗(yàn)的,感興趣的可以學(xué)習(xí)一下2022-04-04
java讀取resources文件詳解及實(shí)現(xiàn)代碼
這篇文章主要介紹了java讀取resources文件詳解及實(shí)現(xiàn)代碼的相關(guān)資料,在開(kāi)發(fā)項(xiàng)目的時(shí)候經(jīng)常會(huì)遇到讀取文件夾里面的內(nèi)容,需要的朋友可以參考下2017-07-07
關(guān)于ApplicationContext的三個(gè)常用實(shí)現(xiàn)類(lèi)
這篇文章主要介紹了關(guān)于ApplicationContext的三個(gè)常用實(shí)現(xiàn)類(lèi),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Maven工程搭建spring boot+spring mvc+JPA的示例
本篇文章主要介紹了Maven工程搭建spring boot+spring mvc+JPA的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01

