maven相关依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
具体代码:
package com.senqiangsoft.shineiot.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
/**
* Excel 相关操作类(小数据量写入<=65536)
*/
public class ExcelUtils {
private static final int DEFAULT_COLUMN_SIZE = 30;
/**
* 断言Excel文件写入之前的条件
*
* @param directory 目录
* @param fileName 文件名
* @return file
* @throws IOException
*/
private static File assertFile(String directory, String fileName) throws IOException {
//File tmpFile = new File(directory + File.separator + fileName + ".xls");
/*if (tmpFile.exists()) {
if (tmpFile.isDirectory()) {
throw new IOException("File '" + tmpFile + "' exists but is a directory");
}
if (!tmpFile.canWrite()) {
throw new IOException("File '" + tmpFile + "' cannot be written to");
}
} else {
File parent = tmpFile.getParentFile();
if (parent != null) {
if (!parent.mkdirs() && !parent.isDirectory()) {
throw new IOException("Directory '" + parent + "' could not be created");
}
}
}*/
// return tmpFile;
File file=null;
if(directory!=null&&fileName!=null) {
file=new File(directory,fileName);
File parentFile=file.getParentFile();
if(!parentFile.exists()) {
parentFile.mkdirs();
}
}
return file;
}
public File savePath() {
String savaPath="D://repertoryDownLoad";
Date now =new Date();
File file=new File(savaPath,now.getTime()+".xls");
if(!file.getParentFile().exists()) {//如果顶级目录不存在
file.mkdirs();
}
if(file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
/**
* 日期转化为字符串,格式为yyyy-MM-dd HH:mm:ss
*/
private static String getCnDate(Date date) {
String format = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}
/**
* Excel 导出,POI实现
*
* @param fileName 文件名
* @param sheetName sheet页名称
* @param columnNames 表头列表名
* @param sheetTitle sheet页Title
* @param objects 目标数据集
*/
public static File writeExcel(String directory, String fileName, String sheetName, List<String> columnNames,
String sheetTitle, List<List<Object>> objects, boolean append) throws Exception {
File tmpFile = assertFile(directory, fileName);
return exportExcel(tmpFile, sheetName, columnNames, sheetTitle, objects, append);
}
/**
* Excel 导出,POI实现,先写入Excel标题,与writeExcelData配合使用
* 先使用writeExcelTitle再使用writeExcelData
*
* @param directory 目录
* @param fileName 文件名
* @param sheetName sheetName
* @param columnNames 列名集合
* @param sheetTitle 表格标题
* @param append 是否在现有的文件追加
* @return file
* @throws IOException
*/
public static File writeExcelTitle(String directory, String fileName, String sheetName, List<String> columnNames,
String sheetTitle, boolean append) throws Exception {
File tmpFile = assertFile(directory, fileName);
return exportExcelTitle(tmpFile, sheetName, columnNames, sheetTitle, append);
}
/**
* Excel 导出,POI实现,写入Excel数据行列,与writeExcelTitle配合使用
* 先使用writeExcelTitle再使用writeExcelData
*
* @param directory 目录
* @param fileName 文件名
* @param sheetName sheetName
* @param objects 数据信息
* @return file
* @throws IOException
*/
public static File writeExcelData(String directory, String fileName, String sheetName, List<List<Object>> objects)
throws Exception {
File tmpFile = assertFile(directory, fileName);
return exportExcelData(tmpFile, sheetName, objects);
}
/**
* 导出字符串数据
*
* @param file 文件名
* @param columnNames 表头
* @param sheetTitle sheet页Title
* @param append 是否追加写文件
* @return file
*/
private static File exportExcelTitle(File file, String sheetName, List<String> columnNames,
String sheetTitle, boolean append) throws Exception {
// 声明一个工作薄
Workbook workBook;
if (file.exists() && append) {
// 声明一个工作薄
workBook = new HSSFWorkbook(new FileInputStream(file));
} else {
workBook = new HSSFWorkbook();
}
Map<String, CellStyle> cellStyleMap = styleMap(workBook);
// 表头样式
CellStyle headStyle = cellStyleMap.get("head");
// 生成一个表格
Sheet sheet = workBook.getSheet(sheetName);
if (sheet == null) {
sheet = workBook.createSheet(sheetName);
}
//最新Excel列索引,从0开始
int lastRowIndex = sheet.getLastRowNum();
if (lastRowIndex > 0) {
lastRowIndex++;
}
// 设置表格默认列宽度
sheet.setDefaultColumnWidth(DEFAULT_COLUMN_SIZE);
// 合并单元格
sheet.addMergedRegion(new CellRangeAddress(lastRowIndex, lastRowIndex, 0, columnNames.size() - 1));
// 产生表格标题行
Row rowMerged = sheet.createRow(lastRowIndex);
lastRowIndex++;
Cell mergedCell = rowMerged.createCell(0);
mergedCell.setCellStyle(headStyle);
mergedCell.setCellValue(new HSSFRichTextString(sheetTitle));
// 产生表格表头列标题行
Row row = sheet.createRow(lastRowIndex);
for (int i = 0; i < columnNames.size(); i++) {
Cell cell = row.createCell(i);
cell.setCellStyle(headStyle);
RichTextString text = new HSSFRichTextString(columnNames.get(i));
cell.setCellValue(text);
}
try {
OutputStream ops = new FileOutputStream(file);
workBook.write(ops);
ops.flush();
ops.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return file;
}
/**
* 导出字符串数据
*
* @param file 文件名
* @param objects 目标数据
* @return
*/
private static File exportExcelData(File file, String sheetName, List<List<Object>> objects) throws Exception {
// 声明一个工作薄
Workbook workBook;
if (file.exists()) {
// 声明一个工作薄
workBook = new HSSFWorkbook(new FileInputStream(file));
} else {
workBook = new HSSFWorkbook();
}
Map<String, CellStyle> cellStyleMap = styleMap(workBook);
// 正文样式
CellStyle contentStyle = cellStyleMap.get("content");
//正文整数样式
CellStyle contentIntegerStyle = cellStyleMap.get("integer");
//正文带小数整数样式
CellStyle contentDoubleStyle = cellStyleMap.get("double");
// 生成一个表格
Sheet sheet = workBook.getSheet(sheetName);
if (sheet == null) {
sheet = workBook.createSheet(sheetName);
}
//最新Excel列索引,从0开始
int lastRowIndex = sheet.getLastRowNum();
if (lastRowIndex > 0) {
lastRowIndex++;
}
// 设置表格默认列宽度
sheet.setDefaultColumnWidth(DEFAULT_COLUMN_SIZE);
// 遍历集合数据,产生数据行,前两行为标题行与表头行
for (List<Object> dataRow : objects) {
Row row = sheet.createRow(lastRowIndex);
lastRowIndex++;
for (int j = 0; j < dataRow.size(); j++) {
Cell contentCell = row.createCell(j);
Object dataObject = dataRow.get(j);
if (dataObject != null) {
if (dataObject instanceof Integer) {
contentCell.setCellStyle(contentIntegerStyle);
contentCell.setCellValue(Integer.parseInt(dataObject.toString()));
} else if (dataObject instanceof Double) {
contentCell.setCellStyle(contentDoubleStyle);
contentCell.setCellValue(Double.parseDouble(dataObject.toString()));
} else if (dataObject instanceof Long && dataObject.toString().length() == 13) {
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(getCnDate(new Date(Long.parseLong(dataObject.toString()))));
} else if (dataObject instanceof Date) {
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(getCnDate((Date) dataObject));
} else {
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(dataObject.toString());
}
} else {
contentCell.setCellStyle(contentStyle);
// 设置单元格内容为字符型
contentCell.setCellValue("");
}
}
}
try {
OutputStream ops = new FileOutputStream(file);
workBook.write(ops);
ops.flush();
ops.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return file;
}
/**
* 导出字符串数据
*
* @param file 文件名
* @param columnNames 表头
* @param sheetTitle sheet页Title
* @param objects 目标数据
* @param append 是否追加写文件
* @return
* @throws RuntimeException
*/
private static File exportExcel(File file, String sheetName, List<String> columnNames,
String sheetTitle, List<List<Object>> objects, boolean append) throws RuntimeException, IOException {
// 声明一个工作薄
Workbook workBook;
if (file.exists() && append) {
// 声明一个工作薄
workBook = new HSSFWorkbook(new FileInputStream(file));
} else {
workBook = new HSSFWorkbook();
}
Map<String, CellStyle> cellStyleMap = styleMap(workBook);
// 表头样式
CellStyle headStyle = cellStyleMap.get("head");
// 正文样式
CellStyle contentStyle = cellStyleMap.get("content");
//正文整数样式
CellStyle contentIntegerStyle = cellStyleMap.get("integer");
//正文带小数整数样式
CellStyle contentDoubleStyle = cellStyleMap.get("double");
// 生成一个表格
Sheet sheet = workBook.getSheet(sheetName);
if (sheet == null) {
sheet = workBook.createSheet(sheetName);
}
//最新Excel列索引,从0开始
int lastRowIndex = sheet.getLastRowNum();
if (lastRowIndex > 0) {
lastRowIndex++;
}
// 设置表格默认列宽度
sheet.setDefaultColumnWidth(DEFAULT_COLUMN_SIZE);
// 合并单元格
sheet.addMergedRegion(new CellRangeAddress(lastRowIndex, lastRowIndex, 0, columnNames.size() - 1));
// 产生表格标题行
Row rowMerged = sheet.createRow(lastRowIndex);
lastRowIndex++;
Cell mergedCell = rowMerged.createCell(0);
mergedCell.setCellStyle(headStyle);
mergedCell.setCellValue(new HSSFRichTextString(sheetTitle));
// 产生表格表头列标题行
Row row = sheet.createRow(lastRowIndex);
lastRowIndex++;
for (int i = 0; i < columnNames.size(); i++) {
Cell cell = row.createCell(i);
cell.setCellStyle(headStyle);
RichTextString text = new HSSFRichTextString(columnNames.get(i));
cell.setCellValue(text);
}
// 遍历集合数据,产生数据行,前两行为标题行与表头行
for (List<Object> dataRow : objects) {
row = sheet.createRow(lastRowIndex);
lastRowIndex++;
for (int j = 0; j < dataRow.size(); j++) {
Cell contentCell = row.createCell(j);
Object dataObject = dataRow.get(j);
if (dataObject != null) {
if (dataObject instanceof Integer) {
contentCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
contentCell.setCellStyle(contentIntegerStyle);
contentCell.setCellValue(Integer.parseInt(dataObject.toString()));
} else if (dataObject instanceof Double) {
contentCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
contentCell.setCellStyle(contentDoubleStyle);
contentCell.setCellValue(Double.parseDouble(dataObject.toString()));
} else if (dataObject instanceof Long && dataObject.toString().length() == 13) {
contentCell.setCellType(HSSFCell.CELL_TYPE_STRING);
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(getCnDate(new Date(Long.parseLong(dataObject.toString()))));
} else if (dataObject instanceof Date) {
contentCell.setCellType(HSSFCell.CELL_TYPE_STRING);
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(getCnDate((Date) dataObject));
}else if(dataObject instanceof Long) {
contentCell.setCellType(HSSFCell.CELL_TYPE_STRING);
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(dataObject.toString());
}
else {
contentCell.setCellType(HSSFCell.CELL_TYPE_STRING);
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(dataObject.toString());
}
} else {
contentCell.setCellStyle(contentStyle);
// 设置单元格内容为字符型
contentCell.setCellValue("");
}
}
}
try {
OutputStream ops = new FileOutputStream(file);
workBook.write(ops);
ops.flush();
ops.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return file;
}
/**
* 创建单元格表头样式
*
* @param workbook 工作薄
*/
private static CellStyle createCellHeadStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
// 设置边框样式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置对齐样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成字体
Font font = workbook.createFont();
// 表头样式
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
return style;
}
/**
* 创建单元格正文样式
*
* @param workbook 工作薄
*/
private static CellStyle createCellContentStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
// 设置边框样式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置对齐样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成字体
Font font = workbook.createFont();
// 正文样式
style.setFillPattern(HSSFCellStyle.NO_FILL);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style.setFont(font);
return style;
}
/**
* 单元格样式(Integer)列表
*/
private static CellStyle createCellContent4IntegerStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
// 设置边框样式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置对齐样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成字体
Font font = workbook.createFont();
// 正文样式
style.setFillPattern(HSSFCellStyle.NO_FILL);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style.setFont(font);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0"));//数据格式只显示整数
return style;
}
/**
* 单元格样式(Double)列表
*/
private static CellStyle createCellContent4DoubleStyle(Workbook workbook) {
CellStyle style = workbook.createCellStyle();
// 设置边框样式
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//设置对齐样式
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成字体
Font font = workbook.createFont();
// 正文样式
style.setFillPattern(HSSFCellStyle.NO_FILL);
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
font.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style.setFont(font);
style.setDataFormat(HSSFDataFormat.getBuiltinFormat("#,##0.00"));//保留两位小数点
return style;
}
/**
* 单元格样式列表
*/
private static Map<String, CellStyle> styleMap(Workbook workbook) {
Map<String, CellStyle> styleMap = new LinkedHashMap<>();
styleMap.put("head", createCellHeadStyle(workbook));
styleMap.put("content", createCellContentStyle(workbook));
styleMap.put("integer", createCellContent4IntegerStyle(workbook));
styleMap.put("double", createCellContent4DoubleStyle(workbook));
return styleMap;
}
public static ResponseEntity download(File file,HttpServletRequest request,HttpServletResponse response) throws IOException {
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
//用来封装响应头的信息
HttpHeaders responseHeaders=new HttpHeaders();
//下载的附件类型
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
String fileName=new String(file.getName().getBytes("utf-8"),"iso-8859-1");
//下载的附件名称,存在数据库中原始的文件名
//String fileName=new String("aop的配置.docx".getBytes("utf-8"),"iso-8859-1");
//告诉响应头这是一个附件
responseHeaders.setContentDispositionFormData("attachment",fileName);
//到目录下找要下载的附件
//String path=req.getServletContext().getRealPath("uploadFiles");
//目录下存在的文件名
//String fname="aop的配置.docx";
//将下载的文件封装流对象
//File file=new File(path,fname);
return new ResponseEntity(FileUtils.readFileToByteArray(file),responseHeaders,HttpStatus.OK);
}
/**
* 多个sheet创建Excel的工具类
* @param directory
* @param fileName
* @return
* @throws IOException
*/
/* public static File writeExcel(String directory, String fileName, String sheetName, List<String> columnNames,
String sheetTitle, List<List<Object>> objects, boolean append) throws Exception {
File tmpFile = assertFile(directory, fileName);
return exportExcel(tmpFile, sheetName, columnNames, sheetTitle, objects, append);
}*/
private static File multipleSheet(String directory, String fileName,List<String> sheetNameList,List<List<String>> columnNamesList,
List<String> sheetTitleList,List<List<List<Object>>> objectList,boolean append) throws IOException {
if(sheetNameList.size()==columnNamesList.size()&&sheetNameList.size()==sheetTitleList.size()&&sheetNameList.size()==objectList.size()) {
File file = assertFile(directory, fileName);
// 声明一个工作薄
Workbook workBook;
if (file.exists() && append) {
// 声明一个工作薄
workBook = new HSSFWorkbook(new FileInputStream(file));
} else {
workBook = new HSSFWorkbook();
}
Map<String, CellStyle> cellStyleMap = styleMap(workBook);
// 表头样式
CellStyle headStyle = cellStyleMap.get("head");
// 正文样式
CellStyle contentStyle = cellStyleMap.get("content");
//正文整数样式
CellStyle contentIntegerStyle = cellStyleMap.get("integer");
//正文带小数整数样式
CellStyle contentDoubleStyle = cellStyleMap.get("double");
// 生成一个表格
for(int k=0;k<sheetNameList.size();k++) {
String sheetName=sheetNameList.get(k);
List<String> columnNames=columnNamesList.get(k);
String sheetTitle=sheetTitleList.get(k);
List<List<Object>> objects=objectList.get(k);
Sheet sheet = workBook.getSheet(sheetName);
if (sheet == null) {
sheet = workBook.createSheet(sheetName);
}
//最新Excel列索引,从0开始
int lastRowIndex = sheet.getLastRowNum();
if (lastRowIndex > 0) {
lastRowIndex++;
}
// 设置表格默认列宽度
sheet.setDefaultColumnWidth(DEFAULT_COLUMN_SIZE);
// 合并单元格
sheet.addMergedRegion(new CellRangeAddress(lastRowIndex, lastRowIndex, 0, columnNames.size() - 1));
// 产生表格标题行
Row rowMerged = sheet.createRow(lastRowIndex);
lastRowIndex++;
Cell mergedCell = rowMerged.createCell(0);
mergedCell.setCellStyle(headStyle);
mergedCell.setCellValue(new HSSFRichTextString(sheetTitle));
// 产生表格表头列标题行
Row row = sheet.createRow(lastRowIndex);
lastRowIndex++;
for (int i = 0;i<columnNames.size();i++) {
Cell cell = row.createCell(i);
cell.setCellStyle(headStyle);
RichTextString text = new HSSFRichTextString(columnNames.get(i));
cell.setCellValue(text);
}
// 遍历集合数据,产生数据行,前两行为标题行与表头行
for (List<Object> dataRow : objects) {
row = sheet.createRow(lastRowIndex);
lastRowIndex++;
for (int j = 0; j < dataRow.size(); j++) {
Cell contentCell = row.createCell(j);
Object dataObject = dataRow.get(j);
if (dataObject != null) {
if (dataObject instanceof Integer) {
contentCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
contentCell.setCellStyle(contentIntegerStyle);
contentCell.setCellValue(Integer.parseInt(dataObject.toString()));
} else if (dataObject instanceof Double) {
contentCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
contentCell.setCellStyle(contentDoubleStyle);
contentCell.setCellValue(Double.parseDouble(dataObject.toString()));
} else if (dataObject instanceof Long && dataObject.toString().length() == 13) {
contentCell.setCellType(HSSFCell.CELL_TYPE_STRING);
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(getCnDate(new Date(Long.parseLong(dataObject.toString()))));
} else if (dataObject instanceof Date) {
contentCell.setCellType(HSSFCell.CELL_TYPE_STRING);
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(getCnDate((Date) dataObject));
}else if(dataObject instanceof Long) {
contentCell.setCellType(HSSFCell.CELL_TYPE_STRING);
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(dataObject.toString());
}
else {
contentCell.setCellType(HSSFCell.CELL_TYPE_STRING);
contentCell.setCellStyle(contentStyle);
contentCell.setCellValue(dataObject.toString());
}
} else {
contentCell.setCellStyle(contentStyle);
// 设置单元格内容为字符型
contentCell.setCellValue("");
}
}
}
}
try {
OutputStream ops = new FileOutputStream(file);
workBook.write(ops);
ops.flush();
ops.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return file;
}else {
return null;
}
}
public static void main(String[] args) {
/* private static File multipleSheet(String directory, String fileName,List<String> sheetNameList,List<List<String>> columnNamesList,
List<String> sheetTitleList,List<List<List<Object>>> objectList,boolean append) throws IOException {
}*/
String directory="C://repertoryDownLoad//report";
String fileName=new Date().getTime()+".xls";
List<String> sheetNameList=new ArrayList<String>();
sheetNameList.add("个人信息");
sheetNameList.add("班级信息");
List<List<String>> columnNamesList=new ArrayList<List<String>>();
List<String> list1=new ArrayList<String>();
list1.add("姓名");
list1.add("年龄");
list1.add("身高");
List<String> list2=new ArrayList<String>();
list2.add("代号");
list2.add("名称");
list2.add("位置");
columnNamesList.add(list1);
columnNamesList.add(list2);
List<String> sheetTitleList=new ArrayList<String>();
sheetTitleList.add("个人信息");
sheetTitleList.add("班级信息");
List<List<List<Object>>> objectList=new ArrayList<List<List<Object>>>();
List<List<Object>> listObject1=new ArrayList<List<Object>>();
List<Object> object1=new ArrayList<Object>();
object1.add("帅哥");
object1.add("18");
object1.add("180cm");
List<Object> object2=new ArrayList<Object>();
object2.add("帅哥");
object2.add("18");
object2.add("180cm");
listObject1.add(object1);
listObject1.add(object2);
objectList.add(listObject1);
List<List<Object>> listObject2=new ArrayList<List<Object>>();
List<Object> object3=new ArrayList<Object>();
object3.add("1.1");
object3.add("一年级一班");
object3.add("一楼东");
List<Object> object4=new ArrayList<Object>();
object4.add("2.1");
object4.add("二年级一班");
object4.add("一楼西");
listObject2.add(object3);
listObject2.add(object4);
objectList.add(listObject2);
boolean append=false;
List<List<Object>> list=new ArrayList<List<Object>>();
try {
multipleSheet(directory,fileName,sheetNameList,columnNamesList,sheetTitleList,objectList,append);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}