嘀嘀嘀!让你不在掉头发
相信好多小伙伴都遇到导出excel的时候,要合并单元个
那么它来了 它来了,直接都可以使用
平行丝滑导出,难道不香吗?
话不多说,直接怼
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Pattern;
import com.alibaba.fastjson.JSON;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ExportExcel {
public static int exportToExcelForXlsx(List> objData, String fileName, String sheetName, List columns, List mergeIndex, boolean isTree, HttpServletRequest request, HttpServletResponse response) {
int flag = 0;
Collections.sort(mergeIndex);//将列号排序
// 创建工作薄
XSSFWorkbook wb = new XSSFWorkbook();
// sheet1
XSSFSheet sheet1 = wb.createSheet(sheetName);
//设置样式
XSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);//水平对齐
//表头
sheet1.createFreezePane(0, 1);//冻结表头
XSSFRow sheet1row1 = sheet1.createRow((short) 0);
sheet1row1.setHeight((short) 480);
//写入表头
if (columns != null && columns.size() > 0) {
for (int i = 0; i < columns.size(); i++) {
String column = columns.get(i);
//列
XSSFCell cell = sheet1row1.createCell(i);
cell.setCellValue(column);
}
}
int dataSatrtIndex = 1;//数据开始行
boolean isMerge = false;
if (mergeIndex != null && mergeIndex.size() != 0) {
isMerge = true;
}
//写入数据
if (objData != null && objData.size() > 0) {
Map poiModels = new HashMap();
//循环写入表中数据
int i = 0;
for (; i < objData.size(); i++) {
//数据行
XSSFRow row = sheet1.createRow((short) (i + dataSatrtIndex));
//行内循环,既单元格(列)
List
调用层代码
估计但凡有点经验的都能看懂,不做任何解释了
导出结果
原文链接 https://www.cnblogs.com/wdk2020/p/13204162.html
在最黑暗的那段人生,是我自己把自己拉出深渊。没有那个人,我就做那个人