Excel 导入导出(poi)
1、引入org.apache.poi包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
2、springMVC.xml中需要配置支持上传操作
<!-- 配置文件上传,如果没有使用文件上传可以不用配置,当然如果不配,那么配置文件中也不必引入上传组件包 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 默认编码 -->
<property name="defaultEncoding" value="utf-8" />
<!-- 文件大小最大值 -->
<property name="maxUploadSize" value="10485760000" />
<!-- 内存中的最大值 -->
<property name="maxInMemorySize" value="40960" />
</bean>
2、页面部分
<form action="<%=request.getContextPath()%>/operate/upload" id="fileForm" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="file" class="file" >
<button type="submit">上传</button>
</form>
1、文件上传操作一定得加入 enctype="multipart/form-data"
3、后台controller部分
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String uploadFile(@RequestParam("file") CommonsMultipartFile file,
HttpServletRequest request) {
// 判断文件是否为空
if (!file.isEmpty()) {
try {
// 文件保存路径
String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/";
// 转存文件
//file.transferTo(new File(filePath));
//操作解析excelutils
excelUtils.readExcel(file.getOriginalFilename(),file,filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
return "redirect:/login/goLogin";
}
4、上传解析Excel部分
4.1、自动识别读取方式
/**
* 合并方法,读取excel文件
* 根据文件名自动识别读取方式
* 支持97-2013格式的excel文档
* @param fileName
* 上传文件名
* @param file
* 上传的文件
*/
public static List<Map> readExcel(String fileName,MultipartFile file) throws Exception{
//准备返回值列表
List<Map> valueList=new ArrayList<Map>();
String filepathtemp="/mnt/b2b/tmp";//缓存文件目录
String tmpFileName= System.currentTimeMillis()+"."+getExtensionName(fileName);
String ExtensionName=getExtensionName(fileName);
File filelist = new File(filepathtemp);
if (!filelist .exists() && !filelist .isDirectory())
{
filelist .mkdir();
}
String filePath = filepathtemp+System.getProperty("file.separator")+tmpFileName;
File tmpfile = new File(filePath);
//拷贝文件到服务器缓存目录(在项目下) // copy(file,tmpfile); //stuts用的方法
copy(file, filepathtemp,tmpFileName);//spring mvc用的方法
//System.out.println("后缀名:"+ExtensionName);
if(ExtensionName.equalsIgnoreCase("xls")){
valueList=readExcel2003(filePath);
}else if(ExtensionName.equalsIgnoreCase("xlsx")) {
valueList=readExcel2007(filePath);
}
//删除缓存文件
tmpfile.delete();
return valueList;
}
4.2、读取Excel格式
/
* 读取97-2003格式
* @param filePath 文件路径
* @throws java.io.IOException
*/
@SuppressWarnings("rawtypes")
public static List<Map> readExcel2003(String filePath) throws IOException{
//返回结果集
List<Map> valueList=new ArrayList<Map>();
FileInputStream fis=null;
try {
fis=new FileInputStream(filePath);
HSSFWorkbook wookbook = new HSSFWorkbook(fis); // 创建对Excel工作簿文件的引用
HSSFSheet sheet = wookbook.getSheetAt(0); // 在Excel文档中,第一张工作表的缺省索引是0
int rows = sheet.getPhysicalNumberOfRows(); // 获取到Excel文件中的所有行数
Map<Integer,String> keys=new HashMap<Integer, String>();
int cells=0;
// 遍历行(第1行 表头) 准备Map里的key
HSSFRow firstRow = sheet.getRow(0);
if (firstRow != null) {
// 获取到Excel文件中的所有的列
cells = firstRow.getPhysicalNumberOfCells();
// 遍历列
for (int j = 0; j < cells; j++) {
// 获取到列的值
try {
HSSFCell cell = firstRow.getCell(j);
String cellValue = getCellValue(cell);
keys.put(j,cellValue);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 遍历行(从第二行开始)
for (int i = 1; i < rows; i++) {
// 读取左上端单元格(从第二行开始)
HSSFRow row = sheet.getRow(i);
// 行不为空
if (row != null) {
//准备当前行 所储存值的map
Map<String, Object> val=new HashMap<String, Object>();
boolean isValidRow = false;
// 遍历列
for (int j = 0; j < cells; j++) {
// 获取到列的值
try {
HSSFCell cell = row.getCell(j);
String cellValue = getCellValue(cell);
val.put(keys.get(j),cellValue);
if(!isValidRow && cellValue!=null && cellValue.trim().length()>0){
isValidRow = true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
//第I行所有的列数据读取完毕,放入valuelist
if(isValidRow){
valueList.add(val);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
fis.close();
}
return valueList;
}
/
* 读取2007-2013格式
* @param filePath 文件路径
* @return
* @throws java.io.IOException
*/
@SuppressWarnings("rawtypes")
public static List<Map> readExcel2007(String filePath) throws IOException{
List<Map> valueList=new ArrayList<Map>();
FileInputStream fis =null;
try {
fis =new FileInputStream(filePath);
XSSFWorkbook xwb = new XSSFWorkbook(fis); // 构造 XSSFWorkbook 对象,strPath 传入文件路径
XSSFSheet sheet = xwb.getSheetAt(0); // 读取第一章表格内容
// 定义 row、cell
XSSFRow row;
// 循环输出表格中的第一行内容 表头
Map<Integer, String> keys=new HashMap<Integer, String>();
row = sheet.getRow(0);
if(row !=null){
//System.out.println("j = row.getFirstCellNum()::"+row.getFirstCellNum());
//System.out.println("row.getPhysicalNumberOfCells()::"+row.getPhysicalNumberOfCells());
for (int j = row.getFirstCellNum(); j <=row.getPhysicalNumberOfCells(); j++) {
// 通过 row.getCell(j).toString() 获取单元格内容,
if(row.getCell(j)!=null){
if(!row.getCell(j).toString().isEmpty()){
keys.put(j, row.getCell(j).toString());
}
}else{
keys.put(j, "K-R1C"+j+"E");
}
}
}
// 循环输出表格中的从第二行开始内容
for (int i = sheet.getFirstRowNum() + 1; i <= sheet.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
if (row != null) {
boolean isValidRow = false;
Map<String, Object> val = new HashMap<String, Object>();
for (int j = row.getFirstCellNum(); j <= row.getPhysicalNumberOfCells(); j++) {
XSSFCell cell = row.getCell(j);
if (cell != null) {
String cellValue = null;
if(cell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC){
if(DateUtil.isCellDateFormatted(cell)){
cellValue = new DataFormatter().formatRawCellContents(cell.getNumericCellValue(), 0, "yyyy-MM-dd HH:mm:ss");
}
else{
cellValue = String.valueOf(cell.getNumericCellValue());
}
}
else{
cellValue = cell.toString();
}
if(cellValue!=null&&cellValue.trim().length()<=0){
cellValue=null;
}
val.put(keys.get(j), cellValue);
if(!isValidRow && cellValue!= null && cellValue.trim().length()>0){
isValidRow = true;
}
}
}
// 第I行所有的列数据读取完毕,放入valuelist
if (isValidRow) {
valueList.add(val);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
fis.close();
}
return valueList;
}
4.3、其他封装方法
/
* 文件操作 获取文件扩展名
*
* @Author: sunny
* @param filename
* 文件名称包含扩展名
* @return
*/
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot > -1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
/ -----------上传文件,工具方法--------- */
private static final int BUFFER_SIZE = 2 * 1024;
/
*
* @param src
* 源文件
* @param dst
* 目标位置
*/
private static void copy(File src, File dst) {
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/
* 上传copy文件方法(for MultipartFile)
* @param savePath 在linux上要保存完整路径
* @param newname 新的文件名称, 采用系统时间做文件名防止中文报错的问题
* @throws Exception
*/
public static void copy(MultipartFile file,String savePath,String newname) throws Exception {
try {
File targetFile = new File(savePath,newname);
if (!targetFile.exists()) {
//判断文件夹是否存在,不存在就创建
targetFile.mkdirs();
}
file.transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getCellValue(HSSFCell cell) {
DecimalFormat df = new DecimalFormat("#");
String cellValue=null;
if (cell == null)
return null;
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_NUMERIC:
if(HSSFDateUtil.isCellDateFormatted(cell)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cellValue=sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue()));
break;
}
cellValue=df.format(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_STRING:
cellValue=String.valueOf(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
cellValue=String.valueOf(cell.getCellFormula());
break;
case HSSFCell.CELL_TYPE_BLANK:
cellValue=null;
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
cellValue=String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
cellValue=String.valueOf(cell.getErrorCellValue());
break;
}
if(cellValue!=null&&cellValue.trim().length()<=0){
cellValue=null;
}
return cellValue;
}
5、下载操作
5.1、下载页面
window.location.href = ctx + "/operate/download?fileName="
+ encodeURI(file);
前端直接调用后台方法,这边由于涉及到中文乱码的问题,所有此处用encodeURI处理,后台直接解码即可。
5.2、下载controller部分
@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(HttpServletRequest request ,HttpServletResponse response){
try {
/**后台解码encodeURI()*/
request.setCharacterEncoding("utf-8");
String name = request.getParameter("fileName");
name = new String(name.getBytes("ISO-8859-1"), "UTF-8");
File file = new File(request.getSession().getServletContext().getRealPath("/upload/"+name));
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;filename="+ new String(name.getBytes("utf-8"),"ISO-8859-1"));
response.setHeader("Content-Length", "" + file.length());
//System.out.println(file.getAbsolutePath());
InputStream in = new BufferedInputStream(new FileInputStream(file));
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int length;
while((length = in.read(b)) != -1) {
out.write(b, 0, length);
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
此处还有更好的方式可以实现,没有必要暴露request接口在外面。
5.3、写入excel操作
package com.fable.attendance.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFComment;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import com.fable.attendance.bean.Operate;
public class ExportExcel<T> {
public void exportExcel(Collection<T> dataset, OutputStream out)
{
exportExcel("测试POI导出EXCEL文档", null, dataset, out, "yyyy-MM-dd");
}
public void exportExcel(String[] headers, Collection<T> dataset,
OutputStream out)
{
exportExcel("测试POI导出EXCEL文档", headers, dataset, out, "yyyy-MM-dd");
}
public void exportExcel(String[] headers, Collection<T> dataset,
OutputStream out, String pattern)
{
exportExcel("测试POI导出EXCEL文档", headers, dataset, out, pattern);
}
@SuppressWarnings({"deprecation" })
public void exportExcel(String title, String[] headers,
OutputStream out, Map<String, List<Operate>> map) {
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
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);
// 生成一个字体
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
// 生成并设置另一个样式
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
HSSFFont font2 = workbook.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style2.setFont(font2);
//循环map对象
for (Map.Entry<String, List<Operate>> entry : map.entrySet()) {
String sheetTitle = entry.getKey();
List<Operate> operates = entry.getValue();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet(sheetTitle);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
// 声明一个画图的顶级管理器
//HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// 定义注释的大小和位置,详见文档
// HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,
// 0, 0, 0, (short) 4, 2, (short) 6, 5));
// 设置注释内容
//comment.setString(new HSSFRichTextString(“可以在POI中添加注释!”));
// 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.
//comment.setAuthor(“leno”);
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (short i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
//遍历集合数据,产生数据行
for(int i = 0; i < operates.size(); i++) {
Operate operate = operates.get(i);
HSSFRow dataRow = sheet.createRow(i + 1);
//姓名
HSSFCell cell = dataRow.createCell(0);
cell.setCellStyle(style2);
cell.setCellValue(operate.getName());
//迟到
cell = dataRow.createCell(1);
cell.setCellValue(operate.getLate());
//早退
cell = dataRow.createCell(2);
cell.setCellValue(operate.getEarly());
}
}
try {
workbook.write(out);
} catch (IOException e1) {
e1.printStackTrace();
}
}
/**
* 这是一个通用的方法,利用了JAVA的反射机制,可以将放置在JAVA集合中并且符号一定条件的数据以EXCEL 的形式输出到指定IO设备上
*
* @param title
* 表格标题名
* @param headers
* 表格属性列名数组
* @param dataset
* 需要显示的数据集合,集合中一定要放置符合javabean风格的类的对象。此方法支持的
* javabean属性的数据类型有基本数据类型及String,Date,byte[](图片数据)
* @param out
* 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中
* @param pattern
* 如果有时间数据,设定输出格式。默认为"yyy-MM-dd"
*/
@SuppressWarnings({ "unchecked", "deprecation" })
public void exportExcel(String title, String[] headers,
Collection<T> dataset, OutputStream out, String pattern)
{
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 15);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
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);
// 生成一个字体
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.VIOLET.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
style.setFont(font);
// 生成并设置另一个样式
HSSFCellStyle style2 = workbook.createCellStyle();
style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
HSSFFont font2 = workbook.createFont();
font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
style2.setFont(font2);
// 声明一个画图的顶级管理器
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
// 定义注释的大小和位置,详见文档
HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0,
0, 0, 0, (short) 4, 2, (short) 6, 5));
// 设置注释内容
//comment.setString(new HSSFRichTextString("可以在POI中添加注释!"));
// 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容.
//comment.setAuthor("leno");
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (short i = 0; i < headers.length; i++)
{
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
// 遍历集合数据,产生数据行
Iterator<T> it = dataset.iterator();
int index = 0;
while (it.hasNext())
{
index++;
row = sheet.createRow(index);
T t = (T) it.next();
// 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
Field[] fields = t.getClass().getDeclaredFields();
for (short i = 0; i < fields.length; i++)
{
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style2);
Field field = fields[i];
String fieldName = field.getName();
String getMethodName = "get"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1);
try
{
Class tCls = t.getClass();
Method getMethod = tCls.getMethod(getMethodName,
new Class[]
{});
Object value = getMethod.invoke(t, new Object[]
{});
// 判断值的类型后进行强制类型转换
String textValue = null;
if (value instanceof Boolean)
{
boolean bValue = (Boolean) value;
textValue = "男";
if (!bValue)
{
textValue = "女";
}
}
else if (value instanceof Date)
{
Date date = (Date) value;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
textValue = sdf.format(date);
}
else if (value instanceof byte[])
{
// 有图片时,设置行高为60px;
row.setHeightInPoints(60);
// 设置图片所在列宽度为80px,注意这里单位的一个换算
sheet.setColumnWidth(i, (short) (35.7 * 80));
// sheet.autoSizeColumn(i);
byte[] bsValue = (byte[]) value;
HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0,
1023, 255, (short) 6, index, (short) 6, index);
anchor.setAnchorType(2);
patriarch.createPicture(anchor, workbook.addPicture(
bsValue, HSSFWorkbook.PICTURE_TYPE_JPEG));
}
else
{
// 其它数据类型都当作字符串简单处理
textValue = value.toString();
}
// 如果不是图片数据,就利用正则表达式判断textValue是否全部由数字组成
if (textValue != null)
{
Pattern p = Pattern.compile("^//d+(//.//d+)?$");
Matcher matcher = p.matcher(textValue);
if (matcher.matches())
{
// 是数字当作double处理
cell.setCellValue(Double.parseDouble(textValue));
}
else
{
HSSFRichTextString richString = new HSSFRichTextString(
textValue);
HSSFFont font3 = workbook.createFont();
font3.setColor(HSSFColor.BLUE.index);
richString.applyFont(font3);
cell.setCellValue(richString);
}
}
}
catch (SecurityException e)
{
e.printStackTrace();
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
finally
{
// 清理资源
}
}
}
try
{
workbook.write(out);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
ExportExcel<Operate> ex = new ExportExcel<Operate>();
String[] headers = {"姓名","迟到","早退","旷工","加班","请假","外出","出差","应扣","实际扣"};
List<Operate> dataset = new ArrayList<Operate>();
try {
String file = "E://test.xls";
OutputStream out = new FileOutputStream(file);
ex.exportExcel(headers, dataset, out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}