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

我们接下来要进行最复杂的一个打印,就是购销合同的打印---制式表单的打印,业界最复杂的报表打印。

a)分析技术难点:

1)插入图片,POI插入图片时,设定区域,自动缩放到这个区域中,图片要设置偏移量

HSSFPatriarch patriarch = sheet.createDrawingPatriarch();//add picture

poiUtil.setPicture(wb, patriarch, path+"make/xlsprint/logo.jpg", curRow, 2, curRow+4, 2);

2)模板,对于购销合同它不能直接做模板。代码中不能做这个功能。Excel支持区域复制。但是目前POI不直接支持区域复制。(合并单元格在sheet对象中、图片、线等)(基于用户使用系统,以及POI不直接支持区域复制,所以它不能直接使用模板)

A.只是改变列宽

B.画一个sheet,利用poi支持sheet的复制,保留样式。

3)插入一根线,设置两个坐标,它直接连线,可以是直线,可以是斜线,可以设置偏移量

poiUtil.setLine(wb, patriarch, curRow, 2, curRow, 8);//draw line

4)合并单元格,特点:合并单元格的边线样式,它实际上是在合并前的单元格上设置的。特殊的地方:在单元格如果有样式,合并单元格的这几个格子都必须设置边线样式。

CellRangeAddress region = null;

region = new CellRangeAddress(curRow-1, curRow-1, 1, 3);//纵向合并单元格

不需要设置内容,只设置样式,补齐线。

for(int j=2;j<9;j++){

nCell = nRow.createCell(j);

nCell.setCellStyle(poiUtil.notehv10_BorderThin(wb, defaultFont10));

}

5)换行

curStyle.setWrapText(true);

6)设置人民币符号(前缀)

format.getFormat("\"¥\"#,###,###.00");

7)设置公式,乘法,合计公式

nCell.setCellType(HSSFCell.CELL_TYPE_FORMULA);

nCell.setCellFormula("F"+String.valueOf(curRow)+"*H"+String.valueOf(curRow));

8)实现审单人的人名等量空格替换,实现它后面的位置不变(工具类)

utilFuns.fixSpaceStr(contract.getCheckBy(),26)

9)单元格自适应(工具类)

float height = poiUtil.getCellAutoHeight(printMap.get("Request"), 12f);//自动高度

10)数据和业务分离

一页数据封装到一个map中,以业务字段名称定义KEY,以string定义内容

所有页面封装到一个arraylist。

Map<String,String> pageMap = null;

List<Map> pageList = new ArrayList();//打印页

11)插入分页符

if(p>0){

sheet.setRowBreak(curRow++);//在第startRow行设置分页符

}

12)公用的工具类PoiUtil poiUtil = new PoiUtil();  样式,特殊内容封装

13)return format.getFormat("\"¥\"#,###,##0.00"); // 设置格式

0代表,这位值如果不为零,显示原来的内容,直接输出0

#代表,这位值如果存在,就直接显示,不存在,就不显示。

14)nCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);

购销合同最总打印样式:

实现代码:

package cn.itcast.jk.print;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;

import cn.itcast.jk.vo.ContractProductVO;
import cn.itcast.jk.vo.ContractVO;
import cn.itcast.util.DownloadUtil;
import cn.itcast.util.UtilFuns;
import cn.itcast.util.file.PoiUtil;

//手工POI写excel文件
public class ContractPrint{

	public void print(ContractVO contract,String path, HttpServletResponse response) throws Exception{
		//相同厂家的信息一起打印
		List<ContractProductVO> oList = contract.getContractProducts();
		UtilFuns utilFuns = new UtilFuns();
		String tempXlsFile = path + "make/xlsprint/tCONTRACT.xls";		//获取模板文件

		//填写每页的内容,之后在循环每页读取打印
		Map<String,String> pageMap = null;
		List<Map> pageList = new ArrayList();			//打印页

		ContractProductVO oProduct = null;
		String stars = "";
		for(int j=0;j<contract.getImportNum();j++){		//重要程度
			stars += "★";
		}

		String oldFactory = "";
		for(int i=0;i<oList.size();i++){
			oProduct = oList.get(i);	//获得货物
			pageMap = new HashMap();	//每页的内容

			pageMap.put("Offeror", "收 购 方:" + contract.getOfferor());
			pageMap.put("Factory", "生产工厂:" + oProduct.getFactory().getFactoryName());
			pageMap.put("ContractNo", "合 同 号:" + contract.getContractNo());
			pageMap.put("Contractor", "联 系 人:" + oProduct.getFactory().getContractor());
			pageMap.put("SigningDate", "签单日期:"+UtilFuns.formatDateTimeCN(UtilFuns.dateTimeFormat(contract.getSigningDate())));
			pageMap.put("Phone", "电    话:" + oProduct.getFactory().getPhone());
			pageMap.put("InputBy", "制单:" + contract.getInputBy());
			pageMap.put("CheckBy", "审单:"+ utilFuns.fixSpaceStr(contract.getCheckBy(),26)+"验货员:"+utilFuns.convertNull(contract.getInspector()));
			pageMap.put("Remark", "  "+contract.getRemark());
			pageMap.put("Request", "  "+contract.getCrequest());

			pageMap.put("ProductImage", oProduct.getProductImage());
			pageMap.put("ProductDesc", oProduct.getProductDesc());
			pageMap.put("Cnumber", String.valueOf(oProduct.getCnumber().doubleValue()));
			if(oProduct.getPackingUnit().equals("PCS")){
				pageMap.put("PackingUnit", "只");
			}else if(oProduct.getPackingUnit().equals("SETS")){
				pageMap.put("PackingUnit", "套");
			}
			pageMap.put("Price", String.valueOf(oProduct.getPrice().doubleValue()));
			pageMap.put("ProductNo", oProduct.getProductNo());

			oldFactory = oProduct.getFactory().getFactoryName();

			if(contract.getPrintStyle().equals("2")){
				i++;	//读取第二个货物信息
				if(i<oList.size()){
					oProduct = oList.get(i);

					if(oProduct.getFactory().getFactoryName().equals(oldFactory)){	//厂家不同另起新页打印,除去第一次的比较

						pageMap.put("ProductImage2", oProduct.getProductImage());
						pageMap.put("ProductDesc2", oProduct.getProductDesc());
						pageMap.put("Cnumber2", String.valueOf(oProduct.getCnumber().doubleValue()));
						if(oProduct.getPackingUnit().equals("PCS")){
							pageMap.put("PackingUnit2", "只");
						}else if(oProduct.getPackingUnit().equals("SETS")){
							pageMap.put("PackingUnit2", "套");
						}
						pageMap.put("Price2", String.valueOf(oProduct.getPrice().doubleValue()));
						//pageMap.put("Amount2", String.valueOf(oProduct.getAmount().doubleValue()));			//在excel中金额采用公式,所以无需准备数据
						pageMap.put("ProductNo2", oProduct.getProductNo());
					}else{
						i--;	//tip:list退回
					}
				}else{
					pageMap.put("ProductNo2", null);	//后面依据此判断是否有第二个货物
				}
			}

			pageMap.put("ContractDesc", stars+" 货物描述");			//重要程度 + 货物描述

			pageList.add(pageMap);
		}

		int cellHeight = 96;	//一个货物的高度			用户需求,一个货物按192高度打印,后来又嫌难看,打印高度和2款高度一样。
//		if(contract.getPrintStyle().equals("2")){
//			cellHeight = 96;	//两个货物的高度
//		}

		PoiUtil poiUtil = new PoiUtil();
		HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(tempXlsFile));	//打开excel文件
		HSSFFont defaultFont10 = poiUtil.defaultFont10(wb);		//设置字体
		HSSFFont defaultFont12 = poiUtil.defaultFont12(wb);		//设置字体
		HSSFFont blackFont = poiUtil.blackFont12(wb);			//设置字体
		Short rmb2Format = poiUtil.rmb2Format(wb);				//设置格式
		Short rmb4Format = poiUtil.rmb4Format(wb);				//设置格式

		HSSFSheet sheet = wb.getSheetAt(0);				//选择第一个工作簿
		wb.setSheetName(0, "购销合同");					//设置工作簿的名称

		//sheet.setDefaultColumnWidth((short) 20); 		// 设置每列默认宽度

//		POI分页符有BUG,必须在模板文件中插入一个分页符,然后再此处删除预设的分页符;最后在下面重新设置分页符。
//		sheet.setAutobreaks(false);
//		int iRowBreaks[] = sheet.getRowBreaks();
//		sheet.removeRowBreak(3);
//		sheet.removeRowBreak(4);
//		sheet.removeRowBreak(5);
//		sheet.removeRowBreak(6);

		CellRangeAddress region = null;
		HSSFPatriarch patriarch = sheet.createDrawingPatriarch();		//add picture

		HSSFRow nRow = null;
		HSSFCell nCell   = null;
		int curRow = 0;

		//打印每页
		Map<String,String> printMap = null;
		for(int p=0;p<pageList.size();p++){
			printMap = pageList.get(p);

			if(p>0){
				sheet.setRowBreak(curRow++);	//在第startRow行设置分页符
			}

			//设置logo图片
			poiUtil.setPicture(wb, patriarch, path+"make/xlsprint/logo.jpg", curRow, 2, curRow+4, 2);

			//header
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(21);

			nCell   = nRow.createCell((3));
			nCell.setCellValue("SHAANXI");
			nCell.setCellStyle(headStyle(wb));

			//header
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(41);

			nCell   = nRow.createCell((3));
			nCell.setCellValue("     JK INTERNATIONAL ");
			nCell.setCellStyle(tipStyle(wb));

			curRow++;

			//header
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(20);

			nCell   = nRow.createCell((1));
			nCell.setCellValue("                 西经济技术开发区西城一路27号无迪大厦19楼");
			nCell.setCellStyle(addressStyle(wb));

			//header
			nCell   = nRow.createCell((6));
			nCell.setCellValue(" CO., LTD.");
			nCell.setCellStyle(ltdStyle(wb));

			//header
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(15);

			nCell   = nRow.createCell((1));
			nCell.setCellValue("                   TEL: 0086-29-86339371  FAX: 0086-29-86303310               E-MAIL: [email protected]");
			nCell.setCellStyle(telStyle(wb));

			//line
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(7);

			poiUtil.setLine(wb, patriarch, curRow, 2, curRow, 8);	//draw line

			//header
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(30);

			nCell   = nRow.createCell((4));
			nCell.setCellValue("    购   销   合   同");
			nCell.setCellStyle(titleStyle(wb));

			//Offeror
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(20);

			nCell   = nRow.createCell((1));
			nCell.setCellValue(printMap.get("Offeror"));
			nCell.setCellStyle(poiUtil.titlev12(wb, blackFont));

			//Facotry
			nCell   = nRow.createCell((5));
			nCell.setCellValue(printMap.get("Factory"));
			nCell.setCellStyle(poiUtil.titlev12(wb, blackFont));

			//ContractNo
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(20);

			nCell   = nRow.createCell(1);
			nCell.setCellValue(printMap.get("ContractNo"));
			nCell.setCellStyle(poiUtil.titlev12(wb, blackFont));

			//Contractor
			nCell  = nRow.createCell(5);
			nCell.setCellValue(printMap.get("Contractor"));
			nCell.setCellStyle(poiUtil.titlev12(wb, blackFont));

			//SigningDate
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(20);

			nCell = nRow.createCell(1);
			nCell.setCellValue(printMap.get("SigningDate"));
			nCell.setCellStyle(poiUtil.titlev12(wb, blackFont));

			//Phone
			nCell = nRow.createCell(5);
			nCell.setCellValue(printMap.get("Phone"));
			nCell.setCellStyle(poiUtil.titlev12(wb, blackFont));

			//importNum
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(24);

			region = new CellRangeAddress(curRow-1, curRow-1, 1, 3);	//纵向合并单元格
			sheet.addMergedRegion(region);

			nCell = nRow.createCell(1);
			nCell.setCellValue("产品");
			nCell.setCellStyle(thStyle(wb));		

			nCell = nRow.createCell(2);
			nCell.setCellStyle(poiUtil.notehv10_BorderThin(wb, defaultFont10));

			nCell = nRow.createCell(3);
			nCell.setCellStyle(poiUtil.notehv10_BorderThin(wb, defaultFont10));

			nCell = nRow.createCell(4);
			nCell.setCellValue(printMap.get("ContractDesc"));
			nCell.setCellStyle(thStyle(wb));	

			region = new CellRangeAddress(curRow-1, curRow-1, 5, 6);	//纵向合并单元格
			sheet.addMergedRegion(region);

			nCell = nRow.createCell(5);
			nCell.setCellValue("数量");
			nCell.setCellStyle(thStyle(wb));	

			nCell = nRow.createCell(6);
			nCell.setCellStyle(poiUtil.notehv10_BorderThin(wb, defaultFont10));			

			nCell = nRow.createCell(7);
			nCell.setCellValue("单价");
			nCell.setCellStyle(thStyle(wb));						

			nCell = nRow.createCell(8);
			nCell.setCellValue("总金额");
			nCell.setCellStyle(thStyle(wb));						

			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(96);

			region = new CellRangeAddress(curRow-1, curRow-1, 1, 3);	//纵向合并单元格
			sheet.addMergedRegion(region);

			//插入产品图片
			if(UtilFuns.isNotEmpty(printMap.get("ProductImage"))){
				System.out.println(printMap.get("ProductImage"));
				poiUtil.setPicture(wb, patriarch, path+"ufiles/jquery/"+printMap.get("ProductImage"), curRow-1, 1, curRow, 3);
			}

			nCell = nRow.createCell(2);
			nCell.setCellStyle(poiUtil.notehv10_BorderThin(wb, defaultFont10));

			nCell = nRow.createCell(3);
			nCell.setCellStyle(poiUtil.notehv10_BorderThin(wb, defaultFont10));

			//ProductDesc
			region = new CellRangeAddress(curRow-1, curRow, 4, 4);	//纵向合并单元格
			sheet.addMergedRegion(region);

			nCell = nRow.createCell(4);
			nCell.setCellValue(printMap.get("ProductDesc"));
			nCell.setCellStyle(poiUtil.notehv10_BorderThin(wb, defaultFont10));		

			//Cnumber
			region = new CellRangeAddress(curRow-1, curRow, 5, 5);	//纵向合并单元格
			sheet.addMergedRegion(region);

			nCell = nRow.createCell(5);
			nCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			nCell.setCellValue(Double.parseDouble(printMap.get("Cnumber")));
			nCell.setCellStyle(poiUtil.numberrv10_BorderThin(wb, defaultFont10));	

			//Unit
			region = new CellRangeAddress(curRow-1, curRow, 6, 6);	//纵向合并单元格
			sheet.addMergedRegion(region);

			nCell = nRow.createCell(6);
			nCell.setCellValue(printMap.get("PackingUnit"));
			nCell.setCellStyle(poiUtil.moneyrv10_BorderThin(wb, defaultFont10, rmb4Format));	

			//Price
			region = new CellRangeAddress(curRow-1, curRow, 7, 7);	//纵向合并单元格
			sheet.addMergedRegion(region);

			nCell = nRow.createCell(7);
			nCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
			nCell.setCellValue(Double.parseDouble(printMap.get("Price")));
			nCell.setCellStyle(poiUtil.moneyrv10_BorderThin(wb, defaultFont10, rmb4Format));

			//Amount
			region = new CellRangeAddress(curRow-1, curRow, 8, 8);	//纵向合并单元格
			sheet.addMergedRegion(region);

			nCell = nRow.createCell(8);
			if(UtilFuns.isNotEmpty(printMap.get("Cnumber")) && UtilFuns.isNotEmpty(printMap.get("Price"))){
				nCell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
				nCell.setCellFormula("F"+String.valueOf(curRow)+"*H"+String.valueOf(curRow));
			}
			nCell.setCellStyle(poiUtil.moneyrv10_BorderThin(wb, defaultFont10, rmb4Format));			

			curRow++;

			region = new CellRangeAddress(curRow-1, curRow-1, 1, 3);	//纵向合并单元格
			sheet.addMergedRegion(region);

			//ProductNo
			nRow = sheet.createRow(curRow-1);
			nRow.setHeightInPoints(24);

			nCell = nRow.createCell(1);
			nCell.setCellValue(printMap.get("ProductNo"));
			nCell.setCellStyle(poiUtil.notecv10_BorderThin(wb, defaultFont10));

			for(int j=2;j<9;j++){
				nCell = nRow.createCell(j);
				nCell.setCellStyle(poiUtil.notehv10_BorderThin(wb, defaultFont10));
			}

			if(contract.getPrintStyle().equals("2") && UtilFuns.isNotEmpty(printMap.get("ProductNo2"))){
				nRow = sheet.createRow(curRow++);
				nRow.setHeightInPoints(96);

				region = new CellRangeAddress(curRow-1, curRow-1, 1, 3);	//纵向合并单元格
				sheet.addMergedRegion(region);

				//插入产品图片
				if(UtilFuns.isNotEmpty(printMap.get("ProductImage2"))){
					System.out.println(printMap.get("ProductImage2"));
					poiUtil.setPicture(wb, patriarch, path+"ufiles/jquery/"+printMap.get("ProductImage2"), curRow-1, 1, curRow, 3);
				}

				//ProductDesc
				region = new CellRangeAddress(curRow-1, curRow, 4, 4);	//纵向合并单元格
				sheet.addMergedRegion(region);

				nCell = nRow.createCell(4);
				nCell.setCellValue(printMap.get("ProductDesc2"));
				nCell.setCellStyle(poiUtil.notehv10_BorderThin(wb, defaultFont10));		

				//Cnumber
				region = new CellRangeAddress(curRow-1, curRow, 5, 5);	//纵向合并单元格
				sheet.addMergedRegion(region);

				nCell = nRow.createCell(5);
				nCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
				nCell.setCellValue(Double.parseDouble(printMap.get("Cnumber2")));
				nCell.setCellStyle(poiUtil.numberrv10_BorderThin(wb, defaultFont10));	

				//Unit
				region = new CellRangeAddress(curRow-1, curRow, 6, 6);	//纵向合并单元格
				sheet.addMergedRegion(region);

				nCell = nRow.createCell(6);
				nCell.setCellValue(printMap.get("PackingUnit2"));
				nCell.setCellStyle(poiUtil.moneyrv10_BorderThin(wb, defaultFont10, rmb4Format));	

				//Price
				region = new CellRangeAddress(curRow-1, curRow, 7, 7);	//纵向合并单元格
				sheet.addMergedRegion(region);

				nCell = nRow.createCell(7);
				nCell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
				nCell.setCellValue(Double.parseDouble(printMap.get("Price2")));
				nCell.setCellStyle(poiUtil.moneyrv10_BorderThin(wb, defaultFont10, rmb4Format));

				//Amount
				region = new CellRangeAddress(curRow-1, curRow, 8, 8);	//纵向合并单元格
				sheet.addMergedRegion(region);

				nCell = nRow.createCell(8);
				if(UtilFuns.isNotEmpty(printMap.get("Cnumber2")) && UtilFuns.isNotEmpty(printMap.get("Price2"))){
					nCell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
					nCell.setCellFormula("F"+String.valueOf(curRow)+"*H"+String.valueOf(curRow));
				}
				nCell.setCellStyle(poiUtil.moneyrv10_BorderThin(wb, defaultFont10, rmb4Format));		

				curRow++;

				region = new CellRangeAddress(curRow-1, curRow-1, 1, 3);	//纵向合并单元格
				sheet.addMergedRegion(region);

				nRow = sheet.createRow(curRow-1);
				nRow.setHeightInPoints(24);

				nCell = nRow.createCell(1);
				nCell.setCellValue(printMap.get("ProductNo2"));
				nCell.setCellStyle(poiUtil.notecv10_BorderThin(wb, defaultFont10));			

				//合并单元格画线
				for(int j=2;j<9;j++){
					nCell = nRow.createCell(j);
					nCell.setCellStyle(poiUtil.notehv10_BorderThin(wb, defaultFont10));
				}
			}

			//InputBy
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(24);

			nCell = nRow.createCell(1);
			nCell.setCellValue(printMap.get("InputBy"));
			nCell.setCellStyle(poiUtil.bnormalv12(wb,defaultFont12));

			//CheckBy+inspector

			nCell = nRow.createCell(4);
			nCell.setCellValue(printMap.get("CheckBy"));
			nCell.setCellStyle(poiUtil.bnormalv12(wb,defaultFont12));

			//if(contract.getPrintStyle().equals("2") && UtilFuns.isNotEmpty(printMap.get("ProductNo2"))){

				nCell = nRow.createCell(7);
				nCell.setCellValue("总金额:");
				nCell.setCellStyle(bcv12(wb));

				//TotalAmount
				nRow = sheet.createRow(curRow-1);
				nRow.setHeightInPoints(24);
				if(UtilFuns.isNotEmpty(printMap.get("Cnumber"))&&UtilFuns.isNotEmpty(printMap.get("Price"))){
					nCell  = nRow.createCell(8);
					nCell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
					nCell.setCellFormula("SUM(I"+String.valueOf(curRow-4)+":I"+String.valueOf(curRow-1)+")");
					nCell.setCellStyle(poiUtil.moneyrv12_BorderThin(wb,defaultFont12,rmb2Format));
				}
			//}

			//note
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(21);

			nCell = nRow.createCell(2);
			nCell.setCellValue(printMap.get("Remark"));
			nCell.setCellStyle(noteStyle(wb));			

			//Request
			region = new CellRangeAddress(curRow, curRow, 1, 8);	//指定合并区域
			sheet.addMergedRegion(region);

			nRow = sheet.createRow(curRow++);
			float height = poiUtil.getCellAutoHeight(printMap.get("Request"), 12f);		//自动高度
			nRow.setHeightInPoints(height);

			nCell = nRow.createCell(1);
			nCell.setCellValue(printMap.get("Request"));
			nCell.setCellStyle(requestStyle(wb));

			//space line
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(20);

			//duty
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(32);

			nCell = nRow.createCell(1);
			nCell.setCellValue("未按以上要求出货而导致客人索赔,由供方承担。");
			nCell.setCellStyle(dutyStyle(wb));	

			//space line
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(32);

			//buyer
			nRow = sheet.createRow(curRow++);
			nRow.setHeightInPoints(25);

			nCell = nRow.createCell(1);
			nCell.setCellValue("    收购方负责人:");
			nCell.setCellStyle(dutyStyle(wb));				

			//seller
			nCell = nRow.createCell(5);
			nCell.setCellValue("供方负责人:");
			nCell.setCellStyle(dutyStyle(wb));	

			curRow++;

		}

		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();			//生成流对象
		wb.write(byteArrayOutputStream);													//将excel写入流

		//工具类,封装弹出下载框:
		String outFile = "购销合同.xls";
		DownloadUtil down = new DownloadUtil();
		down.download(byteArrayOutputStream, response, outFile);

	}

	private HSSFCellStyle leftStyle(HSSFWorkbook wb){
		HSSFCellStyle curStyle = wb.createCellStyle();
		curStyle.setWrapText(true);  						//换行
		HSSFFont curFont = wb.createFont();					//设置字体
		curFont.setCharSet(HSSFFont.DEFAULT_CHARSET);		//设置中文字体,那必须还要再对单元格进行编码设置
		//fTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);	//加粗
		curFont.setFontHeightInPoints((short)10);
		curStyle.setFont(curFont);
		curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);		//单元格垂直居中

		curStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);				//实线右边框
		curStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);				//实线右边框

		return curStyle;
	}  

	private HSSFCellStyle headStyle(HSSFWorkbook wb){
		HSSFCellStyle curStyle = wb.createCellStyle();
		HSSFFont curFont = wb.createFont();					//设置字体
		curFont.setFontName("Comic Sans MS");
		curFont.setCharSet(HSSFFont.DEFAULT_CHARSET);		//设置中文字体,那必须还要再对单元格进行编码设置

		curFont.setItalic(true);
		curFont.setFontHeightInPoints((short)16);
		curStyle.setFont(curFont);
		curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);		//单元格垂直居中

		return curStyle;
	}  

	private HSSFCellStyle tipStyle(HSSFWorkbook wb){
		HSSFCellStyle curStyle = wb.createCellStyle();
		HSSFFont curFont = wb.createFont();					//设置字体
		curFont.setFontName("Georgia");
		curFont.setCharSet(HSSFFont.DEFAULT_CHARSET);		//设置中文字体,那必须还要再对单元格进行编码设置

		curFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);	//加粗
		curFont.setFontHeightInPoints((short)28);
		curStyle.setFont(curFont);
		curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);		//单元格垂直居中

		return curStyle;
	}  

	private HSSFCellStyle addressStyle(HSSFWorkbook wb){
		HSSFCellStyle curStyle = wb.createCellStyle();
		HSSFFont curFont = wb.createFont();					//设置字体
		curFont.setFontName("宋体");
		curFont.setCharSet(HSSFFont.DEFAULT_CHARSET);		//设置中文字体,那必须还要再对单元格进行编码设置

		//fTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);	//加粗
		curFont.setFontHeightInPoints((short)10);
		curStyle.setFont(curFont);
		curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);		//单元格垂直居中

		return curStyle;
	}  

	private HSSFCellStyle ltdStyle(HSSFWorkbook wb){
		HSSFCellStyle curStyle = wb.createCellStyle();
		HSSFFont curFont = wb.createFont();					//设置字体
		curFont.setFontName("Times New Roman");
		curFont.setCharSet(HSSFFont.DEFAULT_CHARSET);		//设置中文字体,那必须还要再对单元格进行编码设置

		curFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);	//加粗
		curFont.setItalic(true);
		curFont.setFontHeightInPoints((short)16);
		curStyle.setFont(curFont);
		curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);		//单元格垂直居中

		return curStyle;
	} 	

	private HSSFCellStyle telStyle(HSSFWorkbook wb){
		HSSFCellStyle curStyle = wb.createCellStyle();
		HSSFFont curFont = wb.createFont();					//设置字体
		curFont.setFontName("宋体");
		curFont.setCharSet(HSSFFont.DEFAULT_CHARSET);		//设置中文字体,那必须还要再对单元格进行编码设置

		//fTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);	//加粗
		curFont.setFontHeightInPoints((short)9);
		curStyle.setFont(curFont);
		curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);		//单元格垂直居中

		return curStyle;
	} 	

	private HSSFCellStyle titleStyle(HSSFWorkbook wb){
		HSSFCellStyle curStyle = wb.createCellStyle();
		HSSFFont curFont = wb.createFont();					//设置字体
		curFont.setFontName("黑体");
		curFont.setCharSet(HSSFFont.DEFAULT_CHARSET);		//设置中文字体,那必须还要再对单元格进行编码设置

		curFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);	//加粗
		curFont.setFontHeightInPoints((short)18);
		curStyle.setFont(curFont);
		curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);		//单元格垂直居中

		return curStyle;
	} 	

	private HSSFCellStyle requestStyle(HSSFWorkbook wb){
		HSSFCellStyle curStyle = wb.createCellStyle();
		curStyle.setWrapText(true);  						//换行
		HSSFFont curFont = wb.createFont();					//设置字体
		curFont.setFontName("宋体");
		curFont.setCharSet(HSSFFont.DEFAULT_CHARSET);		//设置中文字体,那必须还要再对单元格进行编码设置

		curFont.setFontHeightInPoints((short)10);
		curStyle.setFont(curFont);
		curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);		//单元格垂直居中

		return curStyle;
	} 	

	private HSSFCellStyle dutyStyle(HSSFWorkbook wb){
		HSSFCellStyle curStyle = wb.createCellStyle();
		HSSFFont curFont = wb.createFont();					//设置字体
		curFont.setFontName("黑体");
		curFont.setCharSet(HSSFFont.DEFAULT_CHARSET);		//设置中文字体,那必须还要再对单元格进行编码设置

		curFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);	//加粗
		curFont.setFontHeightInPoints((short)16);
		curStyle.setFont(curFont);
		curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);		//单元格垂直居中

		return curStyle;
	} 	

	private HSSFCellStyle noteStyle(HSSFWorkbook wb){
		HSSFCellStyle curStyle = wb.createCellStyle();
		HSSFFont curFont = wb.createFont();					//设置字体
		curFont.setFontName("宋体");
		curFont.setCharSet(HSSFFont.DEFAULT_CHARSET);		//设置中文字体,那必须还要再对单元格进行编码设置

		curFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);	//加粗
		curFont.setFontHeightInPoints((short)12);
		curStyle.setFont(curFont);
		curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);		//单元格垂直居中

		return curStyle;
	} 

	public HSSFCellStyle thStyle(HSSFWorkbook wb){
		HSSFCellStyle curStyle = wb.createCellStyle();
		HSSFFont curFont = wb.createFont();					//设置字体
		curFont.setFontName("宋体");
		curFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);	//加粗
		curFont.setFontHeightInPoints((short)12);
		curFont.setCharSet(HSSFFont.DEFAULT_CHARSET);		//设置中文字体,那必须还要再对单元格进行编码设置

		curStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);				//实线右边框
		curStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);				//实线右边框
		curStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);			//实线右边框
		curStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);				//实线右边框

		curStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);		//单元格垂直居中

		return curStyle;
	}  

	public HSSFCellStyle bcv12(HSSFWorkbook wb){
		HSSFCellStyle curStyle = wb.createCellStyle();
		HSSFFont curFont = wb.createFont();						//设置字体
		curFont.setFontName("Times New Roman");
		curFont.setCharSet(HSSFFont.DEFAULT_CHARSET);			//设置中文字体,那必须还要再对单元格进行编码设置

		curFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);		//加粗
		curFont.setFontHeightInPoints((short)12);
		curStyle.setFont(curFont);

		curStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);				//实线
		curStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);			//粗实线
		curStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);			//实线
		curStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);				//实线

		curStyle.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
		curStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);		//单元格垂直居中

		return curStyle;
	}		

}

打印出来的和前面的样板一样,至此我们完成购销合同的打印工作。

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-08 11:58:28

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

【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项目实战】杰信商贸-30.出口报运增删查修mapper+Dao+Service+Controller

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

【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项目实战】杰信商贸-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

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

我们之前学习了POI技术,可以利用POI进行自定义excel文件的生成.我们接下来就将利用这一技术来实现我们的出货表的打印. 回顾一下我们的出货表 我们将利用POI构造这样一个excel文档,然后将它生成. 我们先从头来分析,"2012年8月份出货表"是一个标题,并且合并了单元格,我们应该怎么做呢? 我们的出货表的开发步骤如下 1.获取数据 2.创建excel文件 3.将数据写入excel文件 先从"获取数据"开始,我们的出货表是按照月份出货的,所以我们要先编写一个