// 导出Excel
public boolean exportExcel(HttpServletResponse response,java.util.List<UserIVerifn> list) {
try {
OutputStream os = response.getOutputStream();// 取得输出流
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
String tmptitle = "验证码表"+df.format(new Date()); // 标题
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename="+tmptitle+".xls");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
/**
String Divpath = "d:\\test";//文件保存路径
File dirFile = new File(Divpath);
if(!dirFile.exists()){//文件路径不存在时,自动创建目录
dirFile.mkdir();
}
String path = Divpath+"\\"+tmptitle;//文件名字
//创建一个可写入的excel文件对象
WritableWorkbook workbook = Workbook.createWorkbook(new File(path));*/
WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
//合并单元格
wsheet.mergeCells(0,0,2,0);
wsheet.setRowView(0,400,false);
//设置列宽
wsheet.setColumnView(0, 15);
wsheet.setColumnView(1, 25);
wsheet.setColumnView(2, 15);
// 设置excel标题
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
WritableCellFormat wcfFC = new WritableCellFormat(wfont);
wsheet.addCell(new Label(0, 0, tmptitle, wcfFC));
wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,WritableFont.BOLD,
false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
wcfFC = new WritableCellFormat(wfont);
wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE); //设置居中对齐
wcfFC.setAlignment(Alignment.CENTRE);//设置居中对齐(这俩哪个是上下/左右对齐也没验证过)
// 开始生成主体内容
wsheet.addCell(new Label(0, 2, "序号"));
wsheet.addCell(new Label(1, 2, "验证码"));
wsheet.addCell(new Label(2, 2, "状态"));
String str = "";
for(int i=0;i<list.size();i++) {
wsheet.addCell(new Label(0, i+3, String.valueOf(list.get(i).getId()))); //数据库字段
wsheet.addCell(new Label(1, i+3, list.get(i).getCode()));
if(list.get(i).getStatu() == 0){
str = "有效";
}else{
str = "无效";
}
wsheet.addCell(new Label(2, i+3,str));
}
// 主体内容生成结束
wbook.write(); // 写入文件
wbook.close();
os.close(); // 关闭流
return true;
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}
public boolean exportExcel(HttpServletResponse response,java.util.List<UserIVerifn> list) {
try {
OutputStream os = response.getOutputStream();// 取得输出流
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
String tmptitle = "验证码表"+df.format(new Date()); // 标题
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename="+tmptitle+".xls");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
/**
String Divpath = "d:\\test";//文件保存路径
File dirFile = new File(Divpath);
if(!dirFile.exists()){//文件路径不存在时,自动创建目录
dirFile.mkdir();
}
String path = Divpath+"\\"+tmptitle;//文件名字
//创建一个可写入的excel文件对象
WritableWorkbook workbook = Workbook.createWorkbook(new File(path));*/
WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
//合并单元格
wsheet.mergeCells(0,0,2,0);
wsheet.setRowView(0,400,false);
//设置列宽
wsheet.setColumnView(0, 15);
wsheet.setColumnView(1, 25);
wsheet.setColumnView(2, 15);
// 设置excel标题
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
WritableCellFormat wcfFC = new WritableCellFormat(wfont);
wsheet.addCell(new Label(0, 0, tmptitle, wcfFC));
wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,WritableFont.BOLD,
false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
wcfFC = new WritableCellFormat(wfont);
wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE); //设置居中对齐
wcfFC.setAlignment(Alignment.CENTRE);//设置居中对齐(这俩哪个是上下/左右对齐也没验证过)
// 开始生成主体内容
wsheet.addCell(new Label(0, 2, "序号"));
wsheet.addCell(new Label(1, 2, "验证码"));
wsheet.addCell(new Label(2, 2, "状态"));
String str = "";
for(int i=0;i<list.size();i++) {
wsheet.addCell(new Label(0, i+3, String.valueOf(list.get(i).getId()))); //数据库字段
wsheet.addCell(new Label(1, i+3, list.get(i).getCode()));
if(list.get(i).getStatu() == 0){
str = "有效";
}else{
str = "无效";
}
wsheet.addCell(new Label(2, i+3,str));
}
// 主体内容生成结束
wbook.write(); // 写入文件
wbook.close();
os.close(); // 关闭流
return true;
}catch(Exception ex){
ex.printStackTrace();
return false;
}
}