如何按照搜索条件的结果导出excel 就是说有几个选择的搜索条件,点击“搜索”出现搜索结果。现在需要增加一个导出按钮,将搜索的数据excel导出,求大家帮忙 ,最好有例子哈。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 点导出后 你还是将搜索的条件传入后台在后台重新查询 后 用POI 将结果集写入excel文件因为一般的查询都是分页的 而导出一般是要所有符合条件的 这个简单啊。设置一个模版、然后根据你的查询用的SQL或者HQL再查一遍数据库就好了啊。然后把数据放进模版里。我们做的项目还可以把查询结果返回列位置变换和隐藏、导出时仍然所见即所得。 POI code:public static void writeExcel(OutputStream out, List<BaseBean> beanList) throws Exception { HSSFWorkbook book = new HSSFWorkbook(); for(int i = 0; i < beanList.size(); i++) { HSSFRow row = sheet.createRow(sheet.getLastRowNum() + 1); //row.setHeight((short) 255); cell = row.createCell(0); cell.setCellValue(String.valueOf(beanList.get(i).getName())); } book.write(out); }download code: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String sufix = request.getParameter("s"); String path = request.getParameter("p") + "." + sufix; try { String fileName = request.getParameter("f").trim(); int c = fileName.lastIndexOf("."); String name = fileName.substring(0, c > 0 ? c : fileName.length()) + "." + sufix; response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachement;filename=" + new String(name.getBytes("GBK"), "ISO-8859-1")); File file = new File(Const.getCurrentUtterlyPath() + path); if (!file.exists()) { throw new IOException(fileName + ",所下载的文件不存在!"); } response.setContentLength(Integer.parseInt(file.length() + "")); InputStream fs = new FileInputStream(file); OutputStream os = response.getOutputStream(); byte[] buff = new byte[1024]; int readCount = 0; while ((readCount = fs.read(buff)) != -1) { os.write(buff, 0, readCount); } if (fs != null) { fs.close(); } if (os != null) { os.close(); } } catch (IOException e) { LOG.error("error: " + e.getMessage() + ",path: " + path); throw e; } response.setStatus(response.SC_OK); response.flushBuffer(); } HSSFSheet sheet = book.createSheet("sheet name"); 不用那么麻烦。。我以前写过。。直接在jsp上就能写晚上回家帮你找找代码 html作为显示层怎么与后台进行数据交互 问个关于数据库访问类封装的问题 jsp页面打开上传的视频有图像没声音 byte[] To String 大家有什么好的编码方法(except BASE64) 求助:有关JSP中读取利用HTML编辑器存储的数据,怎么按WEB格式显示?急! (急)有关网站的定时器 急!!!直接写在JSP页面上的中文显示的是乱码 新手求救:javabean疑难杂症,请赐教,忙了我两天了 求救:一个具体程序的实现(急,在线等待) sqlserver 2000 读写同时进行时,效率很慢 日志管理 谁做过web gis系统,给点思路,非常感谢!
在后台重新查询 后 用POI 将结果集写入excel文件因为一般的查询都是分页的 而导出一般是要所有符合条件的
设置一个模版、然后根据你的查询用的SQL或者HQL再查一遍数据库就好了啊。
然后把数据放进模版里。我们做的项目还可以把查询结果返回列位置变换和隐藏、导出时仍然所见即所得。
POI code:public static void writeExcel(OutputStream out, List<BaseBean> beanList)
throws Exception {
HSSFWorkbook book = new HSSFWorkbook(); for(int i = 0; i < beanList.size(); i++) {
HSSFRow row = sheet.createRow(sheet.getLastRowNum() + 1);
//row.setHeight((short) 255); cell = row.createCell(0);
cell.setCellValue(String.valueOf(beanList.get(i).getName()));
} book.write(out);
}download code:
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String sufix = request.getParameter("s");
String path = request.getParameter("p") + "." + sufix;
try {
String fileName = request.getParameter("f").trim();
int c = fileName.lastIndexOf(".");
String name = fileName.substring(0, c > 0 ? c : fileName.length())
+ "." + sufix;
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachement;filename="
+ new String(name.getBytes("GBK"), "ISO-8859-1"));
File file = new File(Const.getCurrentUtterlyPath() + path);
if (!file.exists()) {
throw new IOException(fileName + ",所下载的文件不存在!");
}
response.setContentLength(Integer.parseInt(file.length() + ""));
InputStream fs = new FileInputStream(file);
OutputStream os = response.getOutputStream();
byte[] buff = new byte[1024];
int readCount = 0;
while ((readCount = fs.read(buff)) != -1) {
os.write(buff, 0, readCount);
}
if (fs != null) {
fs.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
LOG.error("error: " + e.getMessage() + ",path: " + path);
throw e;
}
response.setStatus(response.SC_OK);
response.flushBuffer();
}
HSSFSheet sheet = book.createSheet("sheet name");
晚上回家帮你找找代码