按要求从表中找出数据,再以Excel形式下载,操作步骤如下:
Action类
public ActionForward doBillPackDown(BActionContext context)
throws Exception {
try{
HttpServletResponse response = context.getResponse();
response.setContentType("application/multipart");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String TodayDate = sdf.format(new Date());
String fileName = "Download_BillPack_" + TodayDate + ".xls";
response.setHeader("Content-Disposition", "filename=" + fileName);
EPickService service = (EPickService) context.findService("epickService", EPickService.class);
String[] ids= (String[]) context.getRequest().getParameterValues("ids");
logger.info("======电子提单明细下载开始=======");
EPickSearchModel mdl = new EPickSearchModel();
mdl.setIds(ids);
HSSFWorkbook workbook = service.downBillPackDetail(mdl);
workbook.write(response.getOutputStream());
response.getOutputStream().flush();
response.getOutputStream().close();
return null;
}catch (Exception e) {
e.printStackTrace();
logger.error(e);
if (e.getMessage()!=null && e.getMessage().startsWith("msg:")) {
context.getRequest().setAttribute("msg",
e.getMessage().substring(4));
} else {
context.getRequest().setAttribute("error", e);
}
return context.findForward("fail");
}
}
ServiceImpl类:
public HSSFWorkbook downBillPackDetail(EPickSearchModel mdl)
throws Exception {
return epickDao.downBillPackDetail(mdl);
}
DaoImpl类:
public HSSFWorkbook downBillPackDetail(EPickSearchModel mdl)
throws Exception {
String[] billIds = mdl.getIds();
StringBuffer hql = new StringBuffer();
hql.append("select t.billId,t.orderNum,t.shopsign,t.specDesc,t.packId,t.netWeight,t.grossWeight" +
" from TplBillPack t where t.billNum in (");
int idSize = billIds.length;
for (int i=0; i<idSize-1;i++ ){
hql.append(""+new String(billIds[i]) + ",");
}
hql.append(""+new String(billIds[idSize-1]) + ")");
hql.append(" order by t.billId, t.orderNum");
List billPackList = this.queryByHql(hql.toString());
//实例化一个hssfworkbook对象
HSSFWorkbook wb = new HSSFWorkbook();
downBillPackDetailHelp(wb,billPackList);
return wb;
}
public void downBillPackDetailHelp(HSSFWorkbook wb, List billPackList)throws Exception{
try{
Object [] objHead=getHeadFromBillPack();
LinkedList headList=(LinkedList)objHead[0];
//创建一个sheet
HSSFSheet sheet = wb.createSheet("contract_list");
//设置单元格的样式
HSSFCellStyle cellStyle0 = wb.createCellStyle();
cellStyle0.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle0.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
short rownum = 0;
//创建第一行
HSSFRow srow = sheet.createRow(rownum);
// HSSFCell cellHead = null;
// srow = sheet.createRow( (short) rownum);
//
// srow.setHeightInPoints(30);//设置高度
// cellHead = srow.createCell((short) 0);
// cellHead.setEncoding(HSSFCell.ENCODING_UTF_16);//设置编码
// cellHead.setCellValue("电子提单明细"); //单元格内容
// cellHead.setCellStyle(cellStyle0); //单元格样式
//
// srow = sheet.createRow( (short) 1);
// //srow.setHeight( (short) 300);
// rownum++;
// srow = sheet.createRow(rownum);
for (int i = 0; i < headList.size(); i++){
HSSFCell cell = srow.createCell((short) (i));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(headList.get(i).toString());
}
for(Iterator it = billPackList.iterator();it.hasNext();) {
Object[] obj = (Object[])it.next();
rownum++;
srow = sheet.createRow(rownum);
for (int j = 0; j < headList.size(); j++) {
HSSFCell cell=srow.createCell((short)j);
if(obj[j]==null)
cell.setCellValue("");
else
cell.setCellValue(obj[j].toString());
}
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 获取Excel第一行标题
* @return
*/
private Object []getHeadFromBillPack(){
List head =new LinkedList();
head.add("提单号");
head.add("合同号");
head.add("牌号");
head.add("规格");
head.add("捆包号");
head.add("净重");
head.add("毛重");
Object [] obj=new Object[1];
obj[0]=head;
return obj;
}
jsp页面中的js代码:
function billPackDown(){
var ids = document.getElementsByName('checkbox');
var ids_java = new Array();
var count = 0;
var msg = "";
if(ids.length == 0)
{
alert("现在没有记录信息,不能下载!");
return;
}
for(i=0; i < ids.length; i++)
{
if(ids[i].checked)
{
count ++;
ids_java.push(ids[i].value);
}
}
if(count < 1 || count > 5)
{
alert("电子提单明细下载需至少选择1条且不多于5条的提单记录!");
return;
}
if (msg != ""){
msg += "请用户重新选择!";
alert(msg);
return;
}
window.open('../ePick/ePickPrintManage.do?method=billPackDown&ids='+ids_java,'','width=800,height=500,top=100,left=150,scrollbars=yes,resizable=yes');
}