一、单表头
1、效果图
2、代码
引入jar
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
1)工具类
package com.bootdo.common.utils;
import com.bootdo.biz.domain.ExcelDO;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
import javax.servlet.http.HttpServletResponse;
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
public class ExcelUtils {
/**
* 使用浏览器选择路径下载
* @param response
* @param fileName
* @param data
* @throws Exception
*/
public static void exportExcel(HttpServletResponse response, String fileName, ExcelDO data) throws Exception {
// 告诉浏览器用什么软件可以打开此文件
response.setHeader("content-Type", "application/vnd.ms-excel");
// 下载文件的默认名称
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "utf-8"));
exportExcel(data, response.getOutputStream());
}
public static int generateExcel(ExcelDO ExcelDO, String path) throws Exception {
File f = new File(path);
FileOutputStream out = new FileOutputStream(f);
return exportExcel(ExcelDO, out);
}
private static int exportExcel(ExcelDO data, OutputStream out) throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
int rowIndex = 0;
try {
String sheetName = data.getName();
if (null == sheetName) {
sheetName = "Sheet1";
}
XSSFSheet sheet = wb.createSheet(sheetName);
rowIndex = writeExcel(wb, sheet, data);
wb.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
//此处需要关闭 wb 变量
out.close();
}
return rowIndex;
}
/**
* 表不显示字段
* @param wb
* @param sheet
* @param data
* @return
*/
// private static int writeExcel(XSSFWorkbook wb, Sheet sheet, ExcelDO data) {
// int rowIndex = 0;
// writeTitlesToExcel(wb, sheet, data.getTitles());
// rowIndex = writeRowsToExcel(wb, sheet, data.getRows(), rowIndex);
// autoSizeColumns(sheet, data.getTitles().size() + 1);
// return rowIndex;
// }
/**
* 表显示字段
* @param wb
* @param sheet
* @param data
* @return
*/
private static int writeExcel(XSSFWorkbook wb, Sheet sheet, ExcelDO data) {
int rowIndex = 0;
rowIndex = writeTitlesToExcel(wb, sheet, data.getTitles());
rowIndex = writeRowsToExcel(wb, sheet, data.getRows(), rowIndex);
autoSizeColumns(sheet, data.getTitles().size() + 1);
return rowIndex;
}
/**
* 设置表头
*
* @param wb
* @param sheet
* @param titles
* @return
*/
private static int writeTitlesToExcel(XSSFWorkbook wb, Sheet sheet, List<String> titles) {
int rowIndex = 0;
int colIndex = 0;
Font titleFont = wb.createFont();
//设置字体
titleFont.setFontName("simsun");
//设置粗体
titleFont.setBoldweight(Short.MAX_VALUE);
//设置字号
titleFont.setFontHeightInPoints((short) 14);
//设置颜色
titleFont.setColor(IndexedColors.BLACK.index);
XSSFCellStyle titleStyle = wb.createCellStyle();
//水平居中
titleStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
//垂直居中
titleStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
//设置图案颜色
titleStyle.setFillForegroundColor(new XSSFColor(new Color(182, 184, 192)));
//设置图案样式
titleStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
titleStyle.setFont(titleFont);
setBorder(titleStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
Row titleRow = sheet.createRow(rowIndex);
titleRow.setHeightInPoints(25);
colIndex = 0;
for (String field : titles) {
Cell cell = titleRow.createCell(colIndex);
cell.setCellValue(field);
cell.setCellStyle(titleStyle);
colIndex++;
}
rowIndex++;
return rowIndex;
}
/**
* 设置内容
*
* @param wb
* @param sheet
* @param rows
* @param rowIndex
* @return
*/
private static int writeRowsToExcel(XSSFWorkbook wb, Sheet sheet, List<List<Object>> rows, int rowIndex) {
int colIndex;
Font dataFont = wb.createFont();
dataFont.setFontName("simsun");
dataFont.setFontHeightInPoints((short) 14);
dataFont.setColor(IndexedColors.BLACK.index);
XSSFCellStyle dataStyle = wb.createCellStyle();
dataStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);
dataStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);
dataStyle.setFont(dataFont);
setBorder(dataStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
for (List<Object> rowData : rows) {
Row dataRow = sheet.createRow(rowIndex);
dataRow.setHeightInPoints(25);
colIndex = 0;
for (Object cellData : rowData) {
Cell cell = dataRow.createCell(colIndex);
if (cellData != null) {
cell.setCellValue(cellData.toString());
} else {
cell.setCellValue("");
}
cell.setCellStyle(dataStyle);
colIndex++;
}
rowIndex++;
}
return rowIndex;
}
/**
* 自动调整列宽
*
* @param sheet
* @param columnNumber
*/
private static void autoSizeColumns(Sheet sheet, int columnNumber) {
for (int i = 0; i < columnNumber; i++) {
int orgWidth = sheet.getColumnWidth(i);
sheet.autoSizeColumn(i, true);
int newWidth = (int) (sheet.getColumnWidth(i) + 100);
if (newWidth > orgWidth) {
sheet.setColumnWidth(i, newWidth);
} else {
sheet.setColumnWidth(i, orgWidth);
}
}
}
/**
* 设置边框
*
* @param style
* @param border
* @param color
*/
private static void setBorder(XSSFCellStyle style, BorderStyle border, XSSFColor color) {
style.setBorderTop(border);
style.setBorderLeft(border);
style.setBorderRight(border);
style.setBorderBottom(border);
style.setBorderColor(BorderSide.TOP, color);
style.setBorderColor(BorderSide.LEFT, color);
style.setBorderColor(BorderSide.RIGHT, color);
style.setBorderColor(BorderSide.BOTTOM, color);
}
/**
* 导入数据
* @param inputStream
* @return
*/
public static List<Object[]> importExcel(InputStream inputStream) {
try {
List<Object[]> list = new ArrayList<>();
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheetAt(0);
//获取sheet的行数
int rows = sheet.getPhysicalNumberOfRows();
for (int i = 0; i < rows; i++) {
//过滤表头行
if (i == 0) {
continue;
}
//获取当前行的数据
Row row = sheet.getRow(i);
Object[] objects = new Object[row.getPhysicalNumberOfCells()];
int index = 0;
for (Cell cell : row) {
if(cell.getCellType()==0) {
objects[index] = cell.getNumericCellValue();
}
if(cell.getCellType()==1){
objects[index] = cell.getStringCellValue();
}
index++;
}
list.add(objects);
}
return list;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
实体类
public class ExcelDO implements Serializable {
private static final long serialVersionUID = 6133772627258154184L;
/**
* 表头
*/
private List<String> titles;
/**
* 数据
*/
private List<List<Object>> rows;
/**
* 页签名称
*/
private String name;
public List<String> getTitles() {
return titles;
}
public void setTitles(List<String> titles) {
this.titles = titles;
}
public List<List<Object>> getRows() {
return rows;
}
public void setRows(List<List<Object>> rows) {
this.rows = rows;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2)控制层
@ResponseBody
@RequestMapping(value="/excel")
public void excel(@RequestParam Map<String, Object> params, HttpServletResponse response){
int rowIndex = 0;
ExcelDO data = new ExcelDO();
data.setName("exports");
List<String> titles = new ArrayList();
titles.add("生产日期"); //1
titles.add("入库日期"); //2
titles.add("产品批号"); //3
titles.add("规格(盒/箱)"); //4
titles.add("产品箱数"); //5
titles.add("产品盒数"); //6
titles.add("产品总盒数"); //7
titles.add("破损箱数"); //8
titles.add("破损盒数"); //9
titles.add("破损总盒数"); //10
titles.add("实收箱数"); //11
titles.add("实收盒数"); //12
titles.add("实收总盒数"); //13
titles.add("出库数量"); //14
titles.add("库存盒数"); //15
titles.add("存放地点"); //16
data.setTitles(titles);
List<CInstorageDO> cInstorageList = cInstorageService.list(params);
List<List<Object>> rows = new ArrayList();
for(int i = 0, length = cInstorageList.size();i<length;i++){
CInstorageDO cInstorageDO = cInstorageList.get(i);
List<Object> row = new ArrayList();
row.add(DataCodeUtil.getStringDate(cInstorageDO.getProductDate()));
row.add(DataCodeUtil.getStringDate(cInstorageDO.getInstorageDate()));
row.add(cInstorageDO.getProductSn());
row.add(cInstorageDO.getSpecsNum());
row.add(cInstorageDO.getRecievedPackageNum());
row.add(cInstorageDO.getReceivedBoxNum());
row.add(cInstorageDO.getReceivedTotal());
row.add(cInstorageDO.getDamagedPackageNum());
row.add(cInstorageDO.getDamagedBoxNum());
row.add(cInstorageDO.getDamagedTotal());
row.add(cInstorageDO.getRealPackageNum());
row.add(cInstorageDO.getRealBoxNum());
row.add(cInstorageDO.getRealTotal());
row.add(cInstorageDO.getOutNum());
row.add(cInstorageDO.getStockNum());
row.add(cInstorageDO.getStoragePlace());
rows.add(row);
}
data.setRows(rows);
try{
String fileName ="县级库存";
ExcelUtils.exportExcel(response,fileName,data);
}catch (Exception e){
e.printStackTrace();
}
}
3)前端js
function exports() {
//运算符
var comparisonOperator = $('#comparisonOperator').val();
//库存盒数
var stockNum = $('#stockNum').val();
//生产批号
var productSn = $('#productSn').val();
//生产厂家
var factoryId = $('#factoryId').val();
//入库日期
var instorageDate = $('#instorageDate').val();
location.href = "/yyb/cInstorage/excel?comparisonOperator="+comparisonOperator+"&stockNum="+stockNum+"&productSn"
+productSn+"&factoryId"+factoryId+"&instorageDate"+instorageDate;
}
二、组合表头
1、效果图
2、代码
相比较单表头的,需要增加合并单元格的处理
public class CrossRangeCellMeta {
public CrossRangeCellMeta(int firstRowIndex, int firstColIndex, int rowSpan, int colSpan) {
super();
this.firstRowIndex = firstRowIndex;
this.firstColIndex = firstColIndex;
this.rowSpan = rowSpan;
this.colSpan = colSpan;
}
private int firstRowIndex;
private int firstColIndex;
private int rowSpan;// 跨越行数
private int colSpan;// 跨越列数
public int getFirstRow() {
return firstRowIndex;
}
public int getLastRow() {
return firstRowIndex + rowSpan - 1;
}
public int getFirstCol() {
return firstColIndex;
}
public int getLastCol() {
return firstColIndex + colSpan - 1;
}
public int getColSpan(){
return colSpan;
}
public int getRowSpan() {
return rowSpan;
}
public void setRowSpan(int rowSpan) {
this.rowSpan = rowSpan;
}
public void setColSpan(int colSpan) {
this.colSpan = colSpan;
}
}
public class ExcelPlusDO implements Serializable {
private static final long serialVersionUID = 6133772627258154184L;
/**
* 表头
*/
private List<List<ExcelPlusEntiy>> titles;
/**
* 数据
*/
private List<List<Object>> rows;
/**
* 页签名称
*/
private String name;
public List<List<ExcelPlusEntiy>> getTitles() {
return titles;
}
public void setTitles(List<List<ExcelPlusEntiy>> titles) {
this.titles = titles;
}
public List<List<Object>> getRows() {
return rows;
}
public void setRows(List<List<Object>> rows) {
this.rows = rows;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ExcelPlusEntiy {
// txt+'||'+row+','+col+','+hrt+';;';
private String txt;
private String row;
private String col;
private String hrt; //横竖
public ExcelPlusEntiy(String txt, String row, String col) {
this.txt = txt;
this.row = row;
this.col = col;
this.hrt = "0";
}
public String getTxt() {
return txt;
}
public void setTxt(String txt) {
this.txt = txt;
}
public String getRow() {
return row;
}
public void setRow(String row) {
this.row = row;
}
public String getCol() {
return col;
}
public void setCol(String col) {
this.col = col;
}
public String getHrt() {
return hrt;
}
public void setHrt(String hrt) {
this.hrt = hrt;
}
}
public class ExcelPlusUtils {
/**
* 使用浏览器选择路径下载
* @param response
* @param fileName
* @param data
* @throws Exception
*/
public static void exportExcel(HttpServletResponse response, String fileName, ExcelPlusDO data) throws Exception {
// 告诉浏览器用什么软件可以打开此文件
response.setHeader("content-Type", "application/vnd.ms-excel");
// 下载文件的默认名称
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "utf-8"));
exportExcel(data, response.getOutputStream());
}
public static int generateExcel(ExcelPlusDO ExcelDO, String path) throws Exception {
File f = new File(path);
FileOutputStream out = new FileOutputStream(f);
return exportExcel(ExcelDO, out);
}
private static int exportExcel(ExcelPlusDO data, OutputStream out) throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
int rowIndex = 0;
try {
String sheetName = data.getName();
if (null == sheetName) {
sheetName = "Sheet1";
}
XSSFSheet sheet = wb.createSheet(sheetName);
rowIndex = writeExcel(wb, sheet, data);
wb.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
//此处需要关闭 wb 变量
out.close();
}
return rowIndex;
}
/**
* 表不显示字段
* @param wb
* @param sheet
* @param data
* @return
*/
// private static int writeExcel(XSSFWorkbook wb, Sheet sheet, ExcelDO data) {
// int rowIndex = 0;
// writeTitlesToExcel(wb, sheet, data.getTitles());
// rowIndex = writeRowsToExcel(wb, sheet, data.getRows(), rowIndex);
// autoSizeColumns(sheet, data.getTitles().size() + 1);
// return rowIndex;
// }
/**
* 表显示字段
* @param wb
* @param sheet
* @param data
* @return
*/
private static int writeExcel(XSSFWorkbook wb, Sheet sheet, ExcelPlusDO data) {
int rowIndex = 0;
rowIndex = writeTitlesToExcel(wb, sheet, data.getTitles());
rowIndex = writeRowsToExcel(wb, sheet, data.getRows(), rowIndex);
autoSizeColumns(sheet, data.getTitles().size() + 1);
return rowIndex;
}
/**
* 设置表头
*
* @param wb
* @param sheet
* @param titles
* @return
*/
private static int writeTitlesToExcel(XSSFWorkbook wb, Sheet sheet, List<List<ExcelPlusEntiy>> titles ) {
int rowIndex = 0;
int colIndex = 0;
Font titleFont = wb.createFont();
//设置字体
titleFont.setFontName("simsun");
//设置粗体
titleFont.setBold(true);
//设置字号
titleFont.setFontHeightInPoints((short) 14);
//设置颜色
titleFont.setColor(IndexedColors.BLACK.index);
XSSFCellStyle titleStyle = wb.createCellStyle();
//水平居中
titleStyle.setAlignment(HorizontalAlignment.CENTER);
//垂直居中
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//设置图案颜色
titleStyle.setFillForegroundColor(new XSSFColor(new Color(182, 184, 192)));
//设置图案样式
titleStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
titleStyle.setFont(titleFont);
setBorder(titleStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
List<CrossRangeCellMeta> crossRowEleMetaLs = new ArrayList<CrossRangeCellMeta>();
//遍历出行
for(List<ExcelPlusEntiy> title : titles) {
Row titleRow = sheet.createRow(rowIndex);
makeRowCell(title,rowIndex,titleRow,0,crossRowEleMetaLs,titleStyle);
rowIndex++;
}
for (CrossRangeCellMeta crcm : crossRowEleMetaLs) {
sheet.addMergedRegion(new CellRangeAddress(crcm.getFirstRow(), crcm.getLastRow(), crcm.getFirstCol(), crcm.getLastCkongol()));
setRegionStyle(sheet, new CellRangeAddress(crcm.getFirstRow(), crcm.getLastRow(), crcm.getFirstCol(), crcm.getLastCol()),titleStyle);
}
/*sheet.addMergedRegion(new CellRangeAddress(1, 1, 2, 2));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 3, 3));
sheet.addMergedRegion(new CellRangeAddress(1, 1, 4, 4));*/
return rowIndex;
}
/**
* @Description: 制造行(表头)
* @auther: ke.yang
* @date: 2020/7/22
* @param: [title, rowIndex, row, startCellIndex, crossRowEleMetaLs]
* @return: int
*/
private static int makeRowCell(List<ExcelPlusEntiy> title, int rowIndex, Row row, int startCellIndex,
List<CrossRangeCellMeta> crossRowEleMetaLs,XSSFCellStyle titleStyle) {
int i = startCellIndex;
for (int eleIndex = 0; eleIndex < title.size(); i++, eleIndex++) {
int captureCellSize = getCaptureCellSize(rowIndex, i, crossRowEleMetaLs);
while (captureCellSize > 0) {
for (int j = 0; j < captureCellSize; j++) {// 当前行跨列处理(补单元格)
row.createCell(i);
i++;
}
captureCellSize = getCaptureCellSize(rowIndex, i, crossRowEleMetaLs);
}
String val = title.get(eleIndex).getTxt();
//System.out.println("val:"+val);
Cell c = row.createCell(i);
System.out.println("行:"+rowIndex +" 列:"+ i +" val:"+val);
c.setCellValue(val);
c.setCellStyle(titleStyle);
int rowSpan = NumberUtils.toInt(title.get(eleIndex).getRow(), 1);
int colSpan = NumberUtils.toInt(title.get(eleIndex).getCol(), 1);
if (rowSpan > 1 || colSpan > 1) { // 存在跨行或跨列
crossRowEleMetaLs.add(new CrossRangeCellMeta(rowIndex, i, rowSpan, colSpan));
}
if (colSpan > 1) {// 当前行跨列处理(补单元格)
for (int j = 1; j < colSpan; j++) {
i++;
row.createCell(i);
}
}
}
return i;
}
/**
* 获得因rowSpan占据的单元格
*
* @param rowIndex 行号
* @param colIndex 列号
* @param crossRowEleMetaLs 跨行列元数据
* @return 当前行在某列需要占据单元格
*/
private static int getCaptureCellSize(int rowIndex, int colIndex, List<CrossRangeCellMeta> crossRowEleMetaLs) {
int captureCellSize = 0;
for (CrossRangeCellMeta crossRangeCellMeta : crossRowEleMetaLs) {
if (crossRangeCellMeta.getFirstRow() < rowIndex && crossRangeCellMeta.getLastRow() >= rowIndex) {
if (crossRangeCellMeta.getFirstCol() <= colIndex && crossRangeCellMeta.getLastCol() >= colIndex) {
captureCellSize = crossRangeCellMeta.getLastCol() - colIndex + 1;
}
}
}
return captureCellSize;
}
/**
* 设置合并单元格的边框样式
*
* @param sheet
* @param region
* @param cs
*/
public static void setRegionStyle(Sheet sheet, CellRangeAddress region, XSSFCellStyle titleStyle) {
for (int i = region.getFirstRow(); i <= region.getLastRow(); i++) {
Row row = sheet.getRow(i);
for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) {
Cell cell =row.getCell((short) j);
//不清楚为什么为空
if(cell==null){
continue;
}
cell.setCellStyle(titleStyle);
}
}
}
/**
* 设置内容
*
* @param wb
* @param sheet
* @param rows
* @param rowIndex
* @return
*/
private static int writeRowsToExcel(XSSFWorkbook wb, Sheet sheet, List<List<Object>> rows, int rowIndex) {
int colIndex;
Font dataFont = wb.createFont();
dataFont.setFontName("simsun");
dataFont.setFontHeightInPoints((short) 14);
dataFont.setColor(IndexedColors.BLACK.index);
XSSFCellStyle dataStyle = wb.createCellStyle();
dataStyle.setAlignment(HorizontalAlignment.CENTER);
dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);
dataStyle.setFont(dataFont);
setBorder(dataStyle, BorderStyle.THIN, new XSSFColor(new Color(0, 0, 0)));
for (List<Object> rowData : rows) {
Row dataRow = sheet.createRow(rowIndex);
dataRow.setHeightInPoints(25);
colIndex = 0;
for (Object cellData : rowData) {
Cell cell = dataRow.createCell(colIndex);
if (cellData != null) {
cell.setCellValue(cellData.toString());
} else {
cell.setCellValue("");
}
cell.setCellStyle(dataStyle);
colIndex++;
}
rowIndex++;
}
return rowIndex;
}
/**
* 自动调整列宽
*
* @param sheet
* @param columnNumber
*/
private static void autoSizeColumns(Sheet sheet, int columnNumber) {
for (int i = 0; i < columnNumber; i++) {
int orgWidth = sheet.getColumnWidth(i);
sheet.autoSizeColumn(i, true);
int newWidth = (int) (sheet.getColumnWidth(i) + 100);
if (newWidth > orgWidth) {
sheet.setColumnWidth(i, newWidth);
} else {
sheet.setColumnWidth(i, orgWidth);
}
}
}
/**
* 设置边框
*
* @param style
* @param border
* @param color
*/
private static void setBorder(XSSFCellStyle style, BorderStyle border, XSSFColor color) {
style.setBorderTop(border);
style.setBorderLeft(border);
style.setBorderRight(border);
style.setBorderBottom(border);
style.setBorderColor(BorderSide.TOP, color);
style.setBorderColor(BorderSide.LEFT, color);
style.setBorderColor(BorderSide.RIGHT, color);
style.setBorderColor(BorderSide.BOTTOM, color);
}
/**
* 导入数据
* @param inputStream
* @return
*/
public static List<Object[]> importExcel(InputStream inputStream) {
try {
List<Object[]> list = new ArrayList<>();
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheetAt(0);
//获取sheet的行数
int rows = sheet.getPhysicalNumberOfRows();
for (int i = 0; i < rows; i++) {
//过滤表头行
if (i == 0) {
continue;
}
//获取当前行的数据
Row row = sheet.getRow(i);
Object[] objects = new Object[row.getPhysicalNumberOfCells()];
int index = 0;
for (Cell cell : row) {
if(cell.getCellType()==CellType.NUMERIC) {
objects[index] = cell.getNumericCellValue();
}
if(cell.getCellType()==CellType.STRING){
objects[index] = cell.getStringCellValue();
}
index++;
}
list.add(objects);
}
return list;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
控制层,对于表头,第一个单元格new ExcelPlusEntiy(“列1”,“2”,“1”),第二个参数是占几行,第三个是占几列
@Controller
public class TestController {
@ResponseBody
@RequestMapping(value="/testexcel")
public void excel(@RequestParam Map<String, Object> params, HttpServletResponse response){
int rowIndex = 0;
ExcelPlusDO data = new ExcelPlusDO();
data.setName("exports");
List<List<ExcelPlusEntiy>> titles = new ArrayList();
List<ExcelPlusEntiy> tile1 = new ArrayList();
tile1.add(new ExcelPlusEntiy("列1","2","1"));
tile1.add(new ExcelPlusEntiy("列2","1","1"));
tile1.add(new ExcelPlusEntiy("列3","1","1"));
tile1.add(new ExcelPlusEntiy("列4","1","1"));
List<ExcelPlusEntiy> tile2 = new ArrayList();
tile2.add(new ExcelPlusEntiy("列二","1","1"));
tile2.add(new ExcelPlusEntiy("列三","1","1"));
tile2.add(new ExcelPlusEntiy("列四","1","1"));
titles.add(tile1);
titles.add(tile2);
data.setTitles(titles);
List<List<Object>> rows = new ArrayList();
List<Object> row = new ArrayList();
row.add(1);
row.add(2);
row.add(3);
row.add(4);
rows.add(row);
data.setRows(rows);
try{
String fileName ="123";
ExcelPlusUtils.exportExcel(response,fileName,data);
}catch (Exception e){
e.printStackTrace();
}
}
}
表体和第一个一样,都是单格子。例子里面就不写list了。标题如果也有跨行列的,方法相同。