【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-07 19:09:20

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

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

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

【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">

【springmvc+mybatis项目实战】杰信商贸-6.重点知识回顾

1.重点知识回顾 Maven 1)覆盖仓库文件,实际企业开发,公司会架一个测试服务器,在测试服务器中架私服.我们开发人员的程序,都连接私服.当本地没有项目中要使用的jar,Myeclipse maven插件会自动到私服去找jar,如果没找到去中央仓库maven寻找,找到后下载.activiti-engine-5.13.jar.lastUpdated 当访问远程仓库时,由于网络不稳定,有可能中断.当程序再次连接,它会自动修正.Pom文件报错,jar错误,去仓库目录找jar,jar存在,点击jar开

【springmvc+mybatis项目实战】杰信商贸-7.生产厂家新增

我们来接着我们的项目写 我们要实现新的功能,就是生产厂家的新增 先来回顾一下系统架构图 我们数据库这边已经建好表了,接下来要做的就是mapper映射 编辑FactoryMapper.xml文件,加入"添加"的逻辑配置代码块 <!-- 新增 oracle jbdc驱动当这个值为null时,必须告诉它当前字段 默认值的类型jdbcType=VARCHAR(MyBatis定义),Mysql不用写--> <insert id="insert" parame

【springmvc+mybatis项目实战】杰信商贸-23.重点知识回顾

1.重点知识回顾 购销合同查看,采用类似hibernate方式,都以对象关联方式. (1)PO为了利用MyBatis性能,在创建时,没有采用关联对象关联,而是将对象关键字段,也就是外键,利用这个普通属性,来记录值,表数据间关联关系存在,但对象关联关系不存在.代码也就变得简单.在货物新增时,只要从主对象中携带过来,主表ID即可. (2)VO为了方便对象关联时取数据.在列表循环货物信息时,要去查询当前货物下的附件时,如果采用上面的方式,只能再次查询.但是我们以对象关联方式,可以直接获取到当前货物下的

【springmvc+mybatis项目实战】杰信商贸-22.合同货物附件生产厂家mapper关联

我们上一次完成了合同的总金额的计算,我们这次继续完成一个新的更复杂,但是更有学习意义的业务---购销合同查看 要求:查看合同的主信息,查看合同下的货物信息,还要查看附件信息,货物和附件的信息要显示出它们的关联关系. (注:一个合同下有多个货物,每个货物又有每个货物自己的附件) 利用面对对象的关联关系来实现上面的需求非常简单. 我们之前都是使用的外键进行的关联,这里我们将外键改为实实在在的一个对象,到时候通过对象属性来取下一级的数据,假设之前我们表中有一个货物的外键private String c

【springmvc+mybatis项目实战】杰信商贸-29.购销合同技术难点分析

我们接下来要进行最复杂的一个打印,就是购销合同的打印---制式表单的打印,业界最复杂的报表打印. a)分析技术难点: 1)插入图片,POI插入图片时,设定区域,自动缩放到这个区域中,图片要设置偏移量 HSSFPatriarch patriarch = sheet.createDrawingPatriarch();//add picture poiUtil.setPicture(wb, patriarch, path+"make/xlsprint/logo.jpg", curRow, 2

【springmvc+mybatis项目实战】杰信商贸-20.合同货物数和附件数

我们上一篇完成了购销合同.货物以及附件的级联删除,这次我们需要做的业务就是----要求直接显示合同下的货物数和附件数 上一次我们遗留的问题: 我们每次删除完都要去下一级或者数据库看看我们删除了没有,我们可以直接在合同列表中显示每个合同下的货物以及货物的附件有多少件,这样就避免了反复去下一级查看列表的繁琐操作. 那么下面我们使用SQL来编写获取合同下的货物数: select count(*) from contract_product_c where contract_id='928eb2ae-2

【springmvc+mybatis项目实战】杰信商贸-34.业务出口报运WebService1

我们要为出口报运做一个WebService,来提供跨系统的信息查询功能. 我们使用的技术是 -------Apache CXF WebService 作用:两个异构系统,需要共享数据. 需求:我们要给客户提供合同追踪.在出口报运中增加一个WebService,用户可以通过它的系统来访问这个服务,展现出口报运单,主要可以浏览用户的订单状态(走到哪个流程).查看出口报运单 开发步骤:将现有的Service改造成WebService 1)将CXF整合到项目中,加入jar包.依赖jar.我们系统才 CX