发布网友 发布时间:2022-04-22 02:38
共2个回答
热心网友 时间:2022-04-21 09:39
先通过数据库查询出数据,放到List里,然后把这个List发往页面,然后遍历这个List把数据显示到这个表格里。 要想把数据导出到execel,很简单,把页面接受的这个List用jxl写到Execel就行了。具体将List导出到Execel的类如下:
package cms.;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import cms.utlis.DbUtils;
public class ToExecelByQuery {
//方法接受两个参数,一个是list,这个地方我用了泛型。另一个参数是HttpServletResponse response
public static void toExcelBy(List<AccessLog> list,HttpServletResponse response) {
// 创建工作表
WritableWorkbook book=null;
response.reset();
// 创建工作流
OutputStream os =null;
try {
// 设置弹出对话框
response.setContentType("application/DOWLOAD");
// 设置工作表的标题
response.setHeader("Content-Disposition",
"attachment; filename=****.xls");//设置生成的文件名字
os = response.getOutputStream();
// 初始化工作表
book = Workbook.createWorkbook(os);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
//以下是我做的导出日志的一个模版
int nCount = list.size();
WritableSheet sheet = book.createSheet("访问日志", 0);
// 生成名为"商品信息"的工作表,参数0表示这是第一页
int nI = 1;
// 表字段名
sheet.addCell(new jxl.write.Label(0, 0, "日志编号"));
sheet.addCell(new jxl.write.Label(1, 0, "用户ID"));
sheet.addCell(new jxl.write.Label(2, 0, "用户姓名"));
sheet.addCell(new jxl.write.Label(3, 0, "访问日期"));
sheet.addCell(new jxl.write.Label(4, 0, "访问时间"));
sheet.addCell(new jxl.write.Label(5, 0, "名片ID"));
sheet.addCell(new jxl.write.Label(6, 0, "名片名称"));
sheet.addCell(new jxl.write.Label(7, 0, "创建日期"));
sheet.addCell(new jxl.write.Label(8, 0, "更新日期"));
// 将数据追加
for(int i=1;i<list.size();i++){
sheet.addCell(new jxl.write.Label(0, i, list.get(i).toString()));
sheet.addCell(new jxl.write.Label(1, i, list.get(i).getUserId()));
sheet.addCell(new jxl.write.Label(2, i, list.get(i).getUsername()));
sheet.addCell(new jxl.write.Label(3, i, list.get(i).getCrtTim()));
sheet.addCell(new jxl.write.Label(4, i, list.get(i).getComplTime()));
sheet.addCell(new jxl.write.Label(5, i, list.get(i).getCopId()));
sheet.addCell(new jxl.write.Label(6, i, list.get(i).getFirstname()));
sheet.addCell(new jxl.write.Label(7, i, list.get(i).getCrtTim()));
sheet.addCell(new jxl.write.Label(8, i, list.get(i).getUpdTim()));
}
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
大体思路就是这样的,别忘了在你的项目中导入JXL必要的jar包,这个包叫jxl.jar,可以下载一个。
热心网友 时间:2022-04-21 10:57
用apache 的poi去做吧。我都是这样做的。
还有一种方法就是直接用一个html的表格页面,但后台处理的时候,保存为excel文件,excel能够打开html的表格形式的文件。
再返回到前台显示,就是excel格式。
package com.enstrong.mis.ranliao.report.action;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.enstrong.mis.common.util.DateTimeUtil;
import com.enstrong.mis.common.util.SearchCondition;
import com.enstrong.mis.ranliao.report.service.IStatisticalReportService;
public class StatisticalReportAction extends DownloadAction {
private String path;
public Object getBean(String name) {
ApplicationContext ctx = WebApplicationContextUtils
.getRequiredWebApplicationContext(servlet.getServletContext());
return ctx.getBean(name);
}
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
IStatisticalReportService srv = (IStatisticalReportService)this.getBean("statisticalReportService");
String type = request.getParameter("type"); //报表代号
String seldate = request.getParameter("seldate"); //开始日期
String way = request.getParameter("way"); //运输方式
String enddate= request.getParameter("enddate"); //结束日期
String name = request.getParameter("name"); //矿名
SearchCondition sc = new SearchCondition();
sc.setS_oneCondition(name);
sc.setS_twoCondition(way);
sc.setS_threeCondition(seldate);
sc.setS_fourCondition(enddate);
StringBuffer sb = new StringBuffer("<html><body><table align=center border=1>");
String tmp = "";
// 煤供应情况明细表
if("GMMX".equals(type)){
tmp = srv.getGMMXHtml(seldate);
}
// 生产用煤供应、耗用与结存月报表
else if("JCYB".equals(type)) {
tmp = srv.getJCYBHtml(seldate);
}
// 燃料清单汇总
else if("QDHZ".equals(type)) {
tmp = srv.getQDHZHtml(sc);
}
sb.append(tmp);
sb.append("</table></body></html>");
byte[] buffer = sb.toString().getBytes();
String fileName=DateTimeUtil.dateToString(new Date(), "yyyyMMddHH") + ".xls";
File storeDir = getStoreDir(request);
File newFile = new File(storeDir, fileName);
try {
InputStream is = new ByteArrayInputStream(buffer);
OutputStream os = new FileOutputStream(newFile);
int bytesRead = 0;
// byte buffer[] = new byte[formFile.getFileSize()];
while ((bytesRead = is.read(buffer, 0, buffer.length)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
path = storeDir + "\\" + fileName;
StreamInfo si = getStreamInfo(
mapping, form, request, response);
String contentType = si.getContentType();
InputStream stream = si.getInputStream();
try {
response.setContentType(contentType);
copy(stream, response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null)
stream.close();
}
return null;
}
/**
* 获取路径
* @author hum
* @param request
* @return
*/
private File getStoreDir(HttpServletRequest request) {
File storeDir = new File(servlet.getServletContext().getRealPath(
File.separator)
+ "tempMoleFile");
if (!storeDir.exists()) {
storeDir.mkdirs();
}
return storeDir;
}
@Override
protected StreamInfo getStreamInfo(ActionMapping arg0, ActionForm arg1,
HttpServletRequest arg2, HttpServletResponse response) throws Exception {
String fileName=DateTimeUtil.dateToString(new Date(), "yyyyMMddHH") + ".xls";
response.setHeader("Content-disposition",
"attachment; filename="+fileName);
String contentType = "application/vnd.ms-excel";
File file = new File(path);
return new FileStreamInfo(contentType, file);
}
}
service只需要返回创建的excel中的每一行和列就可以了。