package com.ztesoft.bidding.contract;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import net.buffalo.common.BaseAppCONST;
import net.buffalo.common.DAOFactory;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
import com.ztesoft.bidding.contract.dao.ContractInfoDAO;
import com.ztesoft.bidding.contract.vo.ContractVO;
public class ContractInfoService {
private ContractInfoDAO contractInfoDAO = null;
public ContractInfoService() {
contractInfoDAO = (ContractInfoDAO) DAOFactory
.getDAO(ContractInfoDAO.class);
}
/*
* @生成合同模板
*/
public void createContract(int procurementItemId, int contractId)
throws SQLException {
try {
//保存服务器路径
String FileUrl = BaseAppCONST.getProperty("upload.contractPath");
/*String root_path = this.getServletContext().getRealPath("");
System.out.println(root_path);*/
String fineNameId = "合同模板" + String.valueOf(contractId);
String reportFilePath = "F:\\tomcat-5.5\\webapps\\BiddingOnline\\WEB-INF\\classes\\com\\ztesoft\\bidding\\contract\\jspreport\\javaBean.jasper ";
File reportFile1 = new File(reportFilePath);
if (!reportFile1.exists())
throw new JRRuntimeException("没找到合同模板路径");
JasperReport jasperReport1 = (JasperReport) JRLoader
.loadObject(reportFile1.getPath());
//-------数据传输------------------
List<testVo> data = new ArrayList<testVo>();
testVo TestVo1 = new testVo();
TestVo1 = dataSource(procurementItemId);
Map<String, Object> map = new HashMap<String, Object>();
map.put("partnerName", TestVo1.getPartnerName());
map.put("contactMan", TestVo1.getContactMan());
map.put("contactTel", TestVo1.getContactTel());
map.put("bank", TestVo1.getBank());
map.put("account", TestVo1.getAccount());
map.put("buyerName", TestVo1.getPartyName());
map.put("exDate", TestVo1.getExDate().toString());
map.put("address", TestVo1.getAddress());
map.put("pContactMan", TestVo1.getPContactMan());
map.put("pContactPhone", TestVo1.getPContactPhone());
//一条记录就是个vo
List<testVo> list4 = (List<testVo>) contractInfoDAO
.countInfo(procurementItemId);
List<testVo> list5 = (List<testVo>) contractInfoDAO
.parts(procurementItemId);
String name = null;
String remark = null;
String budgetAmount = null;
String price = null;
//金额大小写转换 对于最后总价价格total_price
String totalPrice = list4.get(0).getTotalPrice();
map.put("totalPrice", totalPrice);
double BigTotalPrice = Double.parseDouble(totalPrice);
String ChineseCurrency = bigMoney(new Double(BigTotalPrice));
map.put("bigTotalPrice", ChineseCurrency);
//得到商品及其配件 价格,数量,总价,
int count = list4.size() + list5.size();
for (int i = 0; i < count; i++) {
testVo TestVo = new testVo();
if (list4.size() > i) {
name = list4.get(i).getName();
remark = list4.get(i).getRemark();
budgetAmount = list4.get(i).getBudgetAmount();
price = list4.get(i).getPrice();
TestVo.setName(name);
TestVo.setRemark(remark);
TestVo.setBudgetAmount(budgetAmount);
TestVo.setPrice(price);
} else {
int k = i - list4.size();
name = list5.get(k).getName();
remark = list5.get(k).getRemark();
TestVo.setName(name);
TestVo.setRemark(remark);
TestVo.setPrice("0");
}
data.add(TestVo);
}
JRDataSource dataSource = new JRBeanCollectionDataSource(data);
/**********数据传输***************/
JasperPrint jasperPrint1 = JasperFillManager.fillReport(
jasperReport1, map, dataSource);
JRExporter exporter = null;
exporter = new JRRtfExporter();
List reportlist = (List) new java.util.ArrayList();
reportlist.add(jasperPrint1);
System.out.println(reportlist.size() + "数据正常传出");
byte[] bytes;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,
reportlist);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
exporter.setParameter(JRExporterParameter.JASPER_PRINT,
jasperPrint1);
exporter.exportReport();
bytes = baos.toByteArray();
/****************************/
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
POIFSFileSystem fs = new POIFSFileSystem();
DirectoryEntry directory = fs.getRoot();
DocumentEntry de = directory.createDocument("WordDocument", bais);
//String fineName = "合同模板";
String path = FileUrl + fineNameId + ".doc";
FileOutputStream ostream = new FileOutputStream(path);
fs.writeFilesystem(ostream);
//插入合同路径文件名称,如果数据库中有该文档则提示用户
String datePath = contractInfoDAO.getPath(contractId);
if (path.equals(datePath)) {
System.out.println("已经存在此文档");
} else {
insertContract(fineNameId, path);
}
bais.close();
ostream.close();
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* @创建合同信息
* @将文件名和路径加入文档库
* @1合同名称 2 path
*/
public void insertContract(String fineName, String path)
throws SQLException {
contractInfoDAO.addContract(fineName, path);
}
/*
* @取得文件的路径根据合同的id
*/
public String getPath(int contractId) throws SQLException {
System.out.println(contractInfoDAO.getPath(contractId));
return contractInfoDAO.getPath(contractId);
}
/*
* @取得数据源
*/
public testVo dataSource(int procurementItemId) throws SQLException {
/**
* 根据采购结果查询供应商信息 查询出来的是一天供应商信息
* @Title:
* @Description: contactMan; 供应商联系人
* @param @param contactTel ; 供应商联系电话
* @param @param bank; 银行
* @param @param Account 账户
* @param @param exDate; 交货期
* @param @param address; 交货地点
* @param @param pContactMan; 采购联系人
*/
testVo TestVo = new testVo();
List<testVo> list = (List<testVo>) contractInfoDAO
.supplierInfo(procurementItemId);
String partnerName = list.get(0).getPartnerName();
String contactMan = list.get(0).getContactMan();
String contactTel = list.get(0).getContactTel();
String bank = list.get(0).getBank();
String account = list.get(0).getAccount();
TestVo.setPartnerName(partnerName);
TestVo.setContactMan(contactMan);
TestVo.setContactTel(contactTel);
TestVo.setBank(bank);
TestVo.setAccount(account);
//采购单位
List<testVo> list2 = (List<testVo>) contractInfoDAO
.buyerName(procurementItemId);
String buyerName = list2.get(0).getPartyName();
/**
* 根据采购结果查询供应商信息 查询出来的是一天供应商信息
* @Title:
* @param @param exDate ; 交货期
* @param @param address; 交货地点
* @param @param pContactMan 采购联系人
* @param @param pContactPhone; 采购联系人
*/
TestVo.setPartyName(buyerName);
List<testVo> list3 = (List<testVo>) contractInfoDAO
.contractItem(procurementItemId);
Date exDate = list3.get(0).getExDate();
String address = list3.get(0).getAddress();
String pContactMan = list3.get(0).getPContactMan();
String pContactPhone = list3.get(0).getPContactPhone();
TestVo.setExDate(exDate);
TestVo.setAddress(address);
TestVo.setPContactMan(pContactMan);
TestVo.setPContactPhone(pContactPhone);
return TestVo;
}
/*
* @金额换算大写
*/
public String bigMoney(Object ob) {
String s = new DecimalFormat("#.00").format(ob);
s = s.replaceAll("\\.", "");// 将字符串中的"."去掉
char d[] = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };
String unit = "仟佰拾兆仟佰拾亿仟佰拾万仟佰拾元角分";
int c = unit.length();
StringBuffer sb = new StringBuffer(unit);
for (int i = s.length() - 1; i >= 0; i--) {
sb.insert(c - s.length() + i, d[s.charAt(i) - 0x30]);
}
s = sb.substring(c - s.length(), c + s.length());
s = s.replaceAll("零[仟佰拾]", "零").replaceAll("零{2,}", "零").replaceAll(
"零([兆万元Ԫ])", "$1").replaceAll("零[角分]", "");
return s;
}
}
分享到:
相关推荐
ireport利用javabean做数据源.docx
本文档详细说明了java调用ireport环境搭建及数据库配置等,及java怎么调用ireport生成报告,附带java生成的报告代码只是word的,生成excel或者PDF可以修改方法即可。内容包含数据库连接配置,参数设置。
IREPORT使用JAVABEAN作为报表数据源IREPORT使用JAVABEAN作为报表数据源IREPORT使用JAVABEAN作为报表数据源
iReport使用JavaBean做数据源
下载包含程序源码,lib包,ireport创建模板操作文档,说明。程序可以在java myeclipse环境下直接运行。jasperreport版本6.3.1,ireport为5.6.0
iReport使用JavaBean作为数据源——最简单的示例。 包含: 1、Java源文件(JavaBean)。 2、iReport的报表源文件(jrxml)。 3、详细的说明。
這是兩個用java作數據源的例子: 其中 javaBean 為用javaBean作數據源的例子 test 為用自定義數據源的例子 兩個例子都通過測試 想要的朋友可以下載看看! 祝大家玩得開心!
简单明了利用javabean做数据源,实现ireport报表功能
图示ireport中使用javabean作数据源开发基于jasperreports报表过程
在一个报表中使用多个数据源的方法,现在分享出来以便后来者少走弯路!网上很多资料说多数据源只能使用子报表方式
ireport java sql 报表 javabean与 ireport结合使用
这篇文档主要介绍了如何使用各种各样的数据源(主要是针对于IREPORT的)
一看就会的实例。能够很快上手ireport
ireport中使用javabean作数据源开发jasperreports报表的详细过程 附有图片
利用javabean数据源,ireport入门级普通报表和子报表
NULL 博文链接:https://shouzhang1-2.iteye.com/blog/1973767
在网上找了很多ireport基于javabean的主子报表配置的文章。要么版本太老,要么无图无真相。对于bean中的子报表集合数据源怎么获取,始终不成功。现经过尝试终于弄明白是怎么回事了,上传以供分享。因从事IT工作,...
Java调用IReport5.6需要的jar包,支持JDK1.7。亲测可用