public class ExcelUtils extends Object
| 构造器和说明 |
|---|
ExcelUtils() |
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
createFreezePane(org.apache.poi.xssf.streaming.SXSSFSheet sxssfSheet,
Integer row)
锁定行(固定表头)
HashMap setPaneMap = new HashMap();
setPaneMap.put(1,3); //第一个表格、第三行开始固定表头
|
static Boolean |
exportForExcel(javax.servlet.http.HttpServletResponse response,
List<List<String[]>> dataLists,
HashMap notBorderMap,
HashMap regionMap,
HashMap columnMap,
HashMap styles,
HashMap paneMap,
String fileName,
String[] sheetName,
String[] labelName,
HashMap rowStyles,
HashMap columnStyles,
HashMap dropDownMap)
功能描述: excel 数据导出、导出模板
更新日志:
1.response.reset();注释掉reset,否在会出现跨域错误。
|
static String |
getCellVal(org.apache.poi.ss.usermodel.Cell cell)
功能描述: 获取Excel单元格中的值并且转换java类型格式
|
static List<List<LinkedHashMap<String,String>>> |
importForExcelData(org.apache.poi.ss.usermodel.Workbook book,
String[] sheetName,
HashMap indexMap,
HashMap continueRowMap)
功能描述:
1.excel 模板数据导入。
|
static boolean |
isBlank(String str)
判断字符串是否为空
源码:只是为了该类不引入其他 jar 包
|
static void |
setColumnWidth(org.apache.poi.xssf.streaming.SXSSFSheet sxssfSheet,
HashMap map)
功能描述: 自定义列宽
使用的方法:
HashMap
|
static void |
setDataValidation(org.apache.poi.xssf.streaming.SXSSFSheet sheet,
List<String[]> dropDownListData,
int dataListSize)
功能描述:下拉列表
|
static void |
setDataValidation(org.apache.poi.xssf.streaming.SXSSFSheet xssfWsheet,
String[] list,
Integer firstRow,
Integer lastRow,
Integer firstCol,
Integer lastCol)
功能描述:下拉列表
|
static void |
setExcelCellStyles(org.apache.poi.ss.usermodel.Cell cell,
org.apache.poi.xssf.streaming.SXSSFWorkbook wb,
org.apache.poi.xssf.streaming.SXSSFRow sxssfRow,
List<Object[]> rowstyleList,
int rowIndex)
自定义某一行、列的样式
|
static void |
setExcelStyles(org.apache.poi.ss.usermodel.Cell cell,
org.apache.poi.xssf.streaming.SXSSFWorkbook wb,
org.apache.poi.xssf.streaming.SXSSFRow sxssfRow,
Integer fontSize,
Boolean bold,
Boolean center,
Boolean isBorder,
Boolean leftBoolean,
Boolean rightBoolean,
Integer fontColor,
Integer height) |
static void |
setExcelStyles(org.apache.poi.ss.usermodel.Cell cell,
org.apache.poi.xssf.streaming.SXSSFWorkbook wb,
org.apache.poi.xssf.streaming.SXSSFRow sxssfRow,
List<List<Object[]>> styles,
int cellIndex,
int rowIndex) |
static void |
setExcelStyles(HashMap notBorderMap,
org.apache.poi.ss.usermodel.Cell cell,
org.apache.poi.xssf.streaming.SXSSFWorkbook wb,
org.apache.poi.xssf.streaming.SXSSFRow sxssfRow,
int k,
int jRow)
所有数据的样式(全加边框、不加粗、12号字体、居中、黑色字体)
|
static void |
setMergedRegion(org.apache.poi.xssf.streaming.SXSSFSheet sheet,
ArrayList<Integer[]> rowColList)
功能描述:
1.excel 合并单元格
参数说明:
List
|
static void |
setMergedRegion(org.apache.poi.xssf.streaming.SXSSFSheet sheet,
int firstRow,
int lastRow,
int firstCol,
int lastCol)
合并单元格
|
public static Boolean exportForExcel(javax.servlet.http.HttpServletResponse response, List<List<String[]>> dataLists, HashMap notBorderMap, HashMap regionMap, HashMap columnMap, HashMap styles, HashMap paneMap, String fileName, String[] sheetName, String[] labelName, HashMap rowStyles, HashMap columnStyles, HashMap dropDownMap)
更新日志: 1.response.reset();注释掉reset,否在会出现跨域错误。 2.新增导出多个单元。 3.poi官方建议大数据量解决方案:SXSSFWorkbook。 4.自定义下拉列表:对每个单元格自定义下拉列表。 5.数据遍历方式换成数组(效率较高)。 6.可提供模板下载。 7.每个表格的大标题[2018-09-14] 8.自定义列宽:对每个单元格自定义列宽[2018-09-18] 9.自定义样式:对每个单元格自定义样式[2018-10-22][2018-10-25修复][2018-11-12添加行高] 10.自定义单元格合并:对每个单元格合并[2018-10-22] 11.固定表头[2018-10-23] 12.自定义样式:单元格自定义某一列或者某一行样式[2018-10-30][2018-11-12添加行高] 13.忽略边框(默认是有边框)[2018-11-15]
版 本: 1.apache poi 3.17 2.apache poi-ooxml 3.17
response - dataLists - 导出的数据(不可为空:如果只有标题就导出模板)sheetName - sheet名称(不可为空)columnMap - 自定义:对每个单元格自定义列宽(可为空)dropDownMap - 自定义:对每个单元格自定义下拉列表(可为空)styles - 自定义:每一个单元格样式(可为空)rowStyles - 自定义:某一行样式(可为空)columnStyles - 自定义:某一列样式(可为空)regionMap - 自定义:单元格合并(可为空)paneMap - 固定表头(可为空)labelName - 每个表格的大标题(可为空)fileName - 文件名称(可为空,默认是:sheet 第一个名称)notBorderMap - 忽略边框(默认是有边框)public static List<List<LinkedHashMap<String,String>>> importForExcelData(org.apache.poi.ss.usermodel.Workbook book, String[] sheetName, HashMap indexMap, HashMap continueRowMap)
更新日志: 1.共用获取Excel表格数据。 2.多单元数据获取。 3.多单元从第几行开始获取数据[2018-09-20] 4.多单元根据那些列为空来忽略行数据[2018-10-22]
版 本: 1.apache poi 3.17 2.apache poi-ooxml 3.17
book - Workbook对象(不可为空)sheetName - 多单元数据获取(不可为空)indexMap - 多单元从第几行开始获取数据,默认从第二行开始获取(可为空,如 hashMapIndex.put(1,3); 第一个表格从第三行开始获取)continueRowMap - 多单元根据那些列为空来忽略行数据(可为空,如 mapContinueRow.put(1,new Integer[]{1, 3}); 第一个表格从1、3列为空就忽略)public static boolean isBlank(String str)
str - public static void createFreezePane(org.apache.poi.xssf.streaming.SXSSFSheet sxssfSheet,
Integer row)
sxssfSheet - row - public static void setColumnWidth(org.apache.poi.xssf.streaming.SXSSFSheet sxssfSheet,
HashMap map)
sxssfSheet - map - public static void setMergedRegion(org.apache.poi.xssf.streaming.SXSSFSheet sheet,
ArrayList<Integer[]> rowColList)
sheet - rowColList - public static void setMergedRegion(org.apache.poi.xssf.streaming.SXSSFSheet sheet,
int firstRow,
int lastRow,
int firstCol,
int lastCol)
sheet - firstRow - lastRow - firstCol - lastCol - public static void setDataValidation(org.apache.poi.xssf.streaming.SXSSFSheet sheet,
List<String[]> dropDownListData,
int dataListSize)
sheet - dropDownListData - dataListSize - 参数说明:
HashMap hashMap = new HashMap();
Listpublic static void setDataValidation(org.apache.poi.xssf.streaming.SXSSFSheet xssfWsheet,
String[] list,
Integer firstRow,
Integer lastRow,
Integer firstCol,
Integer lastCol)
xssfWsheet - list - firstRow - lastRow - firstCol - lastCol - public static void setExcelStyles(HashMap notBorderMap, org.apache.poi.ss.usermodel.Cell cell, org.apache.poi.xssf.streaming.SXSSFWorkbook wb, org.apache.poi.xssf.streaming.SXSSFRow sxssfRow, int k, int jRow)
notBorderMap - 忽略边框cell - wb - sxssfRow - k - jRow - public static void setExcelStyles(org.apache.poi.ss.usermodel.Cell cell,
org.apache.poi.xssf.streaming.SXSSFWorkbook wb,
org.apache.poi.xssf.streaming.SXSSFRow sxssfRow,
Integer fontSize,
Boolean bold,
Boolean center,
Boolean isBorder,
Boolean leftBoolean,
Boolean rightBoolean,
Integer fontColor,
Integer height)
cell - Cell对象。wb - SXSSFWorkbook对象。fontSize - 字体大小。bold - 是否加粗。center - 是否左右上下居中。isBorder - 是否加边框leftBoolean - 左对齐rightBoolean - 右对齐height - 行高public static void setExcelCellStyles(org.apache.poi.ss.usermodel.Cell cell,
org.apache.poi.xssf.streaming.SXSSFWorkbook wb,
org.apache.poi.xssf.streaming.SXSSFRow sxssfRow,
List<Object[]> rowstyleList,
int rowIndex)
cell - wb - rowstyleList - rowIndex - 说明:是否居中?,是否右对齐?,是否左对齐?, 是否加粗?,是否有边框? —— 颜色、字体、行高?
HashMap hashMap = new HashMap();
List list = new ArrayList();
list.add(new Boolean[]{true, false, false, false, true}); //1、样式
list.add(new Integer[]{1, 3}); //2、第几行或者是第几列
list.add(new Integer[]{10,14,null}); //3、颜色值 、字体大小、行高(可不设置)
hashMap.put(1,list); //第一表格public static void setExcelStyles(org.apache.poi.ss.usermodel.Cell cell,
org.apache.poi.xssf.streaming.SXSSFWorkbook wb,
org.apache.poi.xssf.streaming.SXSSFRow sxssfRow,
List<List<Object[]>> styles,
int cellIndex,
int rowIndex)
cell - wb - styles - 是否居中?,是否右对齐?,是否左对齐?, 是否加粗?,是否有边框? —— 颜色、字体、行高?
HashMap cellStyles = new HashMap();
List< Listpublic static String getCellVal(org.apache.poi.ss.usermodel.Cell cell)
cell - Copyright © 2018. All rights reserved.