实现导出Excel功能! 展示报表页面可以预览,预览时提供导出Excel,把报表页面显示的结果以Excel的形式导出。有例子贴个例子,谢谢!不要发连接。公司上不了外网。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 额、难道csdn不算外网 - -# poi工具包实现生成文件,然后提供下载功能,网上代码很多的 基本步骤:点击导出-》后台生成excel放在demp临时文件夹--》下载excelpublic class Excel { /** * * 生成EXCEL文件并导出数据 * * @param path * 文件路径 * @param sheetNums * 创建工作表数量 * @param sheetNames * 多个工作表对应的名称 * @param titles * 表头数组 * @param 写入excel中的正文数据 * 表头数组 * @return file * 返回生成的xls文件 * @throws Exception */ public File CreateExcel(String path, int sheetNums, String[] sheetNames, String[] titles, Object[] objects, HttpServletResponse response) throws Exception { if(Utils.isNull(path)){ throw new Exception("请指定导出文件的路径!");} if(Utils.isNull_1(sheetNums)){ throw new Exception("请指定EXCEL工作表数量!");} if(Utils.isNull(sheetNames)){ throw new Exception("请指定EXCEL工作表名称!");} if(sheetNums != sheetNames.length){ throw new Exception("工作表数量参数必须与名称参数长度一致!");} if(Utils.isNull(titles)){ throw new Exception("请为Excel文件指定表头!");} if(Utils.isNull(objects)){ throw new Exception("请为Excel文件设置正文!");} // 当前时间字符串组成excel文件名称 String xlsName = path + "tokenlog_" + DateNowStr() + ".xls"; File file = new File(xlsName); OutputStream ot = new FileOutputStream(file); // 创建excel文件 WritableWorkbook wwb = Workbook.createWorkbook(ot); // 设置表头字体 WritableFont wtitlefont = new WritableFont(WritableFont.TIMES, 11, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat titleformat = new WritableCellFormat(wtitlefont); // 创建工作表 for(int num = 0; num < sheetNums; num++) { WritableSheet sheet = wwb.createSheet(sheetNames[num], 0); // 设置excel表头 int row =0; int column = 0; for(String title : titles){ Label label0 = new Label(column, row, title, titleformat); sheet.addCell(label0); column ++; } // 写入正文 SetSheetCell(sheet, objects[num]); } // 提交写入数据并关闭 wwb.write(); wwb.close(); ot.close(); return file; } /** * 写入正文--可配置 * @param sheet * @param objects * @throws RowsExceededException * @throws WriteException */ private void SetSheetCell(WritableSheet sheet, Object objects) throws RowsExceededException, WriteException { // 设置正文字体 WritableFont wcellfont = new WritableFont(WritableFont.TIMES, 11, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); WritableCellFormat cellformat = new WritableCellFormat(wcellfont); int number = 1001; HashMap<Integer, String> map = GetXMLConfigProperty.ExcelHeader(); int id = 1; // 记录编号 int row = 1; int column = 0; RadiusAuditInfo[] audits = (RadiusAuditInfo[])objects; for(RadiusAuditInfo audit : audits) { if(map.get(number).equals("true")){ Label label1 = new Label(column, row, String.valueOf(id), cellformat); sheet.addCell(label1); column ++; } number ++; if(map.get(number).equals("true")){ Label label2 = new Label(column, row, audit.getResourceid(), cellformat); sheet.addCell(label2); column ++; } number ++; if(map.get(number).equals("true")){ Label label3 = new Label(column, row, audit.getResourcename(), cellformat); sheet.addCell(label3); column ++; } number ++; if(map.get(number).equals("true")){ Integer type = Integer.parseInt(audit.getLocalname()); String[] localtype = GetXMLConfigProperty.getLocaltype(); String localname = "系统主机"; for (int j = 0; j < localtype.length; j++) { if (type == j) localname = localtype[type]; } Label label4 = new Label(column, row, localname, cellformat); sheet.addCell(label4); column ++; } number ++; if(map.get(number).equals("true")){ Label label5 = new Label(column, row, audit.getAddress(), cellformat); sheet.addCell(label5); column ++; } number ++; if(map.get(number).equals("true")){ Label label6 = new Label(column, row, audit.getAuthuserid(), cellformat); sheet.addCell(label6); column ++; } number ++; if(map.get(number).equals("true")){ Label label7 = new Label(column, row, audit.getId_user(), cellformat); sheet.addCell(label7); column ++; } number ++; if(map.get(number).equals("true")){ Label label8 = new Label(column, row, audit.getIsbypass() == 0 ? "否" : "是", cellformat); sheet.addCell(label8); column ++; } number ++; if(map.get(number).equals("true")){ Label label9 = new Label(column, row, audit.getEquipno() != null ? audit.getEquipno() : "静态口令", cellformat); sheet.addCell(label9); column ++; } number ++; if(map.get(number).equals("true")){ Label label10 = new Label(column, row, audit.getAuthresultcode(), cellformat); sheet.addCell(label10); column ++; } number ++; if(map.get(number).equals("true")){ Label label11 = new Label(column, row, audit.getAuthresultcode().equals("00000") ? "认证成功" : "认证失败", cellformat); sheet.addCell(label11); column ++; } number ++; if(map.get(number).equals("true")){ Label label12 = new Label(column, row, audit.getRe(), cellformat); sheet.addCell(label12); column ++; } row ++; id ++ ; column = 0; number = 1001; } } /** * 获取当前时间组成的字符串,用来组成excel命令 * @return */ public static String DateNowStr(){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date date = new Date(); String dateStr = format.format(date); String excelName = ((dateStr.replaceAll("-", "")).replaceAll(":", "")).replaceAll("\\s+", ""); return excelName; } /** * 下载文件 * @param filePath * @param filename * @param response * @throws Exception */ public static void download(String filePath, String filename, HttpServletResponse response) throws Exception{ // TODO Auto-generated method stub BufferedInputStream bis = null; BufferedOutputStream bos = null; OutputStream fos = null; InputStream fis = null; File uploadFile = new File(filePath); fis = new FileInputStream(uploadFile); bis = new BufferedInputStream(fis); fos = response.getOutputStream(); bos = new BufferedOutputStream(fos); //filePath = URLEncoder.encode(filePath, "UTF-8"); //弹出下载对话框的关键代码 response.setContentType("application/x-download"); response.setHeader("Content-Disposition","attachment;filename="+filename); int bytesRead = 0; //都是用输入流进行先读,然后用输出流去写,用的是缓冲输入输出流 byte[] buffer = new byte[8192]; while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) { try{ bos.write(buffer, 0, bytesRead); }catch(Exception e){} } try{bos.flush();}catch(Exception e){} try{fis.close();}catch(Exception e){} try{bis.close();}catch(Exception e){} try{fos.close();}catch(Exception e){} try{bos.close();}catch(Exception e){} } 上百度搜 ”poi“这个东东,就有了代码多的是,记得要导入poi的那几个jar包,网上也有。。 又碰一CSDN的内部工作人员,话说,你们WEB服务器也在你们的内部局域网中么? 高手请进,关于HTML 转 Word 图片写入问题 严重: Dispatcher initialization failed linux下对象的问题 帮我看看xml的错误 把HIBERNATE的映射类用做STRUTS的FORMBEAN有什么看法? 请问如何正确完整地显示邮件内容 Hibernate查询时性能的提高?? 一个关于SOAP的问题。 Help!散分! 公司让用java搞一个局域网内的管理、监测软件... 网络抓包,怎么用Java程序登录百度? Label标签问题---急救--谢谢 我下了一个网站模板,不知道怎么用,求解答
/**
*
* 生成EXCEL文件并导出数据
*
* @param path
* 文件路径
* @param sheetNums
* 创建工作表数量
* @param sheetNames
* 多个工作表对应的名称
* @param titles
* 表头数组
* @param 写入excel中的正文数据
* 表头数组
* @return file
* 返回生成的xls文件
* @throws Exception
*/
public File CreateExcel(String path, int sheetNums, String[] sheetNames, String[] titles, Object[] objects, HttpServletResponse response) throws Exception
{
if(Utils.isNull(path)){
throw new Exception("请指定导出文件的路径!");}
if(Utils.isNull_1(sheetNums)){
throw new Exception("请指定EXCEL工作表数量!");}
if(Utils.isNull(sheetNames)){
throw new Exception("请指定EXCEL工作表名称!");}
if(sheetNums != sheetNames.length){
throw new Exception("工作表数量参数必须与名称参数长度一致!");}
if(Utils.isNull(titles)){
throw new Exception("请为Excel文件指定表头!");}
if(Utils.isNull(objects)){
throw new Exception("请为Excel文件设置正文!");}
// 当前时间字符串组成excel文件名称
String xlsName = path + "tokenlog_" + DateNowStr() + ".xls";
File file = new File(xlsName);
OutputStream ot = new FileOutputStream(file);
// 创建excel文件
WritableWorkbook wwb = Workbook.createWorkbook(ot);
// 设置表头字体
WritableFont wtitlefont = new WritableFont(WritableFont.TIMES, 11, WritableFont.BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat titleformat = new WritableCellFormat(wtitlefont);
// 创建工作表
for(int num = 0; num < sheetNums; num++)
{
WritableSheet sheet = wwb.createSheet(sheetNames[num], 0);
// 设置excel表头
int row =0;
int column = 0;
for(String title : titles){
Label label0 = new Label(column, row, title, titleformat);
sheet.addCell(label0);
column ++;
}
// 写入正文
SetSheetCell(sheet, objects[num]);
}
// 提交写入数据并关闭
wwb.write();
wwb.close();
ot.close();
return file;
}
/**
* 写入正文--可配置
* @param sheet
* @param objects
* @throws RowsExceededException
* @throws WriteException
*/
private void SetSheetCell(WritableSheet sheet, Object objects) throws RowsExceededException, WriteException
{
// 设置正文字体
WritableFont wcellfont = new WritableFont(WritableFont.TIMES, 11, WritableFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
WritableCellFormat cellformat = new WritableCellFormat(wcellfont);
int number = 1001;
HashMap<Integer, String> map = GetXMLConfigProperty.ExcelHeader();
int id = 1; // 记录编号
int row = 1;
int column = 0;
RadiusAuditInfo[] audits = (RadiusAuditInfo[])objects;
for(RadiusAuditInfo audit : audits)
{
if(map.get(number).equals("true")){
Label label1 = new Label(column, row, String.valueOf(id), cellformat);
sheet.addCell(label1);
column ++;
}
number ++;
if(map.get(number).equals("true")){
Label label2 = new Label(column, row, audit.getResourceid(), cellformat);
sheet.addCell(label2);
column ++;
}
number ++;
if(map.get(number).equals("true")){
Label label3 = new Label(column, row, audit.getResourcename(), cellformat);
sheet.addCell(label3);
column ++;
}
number ++;
if(map.get(number).equals("true")){
Integer type = Integer.parseInt(audit.getLocalname());
String[] localtype = GetXMLConfigProperty.getLocaltype();
String localname = "系统主机";
for (int j = 0; j < localtype.length; j++) {
if (type == j)
localname = localtype[type];
}
Label label4 = new Label(column, row, localname, cellformat);
sheet.addCell(label4);
column ++;
}
number ++;
if(map.get(number).equals("true")){
Label label5 = new Label(column, row, audit.getAddress(), cellformat);
sheet.addCell(label5);
column ++;
}
number ++;
if(map.get(number).equals("true")){
Label label6 = new Label(column, row, audit.getAuthuserid(), cellformat);
sheet.addCell(label6);
column ++;
}
number ++;
if(map.get(number).equals("true")){
Label label7 = new Label(column, row, audit.getId_user(), cellformat);
sheet.addCell(label7);
column ++;
}
number ++;
if(map.get(number).equals("true")){
Label label8 = new Label(column, row, audit.getIsbypass() == 0 ? "否" : "是", cellformat);
sheet.addCell(label8);
column ++;
}
number ++;
if(map.get(number).equals("true")){
Label label9 = new Label(column, row, audit.getEquipno() != null ? audit.getEquipno() : "静态口令", cellformat);
sheet.addCell(label9);
column ++;
}
number ++;
if(map.get(number).equals("true")){
Label label10 = new Label(column, row, audit.getAuthresultcode(), cellformat);
sheet.addCell(label10);
column ++;
}
number ++;
if(map.get(number).equals("true")){
Label label11 = new Label(column, row, audit.getAuthresultcode().equals("00000") ? "认证成功" : "认证失败", cellformat);
sheet.addCell(label11);
column ++;
}
number ++;
if(map.get(number).equals("true")){
Label label12 = new Label(column, row, audit.getRe(), cellformat);
sheet.addCell(label12);
column ++;
}
row ++;
id ++ ;
column = 0;
number = 1001;
}
}
/**
* 获取当前时间组成的字符串,用来组成excel命令
* @return
*/
public static String DateNowStr(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date date = new Date();
String dateStr = format.format(date);
String excelName = ((dateStr.replaceAll("-", "")).replaceAll(":", "")).replaceAll("\\s+", "");
return excelName;
}
/**
* 下载文件
* @param filePath
* @param filename
* @param response
* @throws Exception
*/
public static void download(String filePath, String filename, HttpServletResponse response) throws Exception{
// TODO Auto-generated method stub
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null; File uploadFile = new File(filePath);
fis = new FileInputStream(uploadFile);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos);
//filePath = URLEncoder.encode(filePath, "UTF-8");
//弹出下载对话框的关键代码
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename="+filename);
int bytesRead = 0;
//都是用输入流进行先读,然后用输出流去写,用的是缓冲输入输出流
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
try{
bos.write(buffer, 0, bytesRead);
}catch(Exception e){}
}
try{bos.flush();}catch(Exception e){}
try{fis.close();}catch(Exception e){}
try{bis.close();}catch(Exception e){}
try{fos.close();}catch(Exception e){}
try{bos.close();}catch(Exception e){}
}