java后端导出Excel
Posted 十一路客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java后端导出Excel相关的知识,希望对你有一定的参考价值。
1.jxl导出Excel的几种方法
import jxl.CellView;
import jxl.SheetSettings;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.*;
import jxl.format.VerticalAlignment;
import jxl.write.*;
import org.apache.log4j.Logger;
import java.io.File;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.*;
public class ExcelUtil
private static final Logger logger = Logger.getLogger(ExcelUtil.class);
//注:recordList是对象列表
/*
* 导出Excel
* @param recordList:将要导出的数据集合 对象数组
* @param filePath:指定的路径名
* @param out:输出流对象 通过response.getOutputStream()传入
* @param mapFields:导出字段 key:对应实体类字段 value:对应导出表中显示的中文名
* @param colsSize :列宽
* @param sheetName:工作表名称
*/
public static void exportToExcel(List<Object> recordList, String filePath,
OutputStream out, Map<String,String> mapFields, int[] colsSize, String sheetName)
sheetName = (sheetName != null && !sheetName.equals("")) ? sheetName : "sheet1";
WritableWorkbook wook = null; //可写的工作簿对象
Object objClass = null;
try
if(filePath != null && !filePath.equals("")) //文件导出
wook = Workbook.createWorkbook(new File(filePath));
else //jsp页面导出
wook = Workbook.createWorkbook(out);
//设置头部字体格式
WritableFont font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
//应用字体
WritableCellFormat wcfh = new WritableCellFormat(font);
//设置其他样式
//wcfh.setAlignment(Alignment.CENTRE); //水平对齐
wcfh.setAlignment(Alignment.LEFT); //水平对齐
wcfh.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直对齐
wcfh.setBorder(Border.ALL, BorderLineStyle.THIN); //边框
//wcfh.setBackground(Colour.AUTOMATIC); //背景色
wcfh.setWrap(true); //不自动换行
//设置内容日期格式
DateFormat df = new DateFormat("yyyy-MM-dd");
//设置内容字体格式
WritableFont font2 = new WritableFont(WritableFont.TIMES, 10, WritableFont.NO_BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
//应用日期格式
WritableCellFormat wcfc = new WritableCellFormat(font2,df);
//wcfc.setAlignment(Alignment.CENTRE);
wcfc.setAlignment(Alignment.LEFT); //20160420 update
wcfc.setVerticalAlignment(VerticalAlignment.CENTRE);
wcfc.setBorder(Border.ALL, BorderLineStyle.THIN);
wcfc.setWrap(true);//自动换行
//创建工作表
WritableSheet sheet = wook.createSheet(sheetName, 0);
SheetSettings setting = sheet.getSettings();
setting.setVerticalFreeze(1); //冻结窗口头部
Integer colsNum = mapFields.size();
//设置列宽
if (colsSize.length == colsNum)
for (int i = 0; i < colsSize.length; i++)
sheet.setColumnView(i, colsSize[i]);
else
// 设置默认的宽度
for (int i = 0; i < colsNum; i++)
CellView cellView = new CellView();
cellView.setAutosize(true); //设置自动大小
sheet.setColumnView(i, cellView);//根据内容自动设置列宽
int columnIndex = 0; //列索引
List<String> methodNameList = new ArrayList<String>();
if(mapFields != null)
String key = "";
Map<String, Method> getMap = null;
Method method = null;
//开始导出表格头部
for(Iterator<String> i = mapFields.keySet().iterator(); i.hasNext();)
key = i.next();
//应用wcfh样式创建单元格
sheet.addCell(new Label(columnIndex, 0, mapFields.get(key),wcfh));
methodNameList.add(key);
columnIndex++;
if(recordList != null && recordList.size() > 0)
//导出表格内容
for(int i = 0, len = recordList.size(); i<len ; i++)
objClass = recordList.get(i);
getMap = getAllMethod(objClass); //获得对象所有的get方法
//按保存的字段顺序导出内容
for(int j = 0; j < methodNameList.size(); j++)
//根据key获取对应方法
method = getMap.get("GET" + methodNameList.get(j).toString().toUpperCase());
if(method != null)
//从对应的get方法得到返回值
//System.out.println("method: " + method);
String value = "";
//20160304 update
if(method.getGenericReturnType().toString().equals("class java.util.Date"))
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if(method.invoke(objClass, (Object[])null) == null)
value = "";
else
value = formatter.format(method.invoke(objClass, (Object[])null));
else if(method.getGenericReturnType().toString().equals("class java.sql.Date")) //20160421 update
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
if(method.invoke(objClass, (Object[])null) == null)
value = "";
else
value = formatter.format(method.invoke(objClass, (Object[])null));
else
if(method.invoke(objClass, (Object[])null) == null)
value = "";
else
value = method.invoke(objClass, (Object[])null).toString();
//应用wcfc样式创建单元格
sheet.addCell(new Label(j, i+1, value, wcfc));
else
sheet.addCell(new Label(j, i+1, "", wcfc));
//内容导出里层for循环结束
//内容导出外层for循环结束
//recordList不为空
wook.write();
System.out.println("导出Excel成功");
/*else //字段参数为null
throw new Exception("传入参数不合法");
*/
catch(Exception e)
e.printStackTrace();
finally
try
if(wook != null)
wook.close();
if(out != null)
out.flush();
out.close();
catch(Exception e2)
e2.printStackTrace();
//finally结束
/**
* 获取类的所有get方法
* @param obj
* @return 类中所有的get方法
*/
public static HashMap<String, Method> getAllMethod(Object obj) throws Exception
HashMap<String, Method> map = new HashMap<String, Method>();
Method[] methods = obj.getClass().getMethods();
String methodName = "";
for(int i = 0; i < methods.length; i++)
methodName = methods[i].getName().toUpperCase();
if(methodName.startsWith("GET"))
map.put(methodName, methods[i]);
return map;
/**将对象列表转化为二维数据列表
* @param recordList 对象列表
* @param methodNameList 需要放入数据列表中的字段名
* */
public static List<Object> convertObjectListToDataList(List recordList,List<String> methodNameList) throws Exception
List<Object> dataList = new ArrayList<Object>();
Map<String, Method> getMap = null;
Method method = null;
if (recordList != null && recordList.size() > 0)
//导出表格内容
for (int i = 0, len = recordList.size(); i < len; i++)
List<Object> tempList = new ArrayList<Object>();
Object temp = recordList.get(i);
getMap = getAllMethod(temp); //获得对象所有的get方法
//按保存的字段顺序导出内容
for (int j = 0; j < methodNameList.size(); j++)
//根据key获取对应方法
method = getMap.get("GET" + methodNameList.get(j).toString().toUpperCase());
if (method != null)
//从对应的get方法得到返回值
//System.out.println("method: " + method);
String value = "";
//20160304 update
if (method.getGenericReturnType().toString().equals("class java.util.Date"))
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (method.invoke(temp, (Object[]) null) == null)
value = "";
else
value = formatter.format(method.invoke(temp, (Object[]) null));
else if (method.getGenericReturnType().toString().equals("class java.sql.Date")) //20160421 update
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
if (method.invoke(temp, (Object[]) null) == null)
value = "";
else
value = formatter.format(method.invoke(temp, (Object[]) null));
else if (method.getGenericReturnType().toString().equals("class java.sql.Timestamp")) //20160421 update
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
if (method.invoke(temp, (Object[]) null) == null)
value = "";
else
value = formatter.format(method.invoke(temp, (Object[]) null));
else
if (method.invoke(temp, (Object[]) null) == null)
value = "";
else
value = method.invoke(temp, (Object[]) null).toString();
//应用wcfc样式创建单元格
if(value.equals(""))
tempList.add("\\\\");
else
tempList.add(value);
else
tempList.add("\\\\");
//内容导出里层for循环结束
dataList.add(tempList);
//内容导出外层for循环结束
//recordList不为空
return dataList;
/*public static void main(String[] args) throws Exception
SendTaskInnerQuery obj = new SendTaskInnerQuery();
getAllMethod(obj);
*/
/**
* 根据指定路径导出Excel
* @param list
* @param filePath
* @param mapFields
* @param sheetName
*/
public static void ImportExcel(List list,String filePath,Map<String, String> mapFields,int[] colsSize,String sheetName)
exportToExcel(list,filePath,null,mapFields,colsSize,sheetName);
/**
* 从Jsp页面导出Excel
* @param list
* @param out
* @param mapFields
* @param colsSize
* @param sheetName
*/
public static void ImportExcel(List list,OutputStream out,Map<String, String> mapFields,int[] colsSize, String sheetName)
exportToExcel(list,null,out,mapFields,colsSize,sheetName);
//注:recordList是列表的列表,相当于一个二维数组
/**
* 导出Excel
* @param recordList:将要导出的数据集合 二维数组
* @param filePath:指定的路径名
* @param out:输出流对象 通过response.getOutputStream()传入
* @param mapFields:导出字段 key:对应实体类字段 value:对应导出表中显示的中文名
* @param colsSize :列宽
* @param sheetName:工作表名称
*/
public static void exportToExcel2(List<Object> recordList, String filePath,
OutputStream out, Map<String,String> mapFields, int[] colsSize, String sheetName)
sheetName = (sheetName != null && !sheetName.equals("")) ? sheetName : "sheet1";
WritableWorkbook wook = null; //可写的工作簿对象
List<Object> objClass = null;
try
if(filePath != null && !filePath.equals("")) //文件导出
wook = Workbook.createWorkbook(new File(filePath));
else //jsp页面导出
wook = Workbook.createWorkbook(out);
//设置头部字体格式
WritableFont font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
//应用字体
WritableCellFormat wcfh = new WritableCellFormat(font);
//设置其他样式
wcfh.setAlignment(Alignment.CENTRE); //水平对齐
wcfh.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直对齐
wcfh.setBorder(Border.ALL, BorderLineStyle.THIN); //边框
//wcfh.setBackground(Colour.AUTOMATIC); //背景色
wcfh.setWrap(true); //不自动换行
//设置内容日期格式
DateFormat df = new DateFormat("yyyy-MM-dd");
//设置内容字体格式
WritableFont font2 = new WritableFont(WritableFont.TIMES, 10, WritableFont.NO_BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
//应用日期格式
WritableCellFormat wcfc = new WritableCellFormat(font2,df);
wcfc.setAlignment(Alignment.LEFT);
wcfc.setVerticalAlignment(VerticalAlignment.CENTRE);
wcfc.setBorder(Border.ALL, BorderLineStyle.THIN);
wcfc.setWrap(true);//自动换行
//创建工作表
WritableSheet sheet = wook.createSheet(sheetName, 0);
SheetSettings setting = sheet.getSettings();
setting.setVerticalFreeze(1); //冻结窗口头部
Integer colsNum = mapFields.size();
//设置列宽
if (colsSize.length == colsNum)
for (int i = 0; i < colsSize.length; i++)
sheet.setColumnView(i, colsSize[i]);
else
// 设置默认的宽度
for (int i = 0; i < colsNum; i++)
CellView cellView = new CellView();
cellView.setAutosize(true); //设置自动大小
sheet.setColumnView(i, cellView);//根据内容自动设置列宽
int columnIndex = 0; //列索引
List<String> methodNameList = new ArrayList<String>();
if(mapFields != null)
String key = "";
Map<String, Method> getMap = null;
Method method = null;
//开始导出表格头部
for(Iterator<String> i = mapFields.keySet().iterator(); i.hasNext();)
key = i.next();
//应用wcfh样式创建单元格
sheet.addCell(new Label(columnIndex, 0, mapFields.get(key),wcfh));
methodNameList.add(key);
columnIndex++;
if(recordList != null && recordList.size() > 0)
//导出表格内容
System.out.println("recordList.size():" + recordList.size());
System.out.println("recordList.get(0).size():" + ((List)recordList.get(0)).size());
for(int i = 0, len = recordList.size(); i<len ; i++)
objClass =(List) recordList.get(i);
//System.out.println("objClass.size():" + objClass.size());
//按保存的字段顺序导出内容
for(int j = 0; j < objClass.size(); j++)
//根据key获取对应方法
//String value = (String)objClass.get(j);
String value = String.valueOf(objClass.get(j));
//应用wcfc样式创建单元格
sheet.addCell(new Label(j, i+1, value, wcfc));
//内容导出里层for循环结束
//内容导出外层for循环结束
//recordList不为空
wook.write();
System.out.println("导出Excel成功");
/*else //字段参数为null
throw new Exception("传入参数不合法");
*/
catch(Exception e)
e.printStackTrace();
finally
try
if(wook != null)
wook.close();
if(out != null)
out.flush();
out.close();
catch(Exception e2)
e2.printStackTrace();
//finally结束
/**
* 根据指定路径导出Excel
* @param list
* @param filePath
* @param mapFields
* @param sheetName
*/
public static void ImportExcel2(List list,String filePath,Map<String, String> mapFields,int[] colsSize,String sheetName)
exportToExcel2(list,filePath,null,mapFields,colsSize,sheetName);
/**
* 从Jsp页面导出Excel
* @param list
* @param out
* @param mapFields
* @param sheetName
*/
public static void ImportExcel2(List list,OutputStream out,Map<String, String> mapFields,int[] colsSize,String sheetName)
exportToExcel2(list,null,out,mapFields,colsSize,sheetName);
//注:recordList是列表的列表,相当于一个二维数组; 标题行可以是多行
/**
* 导出Excel
* @param recordList:将要导出的数据集合 二维数组
* @param filePath:指定的路径名
* @param out:输出流对象 通过response.getOutputStream()传入
* @param mapFields:二维数组 一维的大小是标题行的行数 二维的大小是标题行的列数
* @param colsSize :列宽
* @param sheetName:工作表名称
* @param mergePosList 二维数组 每个一维包含 合并单元格的列表坐标 起始列 起始行 结束列 结束行
*/
public static void exportToExcel3(List<Object> recordList, String filePath,
OutputStream out, List<Object> mapFields, int[] colsSize, String sheetName, List<Object> mergePosList)
logger.info("filePath: " + filePath);
sheetName = (sheetName != null && !sheetName.equals("")) ? sheetName : "sheet1";
WritableWorkbook wook = null; //可写的工作簿对象
List<Object> objClass = null;
int titleRowNum = 0;
if(mapFields != null)
titleRowNum = mapFields.size();
try
if(filePath != null && !filePath.equals("")) //文件导出
wook = Workbook.createWorkbook(new File(filePath));
else //jsp页面导出
wook = Workbook.createWorkbook(out);
//设置头部字体格式
WritableFont font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
//应用字体
WritableCellFormat wcfh = new WritableCellFormat(font);
//设置其他样式
wcfh.setAlignment(Alignment.CENTRE); //水平对齐
wcfh.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直对齐
wcfh.setBorder(Border.ALL, BorderLineStyle.THIN); //边框
//wcfh.setBackground(Colour.AUTOMATIC); //背景色
wcfh.setWrap(true); //自动换行
//设置内容日期格式
DateFormat df = new DateFormat("yyyy-MM-dd");
//应用日期格式
//设置内容字体格式
WritableFont font2 = new WritableFont(WritableFont.TIMES, 10, WritableFont.NO_BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat wcfc = new WritableCellFormat(font2,df);
wcfc.setAlignment(Alignment.LEFT);
wcfc.setVerticalAlignment(VerticalAlignment.CENTRE);
wcfc.setBorder(Border.ALL, BorderLineStyle.THIN);
wcfc.setWrap(true);
//创建工作表
WritableSheet sheet = wook.createSheet(sheetName, 0);
SheetSettings setting = sheet.getSettings();
setting.setVerticalFreeze(2); //冻结窗口头部
Integer colsNum = 0;
if(mapFields != null && mapFields.size() != 0)
colsNum = ((List)mapFields.get(0)).size();
logger.info("export excel 3 colsNum: " + colsNum);
//设置列宽
if (colsSize.length == colsNum)
for (int i = 0; i < colsSize.length; i++)
sheet.setColumnView(i, colsSize[i]);
else
// 设置默认的宽度
for (int i = 0; i < colsNum; i++)
CellView cellView = new CellView();
cellView.setAutosize(true); //设置自动大小
sheet.setColumnView(i, cellView);//根据内容自动设置列宽
//20170905 add 设置行高
if(recordList != null && recordList.size() > 0)
//标题行
sheet.setRowView(0, 500,false);
for (int i = 0; i < recordList.size(); i++)
sheet.setRowView(i+1, 500,false);
//向Excel中写标题行
if(mapFields != null)
//开始导出表格头部
for(int i = 0; i<mapFields.size(); i++)//标题行
List<Object> tempList = (List<Object>)mapFields.get(i);
for(int j=0; j<tempList.size(); j++)//标题列
//应用wcfh样式创建单元格
sheet.addCell(new Label(j, i, String.valueOf(tempList.get(j)),wcfh));
//合并标题行单元格
logger.info("mergePosList.size(): " + mergePosList.size());
for(int i=0; i<mergePosList.size(); i++)
List<Object> tempPosList = (List<Object>)mergePosList.get(i);
sheet.mergeCells((Integer)tempPosList.get(0), (Integer)tempPosList.get(1),
(Integer)tempPosList.get(2), (Integer)tempPosList.get(3));
logger.info("i: " + i + "-- " + (Integer)tempPosList.get(0)+ ","+(Integer)tempPosList.get(1)
+"," + (Integer)tempPosList.get(2) + "," + (Integer)tempPosList.get(3));
//向Excel中写内容行
if(recordList != null && recordList.size() > 0)
//导出表格内容
System.out.println("recordList.size():" + recordList.size());
System.out.println("recordList.get(0).size():" + ((List)recordList.get(0)).size());
for(int i = 0, len = recordList.size(); i<len ; i++)
objClass =(List) recordList.get(i);
//System.out.println("objClass.size():" + objClass.size());
//按保存的字段顺序导出内容
for(int j = 0; j < objClass.size(); j++)
//根据key获取对应方法
//String value = (String)objClass.get(j);
String value = String.valueOf(objClass.get(j));
//应用wcfc样式创建单元格
sheet.addCell(new Label(j, i+titleRowNum, value, wcfc));
//内容导出里层for循环结束
//内容导出外层for循环结束
//recordList不为空
wook.write();
System.out.println("导出Excel成功");
else //字段参数为null
throw new Exception("传入参数不合法");
catch(Exception e)
e.printStackTrace();
finally
try
if(wook != null)
wook.close();
if(out != null)
out.flush();
out.close();
catch(Exception e2)
e2.printStackTrace();
//finally结束
/**
* 根据指定路径导出Excel
* @param list
* @param filePath
* @param mapFields
* @param sheetName
*/
public static void ImportExcel3(List list,String filePath, List<Object> mapFields,int[] colsSize,String sheetName,List<Object> mergePosList)
exportToExcel3(list,filePath,null,mapFields,colsSize,sheetName,mergePosList);
/**
* 从Jsp页面导出Excel
* @param list
* @param out
* @param mapFields
* @param sheetName
*/
public static void ImportExcel3(List list,OutputStream out, List<Object> mapFields,int[] colsSize,String sheetName,List<Object> mergePosList)
exportToExcel3(list,null,out,mapFields,colsSize,sheetName,mergePosList);
/**
* @param wook 工作簿
* @param index sheet索引
* */
//20160214 添加可以导出多个sheet的excel方法
public static void exportToExcelSheet(List<Object> recordList, String filePath,
OutputStream out, Map<String,String> mapFields, int[] colsSize, String sheetName,WritableWorkbook wook,int index) throws Exception
sheetName = (sheetName != null && !sheetName.equals("")) ? sheetName : "sheet1";
Object objClass = null;
//设置头部字体格式
WritableFont font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
//应用字体
WritableCellFormat wcfh = new WritableCellFormat(font);
//设置其他样式
wcfh.setAlignment(Alignment.CENTRE); //水平对齐
wcfh.setVerticalAlignment(VerticalAlignment.CENTRE); //垂直对齐
wcfh.setBorder(Border.ALL, BorderLineStyle.THIN); //边框
//wcfh.setBackground(Colour.AUTOMATIC); //背景色
wcfh.setWrap(true); //不自动换行
//设置内容日期格式
DateFormat df = new DateFormat("yyyy-MM-dd");
//设置内容字体格式
WritableFont font2 = new WritableFont(WritableFont.TIMES, 10, WritableFont.NO_BOLD,
false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
//应用日期格式
WritableCellFormat wcfc = new WritableCellFormat(font2,df);
wcfc.setAlignment(Alignment.LEFT);
wcfc.setVerticalAlignment(VerticalAlignment.CENTRE);
wcfc.setBorder(Border.ALL, BorderLineStyle.THIN);
wcfc.setWrap(true);//自动换行
//创建工作表
WritableSheet sheet = wook.createSheet(sheetName, index);//index是sheet的索引
SheetSettings setting = sheet.getSettings();
setting.setVerticalFreeze(1); //冻结窗口头部
Integer colsNum = mapFields.size();
//设置列宽
if (colsSize.length == colsNum)
for (int i = 0; i < colsSize.length; i++)
sheet.setColumnView(i, colsSize[i]);
else
// 设置默认的宽度
for (int i = 0; i < colsNum; i++)
CellView cellView = new CellView();
cellView.setAutosize(true); //设置自动大小
sheet.setColumnView(i, cellView);//根据内容自动设置列宽
int columnIndex = 0; //列索引
List<String> methodNameList = new ArrayList<String>();
if(mapFields != null)
String key = "";
Map<String, Method> getMap = null;
Method method = null;
//开始导出表格头部
for(Iterator<String> i = mapFields.keySet().iterator(); i.hasNext();)
key = i.next();
//应用wcfh样式创建单元格
sheet.addCell(new Label(columnIndex, 0, mapFields.get(key),wcfh));
methodNameList.add(key);
columnIndex++;
if(recordList != null && recordList.size() > 0)
//导出表格内容
for(int i = 0, len = recordList.size(); i<len ; i++)
objClass = recordList.get(i);
getMap = getAllMethod(objClass); //获得对象所有的get方法
//按保存的字段顺序导出内容
for(int j = 0; j < methodNameList.size(); j++)
//根据key获取对应方法
method = getMap.get("GET" + methodNameList.get(j).toString().toUpperCase());
if(method != null)
//从对应的get方法得到返回值
//System.out.println("method: " + method);
String value = "";
if(method.invoke(objClass, (Object[])null) != null)
//20160304 update
if(method.getGenericReturnType().toString().equals("class java.util.Date"))
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if(method.invoke(objClass, (Object[])null) == null)
value = "";
else
value = formatter.format(method.invoke(objClass, (Object[])null));
else
if(method.invoke(objClass, (Object[])null) == null)
value = "";
else
value = method.invoke(objClass, (Object[])null).toString();
//应用wcfc样式创建单元格
sheet.addCell(new Label(j, i+1, value, wcfc));
else
sheet.addCell(new Label(j, i+1, "", wcfc));
//内容导出里层for循环结束
//内容导出外层for循环结束
//recordList不为空
/*else //字段参数为null
throw new Exception("传入参数不合法");
*/
private static void exportToExcel4(List<Object> dataList, String filePath,
OutputStream out, List<Map<String, String>> mapFieldList,
List<int[]> colsSizeList, List<String> sheetNameList)
// TODO Auto-generated method stub
WritableWorkbook wook = null;
try
wook = Workbook.createWorkbook(out); //可写的工作簿对象
for(int i=0; i<sheetNameList.size(); i++)
List<Object> recordList = (List<Object>)dataList.get(i);
Map<String,String> mapFileds = mapFieldList.get(i);
int[] colsSize = colsSizeList.get(i);
String sheetName = sheetNameList.get(i);
logger.info("i - " + i + " : sheetName - " + sheetName);
exportToExcelSheet(recordList,null,out,mapFileds,colsSize,sheetName,wook,i);
wook.write();
System.out.println("导出Excel成功");
catch(Exception e)
e.printStackTrace();
finally
try
if(wook != null)
wook.close();
if(out != null)
out.flush();
out.close();
catch(Exception e2)
e2.printStackTrace();
//finally结束
public static void ImportExcel4(List<Object> dataList,
OutputStream out,
List<Map<String, String>> mapFieldList, List<int[]> colsSizeList,
List<String> sheetNameList)
// TODO Auto-generated method stub
exportToExcel4(dataList,null,out,mapFieldList,colsSizeList,sheetNameList);
2.调用形式
//1.标题
Map<String, String> mapField = new LinkedHashMap<String, String>();
mapField.put("num", "序号");//0
//2.设置列宽
Integer fieldNum = mapField.keySet().size();
int[] colsSize = new int[fieldNum];
for(int i=0; i<fieldNum; i++)
if(i==0 || i==1 || i==2 || i==3)
colsSize[i] = 8;
else if(i==7) //需求名称
colsSize[i] = 16;
else if(i==8 || i==15) //需求描述 验证方案
colsSize[i] = 30;
else
colsSize[i] = 12;
//3.标题行
List<Object> titleList = new ArrayList<Object>();
//第一行
List<Object> tempList1 = new ArrayList<Object>();
tempList1.add("序号"); // 1
//第二行
List<Object> tempList2 = new ArrayList<Object>();
tempList2.add("序号"); // 1
titleList.add(tempList1);
titleList.add(tempList2);
//4.设置合并单元格的列表坐标 起始列 起始行 结束列 结束行
List<Object> mergePosList = new ArrayList<Object>();
List<Object> tempPosList1 = new ArrayList<Object>();
tempPosList1.add(18);
tempPosList1.add(0);
tempPosList1.add(23);
tempPosList1.add(0);
List<Object> tempPosList2 = new ArrayList<Object>();
tempPosList2.add(24);
tempPosList2.add(0);
tempPosList2.add(29);
tempPosList2.add(0);
mergePosList.add(tempPosList1);
mergePosList.add(tempPosList2);
//5.将查询对象列表转化为导出需要的形式
List<Object> dataList = ExcelUtil.convertObjectListToDataList(resultList,keyList);
response.reset();
response.setContentType("application/msexcel");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String nowTime = sdf.format(new Date());
String fileName = nowTime + "_导出列表";
response.setHeader("content-disposition", "attachment;filename=" + new
String(fileName.getBytes("gb2312"),"iso8859-1") + ".xls");
ExcelUtil.ImportExcel3(dataList,response.getOutputStream(), titleList, colsSize, fileName, mergePosList);
以上是关于java后端导出Excel的主要内容,如果未能解决你的问题,请参考以下文章