package cn.itcast.jx.action.cargo;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.ServletActionContext;
import cn.itcast.jx.action.BaseAction;
import cn.itcast.jx.domain.ContractProduct;
import cn.itcast.jx.service.ContractProductService;
import cn.itcast.jx.util.DownloadUtil;
import cn.itcast.jx.util.UtilFuns;
public class OutProductAction extends BaseAction {
private String inputDate;
private ContractProductService contractProductService;
public void setContractProductService(
ContractProductService contractProductService) {
this.contractProductService = contractProductService;
}
public String getInputDate() {
return inputDate;
}
public void setInputDate(String inputDate) {
this.inputDate = inputDate;
}
public String toedit() throws Exception {
// TODO Auto-generated method stub
return "toedit";
}
public String print() throws Exception{
//1加载模板 path 对应的是tomcat的绝对路径,拼接path将模板导入
String path = ServletActionContext.getRequest().getRealPath("/");
path +="/make/xlsprint/tOUTPRODUCT.xls";
//2获取sheet
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File(path)));
HSSFSheet sheet = wb.getSheetAt(0);
//定义公共变量
Row nRow = null;//行对象
Cell nCell = null;//单元格对象
int rowNo =0;
int cellNo = 1;
/**************设置大标题*********************/
nRow = sheet.getRow(rowNo);
nCell = nRow.getCell(cellNo);
// 5 设置数据
/** inputDate.replace("-", "年")
* 2015-02 2015年02月份出货表
* 2015-11 2015年11月份出货表
*
* 2015-02 2015-2 2015年2
* 2015-11 2015-11 2015年11
*/
nCell.setCellValue(inputDate.replace("-0", "-").replace("-", "年")+"月份出货表");
/********设置小标题*********/
rowNo++;//跳过小标题
/********设置出货数据*********/
rowNo++;//进入第一行数据
nRow = sheet.getRow(rowNo);
CellStyle customerCellStyle = nRow.getCell(cellNo++).getCellStyle();
//订单号
CellStyle contractNoCellStyle = nRow.getCell(cellNo++).getCellStyle();
//货号
CellStyle productNoCellStyle = nRow.getCell(cellNo++).getCellStyle();
//数量
CellStyle cnumberCellStyle = nRow.getCell(cellNo++).getCellStyle();
//工厂
CellStyle factoryCellStyle = nRow.getCell(cellNo++).getCellStyle();
//工厂交期
CellStyle deliveryPeriodCellStyle = nRow.getCell(cellNo++).getCellStyle();
//船期
CellStyle shipTimeCellStyle = nRow.getCell(cellNo++).getCellStyle();
//贸易条款
CellStyle tradeTermsCellStyle = nRow.getCell(cellNo++).getCellStyle();
// 准备数据
//String hql = "from ContractProduct where contract.shipTime like '%"+inputDate+"%'";//mysql支持,oracle不支持
//to_char可以将Date转成varchar,oracle中的所有的PL/SQL函数都可以直接写在hql语句中
String hql = "from ContractProduct where to_char(contract.shipTime,'yyyy-mm')='"+inputDate+"'";
//货物的集合 调用合同货物的业务层查找所有要打印的货物
List<ContractProduct> list = contractProductService.find(hql, ContractProduct.class, null);
for(ContractProduct cp:list){
//单元格no归1
cellNo = 1;
//一条数据创建一行
nRow = sheet.createRow(rowNo);
//在创建每列的数据
//客户
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(cp.getContract().getCustomName());
nCell.setCellStyle(customerCellStyle);
//订单号
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(cp.getContract().getContractNo());
nCell.setCellStyle(contractNoCellStyle);
//货号
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(cp.getProductNo());
nCell.setCellStyle(productNoCellStyle);
//数量
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(cp.getCnumber());
nCell.setCellStyle(cnumberCellStyle);
//工厂
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(cp.getFactoryName());
nCell.setCellStyle(factoryCellStyle);
//工厂交期
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(UtilFuns.dateTimeFormat(cp.getContract().getDeliveryPeriod()));
nCell.setCellStyle(deliveryPeriodCellStyle);
//船期
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(UtilFuns.dateTimeFormat(cp.getContract().getShipTime()));
nCell.setCellStyle(shipTimeCellStyle);
//贸易条款
nCell = nRow.createCell(cellNo++);
nCell.setCellValue(cp.getContract().getTradeTerms());
nCell.setCellStyle(tradeTermsCellStyle);
// 开始操作数据,rowNo++
rowNo++;
}
/***************************************************/
// 7 写入流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();//字节数组缓冲流
wb.write(byteArrayOutputStream);
// 8下 载
DownloadUtil downloadUtil = new DownloadUtil();
HttpServletResponse response = ServletActionContext.getResponse();
/**
* 第一个参数:流
* 第二个参数:response
* 第三个参数:下载的文件名
*/
downloadUtil.download(byteArrayOutputStream, response, "出货表.xls");
return NONE;
}
}