MVC实现下载EXCEL

Action



public String listToExcel()throws Exception{
try{
String filePath=ServletActionContext.getServletContext().getRealPath("/toExcel/仓库备货.xls"); //获得绝对路径
excelStream=slSrvToExcelService.getShipNoticeToExcel(id,filePath);
return SUCCESS;
}catch(Exception e){
e.printStackTrace(); 
return ERROR;
}


slSrvToExcelService.java

public InputStream getShipNoticeToExcel(Integer id, String filePath)
throws ServiceException {
InputStream excelStream=null;
try {
List list = slSrvShipNoticeDAO.getShipNoticeDetails(id);
File file = new File(filePath);
if (file.exists()) {
SimpleDateFormat   sf   =   new   SimpleDateFormat( "yyyy-MM-dd "); 
excelStream = new FileInputStream(file);
HSSFWorkbook wb = new HSSFWorkbook(excelStream);
HSSFSheet sheet = wb.getSheetAt(0);
// 第一行
HSSFRow row = sheet.createRow((short)0);
//row = sheet.createRow(0);
HSSFCell cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("仓库名称");
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("储位");
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("存货编码");
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("存货名称");
cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("生产日期");
cell = row.createCell((short) 5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("生产批号");
cell = row.createCell((short) 6);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("库存数量");
cell = row.createCell((short) 7);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("备货数量");
int amount = 0;
for (int i = 0; i < list.size(); i++) {
SlSrvShipNotices o = (SlSrvShipNotices) list.get(i);
List list2 = ivSrvStoreDAO.getIvSrvCurrentyGoods(o
.getGoodsId());
for (int n = 0; n < list2.size(); n++) {
amount++;
IvSrvStoreDto dto = (IvSrvStoreDto) list2.get(n);
 row = sheet.createRow((short)amount);
cell = row.createCell((short) 0);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(dto.getWareHouseName());
cell = row.createCell((short) 1);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(dto.getStoreNumberName());
cell = row.createCell((short) 2);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(dto.getGoodsCode());
cell = row.createCell((short) 3);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(dto.getGoodsName());
cell = row.createCell((short) 4);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(sf.format(dto.getManufactureDate()));
cell = row.createCell((short) 5);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(dto.getBatchNumber());
cell = row.createCell((short) 6);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(dto.getAmount().toString());
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
   wb.write(baos);
   byte[] ba = baos.toByteArray();
   excelStream= new ByteArrayInputStream(ba);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return excelStream;
}


slSrvShipNoticeDAO。java

//获取明细的信息
public List getShipNoticeDetails(Integer mainId) throws DataAccessException {
List list = null;
List result = new ArrayList();
String sql="select s.* , m.bill_no as billNo, g.code as code, g.name as name , g.unit as unit, g.standard as standard ,l.name as locationName from sl_srv_order m, iv_pam_goods g, sl_srv_shipnotices s left join iv_pam_warehouse i on s.warehouse_id=i.id left join iv_pam_storenumber as l on s.location=l.id  ";
String where = " where s.order_id=m.id and s.goods_id=g.id and s.main_id=:mainId ";
Session session = this.getSession();
try {
Query q = session.createSQLQuery(sql.concat(where)).addEntity(SlSrvShipNotices.class).addScalar("billNo").addScalar("code").addScalar("name").addScalar("standard").addScalar("unit").addScalar("locationName");
q.setParameter("mainId", mainId);
list = q.list();
for (int i = 0; i < list.size(); i++) {
SlSrvShipNoticesDto dto = new SlSrvShipNoticesDto();
Object[] objs = (Object[]) list.get(i);
PropertyUtils.copyProperties(dto, objs[0]);
dto.setSaleBillNo((String) objs[1]);
dto.setGoodsCode((String) objs[2]);
dto.setGoodsName((String) objs[3]);
dto.setUnit((String)objs[4]);
dto.setStandard((String) objs[5]);
dto.setLocationName((String)objs[6]);
result.add(dto);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值