【springmvc+mybatis项目实战】杰信商贸-25.出货表打印

我们之前学习了POI技术,可以利用POI进行自定义excel文件的生成。我们接下来就将利用这一技术来实现我们的出货表的打印。

回顾一下我们的出货表

我们将利用POI构造这样一个excel文档,然后将它生成。

我们先从头来分析,“2012年8月份出货表”是一个标题,并且合并了单元格,我们应该怎么做呢?

我们的出货表的开发步骤如下
1.获取数据
2.创建excel文件
3.将数据写入excel文件

先从“获取数据”开始,我们的出货表是按照月份出货的,所以我们要先编写一个界面,让用户在这个页面来选择需要打印几月份的出货表:

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="../../base.jsp"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>打印出货表</title>
    <script language="javascript" src="${ctx}/js/datepicker/WdatePicker.js"></script>
</head>
<body>
<form method="post">

<div id="menubar">
<div id="middleMenubar">
<div id="innerMenubar">
    <div id="navMenubar">
<ul>
<li id="save"><a href="#" onclick="formSubmit('print.action','_self');">打印</a></li>
</ul>
    </div>
</div>
</div>
</div>

<div class="textbox" id="centerTextbox">

    <div class="textbox-header">
    <div class="textbox-inner-header">
    <div class="textbox-title">
		出货表月统计
    </div>
    </div>
    </div>
<div>

    <div>
		<table class="commonTable" cellspacing="1">
		        <tr>
		            <td class="columnTitle_mustbe">船期:</td>
		            <td class="tableContent">
		            <input type="text" style="width: 90px" name="inputDate" readonly class="Wdate"
		             onclick="WdatePicker({el:this,isShowOthers:true,dateFmt:'yyyy-MM'});"/>
		            </td>
		        </tr>
		</table>
	</div>
</div>

</form>
</body>
</html>

然后我们需要一个Controller来处理用户选择的日期,然后创建相应的文件:

package cn.hpu.jk.controller.cargo.outproduct;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.hpu.jk.controller.BaseController;

@Controller
public class OutProductController extends BaseController{
	//转向输入年月的页面
	@RequestMapping("/cargo/outproduct/toedit.action")
	public String toedit(){
		return "/cargo/outproduct/jOutProduct.jsp";
	}

	//打印
	@RequestMapping("/cargo/outproduct/print.action")
	public void print(String inputDate){  //inputDate格式:yyyy-MM
		System.out.println(inputDate);
	}
}

这里我们先打印inputDate看看值有没有传过来

然后我们在我们页面的左侧菜单栏中添加我们的入口:

</div>
       <div class="panel">
       <div class="panel_icon"><img src="${ctx}/skin/default/images/icon/document_into.png"/></div>
       <div class="panel-header">
       <div class="panel-title">货运管理</div>
       <div class="panel-content">
		<ul>
			<li><a href="${ctx }/cargo/contract/list.action" onclick="linkHighlighted(this)" target="main" id="aa_1">购销合同</a></li>
			<li><a href="${ctx }/cargo/outproduct/toedit.action" onclick="linkHighlighted(this)" target="main" id="aa_1">出货表</a></li>
		</ul>
       </div>
       </div>
</div>

效果:

重启服务器后,点击“出货表”,就会看到如下界面

然后选择一个日期点击打印,在控制台就会看到我们选择的日期

所以我们的日期是可以正常传过去的。

我们接下来要进行出货表数据的获取了,我们之前分析过,精简到最后我们也是要查询合同和货物表的。

这里是我们获取出货表数据的SQL(以取出2014年11月的数据为例)

select
c.custom_name,c.contract_no,cp.product_no,cp.cnumber,cp.factory_name,cp.exts,c.delivery_period,c.ship_time,c.trade_terms
from
(
select
contract_id,custom_name,contract_no,delivery_period,ship_time,trade_terms
from contract_c
) c
left join
(
select
contract_id,product_no,cnumber||packing_unit as cnumber,factory_name,exts
from contract_product_c
) cp
on c.contract_id=cp.contract_id

where to_char(c.ship_time,'yyyy-MM')= '2014-11'

我们从上面的出货表信息可以看到,我们所需要查询的就是“客户”、“订单号”、“货号”、“数量”、“工厂”、“附件”、“工厂交期”、“船期”和“贸易条款”这几个字段。
所以我们可以自定义一个VO对象(包含上面这些数据),用来有选择性的用过SQL查询出需要的数据然后封装到我们自定义的VO对象

package cn.hpu.jk.vo;

public class OutProductVO {
	private String customName;
	private String contract_no;
	private String productNo;
	private String cnumber;
	private String factoryName;
	private String exts;
	private String delivery_preriod;
	private String ship_time;
	private String tradeTerms;

	//get和set方法省略
}

我们现在构造一个出货表的Mapper映射文件OutProductMapper.xml,在其中添加查询语句,主要来查询我们出货表需要的数据:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.hpu.jk.mapper.OutProductMapper">

	<resultMap type="cn.hpu.jk.vo.OutProductVO" id="OutProductRM">
		<result property="customName" column="CUSTOM_NAME" jdbcType="VARCHAR"/>
		<result property="contractNo" column="CONTRACT_NO" jdbcType="VARCHAR"/>
		<result property="productNo" column="PRODUCT_NO" jdbcType="VARCHAR"/>
		<result property="cnumber" column="CNUMBER" jdbcType="VARCHAR"/>
		<result property="factoryName" column="FACTORY_NAME" jdbcType="VARCHAR"/>
		<result property="exts" column="EXTS" jdbcType="VARCHAR"/>
		<result property="delivery_preriod" column="DELIVERY_PRERIOD" jdbcType="VARCHAR"/>
		<result property="ship_time" column="SHIP_TIME" jdbcType="VARCHAR"/>
		<result property="tradeTerms" column="TRADE_TERMS" jdbcType="VARCHAR"/>
	</resultMap>

	<select id="find" parameterType="string" resultMap="OutProductRM">
		select
		  c.custom_name,c.contract_no,cp.product_no,cp.cnumber,cp.factory_name,cp.exts,
		  to_char(c.delivery_period,'yyyy-mm-dd')as delivery_period,
		  to_char(c.ship_time,'yyyy-mm-dd')as ship_time,c.trade_terms
		from
		(
		select
		contract_id,custom_name,contract_no,delivery_period,ship_time,trade_terms
		from contract_c
		) c
		left join
		(
		select
		contract_id,product_no,cnumber||packing_unit as cnumber,factory_name,exts
		from contract_product_c
		) cp
		on c.contract_id=cp.contract_id

		where to_char(c.ship_time,'yyyy-MM')= #{inputDate}
	</select>
</mapper>

接下来开始编写我们的Dao层:
OutProductDao.java:

package cn.hpu.jk.dao;

import cn.hpu.jk.vo.OutProductVO;

public interface OutProductDao extends BaseDao<OutProductVO>{

}

OutProductDaoImpl.java:

package cn.hpu.jk.dao.impl;

import org.springframework.stereotype.Repository;

import cn.hpu.jk.dao.OutProductDao;
import cn.hpu.jk.vo.OutProductVO;

@Repository //为了包扫描的时候这个Dao被扫描到
public class OutProductDaoImpl extends BaseDaoImpl<OutProductVO> implements OutProductDao{

	public OutProductDaoImpl(){
		//设置命名空间
		super.setNs("cn.hpu.jk.mapper.OutProductMapper");
	}

}

接下来开始写Service层:
OutProductService.java:

package cn.hpu.jk.service;

import java.util.List;

import cn.hpu.jk.vo.OutProductVO;

public interface OutProductService {
	public List<OutProductVO> find(String inputDate);
}

OutProductServiceImpl.java:

package cn.hpu.jk.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.hpu.jk.dao.OutProductDao;
import cn.hpu.jk.service.OutProductService;
import cn.hpu.jk.vo.OutProductVO;

public class OutProductServiceImpl implements OutProductService{

	@Resource
	OutProductDao outProductDao;

	@Override
	public List<OutProductVO> find(String inputDate) {
		Map paraMap=new HashMap();
		paraMap.put("inputDate", inputDate);
		return outProductDao.find(paraMap);
	}

}

在beans-service.xml中注入我们的Service:

<bean name="outProductService" class="cn.hpu.jk.service.impl.OutProductServiceImpl"/>

回到我们之前的Controller类中,我们对print方法进行编写

package cn.hpu.jk.controller.cargo.outproduct;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.hpu.jk.controller.BaseController;
import cn.hpu.jk.service.OutProductService;
import cn.hpu.jk.vo.OutProductVO;

@Controller
public class OutProductController extends BaseController{

	@Resource
	OutProductService outProductService;

	//转向输入年月的页面
	@RequestMapping("/cargo/outproduct/toedit.action")
	public String toedit(){
		return "/cargo/outproduct/jOutProduct.jsp";
	}

	//打印
	@RequestMapping("/cargo/outproduct/print.action")
	public void print(String inputDate){  //inputDate格式:yyyy-MM

		List<OutProductVO> dataList=outProductService.find(inputDate);
		System.out.println(dataList.size());
		System.out.println(inputDate);
	}
}

我们在数据库中取出的2014年11月的是2条记录

我们实验一下,发现我们取出了2条,说明我们的find方法功能实现。

那么接下来我们就开始把取到的数据编写成excel形式,我们在打印方法中添加下面的代码:

//打印
@RequestMapping("/cargo/outproduct/print.action")
public void print(String inputDate) throws IOException{  //inputDate格式:yyyy-MM

	List<OutProductVO> dataList=outProductService.find(inputDate);
	/*System.out.println(dataList.size());
	System.out.println(inputDate);*/

	Workbook wb=new HSSFWorkbook();
	Sheet sheet=wb.createSheet();
	Row nRow=null;
	Cell nCell=null;

	int rowNo=0;  //行号
	int cellNo=1;//列号
	rowNo++;

	//配置标题行
	String [] title=new String[]{"客户","订单号","货号","数量","工厂","附件","工厂交期","船期","贸易条款"};

	nRow=sheet.createRow(rowNo++);
	for (int i = 0; i < title.length; i++) {
		nCell=nRow.createCell(i+1);
		nCell.setCellValue(title[i]);
	}

	//处理数据
	for (int i = 0; i < dataList.size(); i++) {
		OutProductVO op=dataList.get(i);

		nRow=sheet.createRow(rowNo++);
		cellNo=1;//列号初始化

		nCell=nRow.createCell(cellNo++);
		nCell.setCellValue(op.getCustomName());

		nCell=nRow.createCell(cellNo++);
		nCell.setCellValue(op.getcontractNo());

		nCell=nRow.createCell(cellNo++);
		nCell.setCellValue(op.getProductNo());

		nCell=nRow.createCell(cellNo++);
		nCell.setCellValue(op.getCnumber());

		nCell=nRow.createCell(cellNo++);
		nCell.setCellValue(op.getFactoryName());

		nCell=nRow.createCell(cellNo++);
		nCell.setCellValue(op.getExts());

		nCell=nRow.createCell(cellNo++);
		nCell.setCellValue(op.getDelivery_preriod());

		nCell=nRow.createCell(cellNo++);
		nCell.setCellValue(op.getShip_time());

		nCell=nRow.createCell(cellNo++);
		nCell.setCellValue(op.getTradeTerms());
	}

	OutputStream os=new FileOutputStream(new File("F:\\outproduct.xls"));
	wb.write(os);
	os.close();

}

然后我们去F盘下,看到生成了一个新的文件

打开这个文件,我们可以看到我们的出货表已经打印出来了,跟数据库中的一样

但是我们还需要修饰使表看起来更加好看,并添加下载链接,我们在下一篇继续讨论。

转载请注明出处:http://blog.csdn.net/acmman/article/details/48710311

时间: 2024-08-25 11:32:19

【springmvc+mybatis项目实战】杰信商贸-25.出货表打印的相关文章

【springmvc+mybatis项目实战】杰信商贸-26.出货表修饰+下载

上一次我们利用POI技术完成了出货表的取值.构建以及生产excel文件.下面我们继续利用POI技术来修饰我们之前构造的excel,然后将修饰完的excel能让用户下载. 回顾一下之前用代码生成的excel 我们的表格并不是很好看,而且大部分表格的长度不足以存储数据,我们要对这个表进行完整的修改. 记不记得我们之前的出货表样板: 我们现在没有标题,所以要给它加一个标题,这个标题是需要单元格合并的,所以我们在这里设置它 //大标题,合并单元格 sheet.addMergedRegion(new Ce

【springmvc+mybatis项目实战】杰信商贸-1.项目背景

1.项目背景 杰信项目物流行业的项目, 杰信商贸是国际物流行业一家专门从事进出口玻璃器皿贸易的公司.公司总部位于十一个朝代的帝王之都西安,业务遍及欧美.随着公司不断发展壮大,旧的信息系统已无法满足公司的快速发展需求,妨碍公司成长,在此背景下,公司领导决定研发<杰信商贸综合管理平台>. <杰信商贸综合管理平台>分三期完成.一期完成仓储管理(包括:采购单.仓库.货物.条形码.入库.出库.退货.盘点.库存.库存上限报警.统计查询)和展会管理(包括:展会管理.出单管理),形成货物统一数字化

【springmvc+mybatis项目实战】杰信商贸-13.购销合同列表

我们接下来开始编写购销合同的列表业务 首先我们现往我们的FACTORY_C和基础表中倒入一些数据方便我们测试 导入数据的SQL文件:jk.sql (点击下载) 然后我们看到数据已经插入 启动服务器查看我们的数据已经插入 接下来我们开始做购销合同的业务.首先我们的数据库表已经建立(CONTRACT_C),我们首先创建购销合同的实体类Contract.java: package cn.hpu.jk.domain; import java.util.Date; public class Contrac

【springmvc+mybatis项目实战】杰信商贸-31.出口报运业务-购销合同查询与上报

我们上一篇完成了出口报运增删查修的mapper+Dao+Service+Controller,而且在Controller中添加和获取出口报运的列表的方法,然后成功获取了出口报运的列表: 然后我们这次要添加出口报运的"新增"方法,但是这个"新增"和以往的新增方法不同,这个要与之前的购销合同关联在一起. 我们出口报运的"新增"是这样的: 用户要选择多个合同,然后进行报运.(体现出业务关联) 报运新增时,报运专责只能看到已上报的购销合同. (操作出口报

【springmvc+mybatis项目实战】杰信商贸-11.购销合同业务

生产厂家我们做完了,接下来我们要做一个更加复杂的业务,就是"货运管理" 首先我们先了解一下业务,什么是"货运管理"? "货运管理"是国际物流的概念,往常我们货物送到国内没有那么多事情,但是货物发往国外就多了很多手续,依靠小物流公司我们是很不可能的,需要行业的大背景的支撑.下面我们来看看它是怎么做的 a)业务:购销合同 当我们的客户通过展会.其它渠道跟杰信公司联系上以后,他们看上杰信的货物样品,他们会跟杰信签订一个购买合同(样式要求,货物数量),客

【springmvc+mybatis项目实战】杰信商贸-15.细粒度的权限控制+业务上报取消

上一篇总结我们完成了购销合同的增删改查业务,这一篇我们首先完成权限控制以及业务的上报取消的设置功能. 先说我们的权限控制 1.细粒度的权限控制 a)日常权限框架: 基于角色权限,用户.角色.权限(URL.主菜单.左侧菜单.按钮) b)数据权限: 纵向的数据权限过滤:对数据进行过滤 1)本人(专责):登录后只能看到自己的信息Where条件 create_by = #{当前登录者id} 2)部门(集团公司):登录后登录人是经理级别A.只能看本部门Where条件 create_dept=#{当前登录者

【springmvc+mybatis项目实战】杰信商贸-30.出口报运增删查修mapper+Dao+Service+Controller

我们接下来做我们项目的下一个环节,就是出口报运的业务,首先我们来看看两个设计方式 a)大型项目设计方式 传统设计方式,基于对象关联,数据量小时,系统无碍:当数据随着系统的使用,线性增长,系统变的越来越慢,到达一定数据量时,性能急剧下降. b)新的设计方式:打断设计 在表中增加一个字段,这个字段用来存储关联表的主键集合:在报运业务中要查询合同下的货物信息.直接通过这个关联字段,利用in子查询直接去查询货物表,这样查询效率提高很多.而且数据量越大,查询效率越高.这种方式,业务关联层级越多,这种设计方

【springmvc+mybatis项目实战】杰信商贸-4.maven依赖+PO对+映射文件

我们来为刚刚创建的数据库表来在工程中创建PO对象 首先我们要创建工程 项目的开发环境 序号 工  具                用  途 1 MyEclipse 2014   IDE 2 JDK 1.7.9        Java虚拟机 3 TOMCAT 7.0.26       中间件 4 Oracle11g/10g.MySQL 5.0.87数据库 5 PL/SQL .SQLyog 8.2SQL控制台 6 apache-maven-3.0.5MAVEN 7 PowerDesigner 15

【springmvc+mybatis项目实战】杰信商贸-8.生产厂家修改

上一次我们做了生产厂家的新增,下面我们来做一下生产厂家的修改 回顾一下我们的FactoryMapper.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">