java 调ireport (javaBean数据源)保存服务器 生成word

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;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值