利用POI生成简单报表

public String print() throws IOException, ParseException{

int rowNo=0;
int cellNo=1;
Row row=null;
Cell cell=null;
//1,创建工作簿
HSSFWorkbook wb = new HSSFWorkbook();
//创建sheet
HSSFSheet sheet = wb.createSheet("出货表");
//设置列宽   本身是个bug会出现一些偏差  
sheet.setColumnWidth(cellNo++, 26*256);
sheet.setColumnWidth(cellNo++, 11*256);
sheet.setColumnWidth(cellNo++, 29*256);
sheet.setColumnWidth(cellNo++, 12*256);
sheet.setColumnWidth(cellNo++, 15*256);
sheet.setColumnWidth(cellNo++, 10*256);
sheet.setColumnWidth(cellNo++, 10*256);
sheet.setColumnWidth(cellNo++, 8*256);

cellNo=1;
//===============打印大标题=====================
//创建行
row = sheet.createRow(rowNo++);

//设置行高
row.setHeightInPoints(36);
//
//创建单元格
cell = row.createCell(cellNo);
//合併單元格
sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 8));//横向合并单元格
//设置单元格内容
String inputDate2 = inputDate.replace("-", "年");
cell.setCellValue(inputDate2+"月份出货表");

//设置标题单元格样式
cell.setCellStyle(this.bigTitle(wb));

//==================打印小标题===============================
//1,创建行
row = sheet.createRow(rowNo++);
//2,设置行高
row.setHeightInPoints(26.25f);
String[] title={"客户","订单号","货号","数量","工厂","工厂交期","船期","贸易条款"};
//2,循环创建单元格
for (String s : title) {

cell = row.createCell(cellNo++);
cell.setCellValue(s);
//设置小标题单元格样式
cell.setCellStyle(this.title(wb));
}

//===============设置表单数据=============================================
//根据船期查询货物
String hql= "from ContractProduct  where to_char(contract.shipTime,'yyyy-MM') ='"+inputDate+"'";
List<ContractProduct> ContractProducts = contractProductService.find(hql, ContractProduct.class, null);
//对象导航查询购销合同
int i=1;
for (ContractProduct c : ContractProducts) {

Contract contract = c.getContract();

String customName = contract.getCustomName();
String contractNo = contract.getContractNo();
Date deliveryPeriod = contract.getDeliveryPeriod();
Date shipTime = contract.getShipTime();
String tradeTerms = contract.getTradeTerms();
String factoryName = c.getFactoryName();
String productNo = c.getProductNo();

Integer cnumber = c.getCnumber();

//创建行
row = sheet.createRow(rowNo++);
//设置行高
row.setHeightInPoints(24);
//设置单元格
cellNo=1;
cell = row.createCell(cellNo++);
cell.setCellValue(customName);
cell.setCellStyle(this.text(wb));
//设置单元格
cell = row.createCell(cellNo++);
cell.setCellValue(contractNo);
cell.setCellStyle(this.text(wb));
//设置单元格
cell = row.createCell(cellNo++);
cell.setCellValue(productNo);
cell.setCellStyle(this.text(wb));
//设置单元格
cell = row.createCell(cellNo++);
if(cnumber!=null){
cell.setCellValue(cnumber);
}
cell.setCellStyle(this.text(wb));
//设置单元格
cell = row.createCell(cellNo++);
cell.setCellValue(factoryName);
cell.setCellStyle(this.text(wb));


//设置单元格
cell = row.createCell(cellNo++);
cell.setCellValue(UtilFuns.dateTimeFormat(deliveryPeriod, "yyyy-MM-dd"));
cell.setCellStyle(this.text(wb));


//设置单元格
cell = row.createCell(cellNo++);
cell.setCellValue(UtilFuns.dateTimeFormat(shipTime, "yyyy-MM-dd"));
cell.setCellStyle(this.text(wb));


//设置单元格
cell = row.createCell(cellNo++);
cell.setCellValue(tradeTerms);
cell.setCellStyle(this.text(wb));

}

//=========================下载===============================

DownloadUtil downloadUtil = new DownloadUtil();

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

HttpServletResponse response = ServletActionContext.getResponse();

//将wb工作簿内容写入到byteArrayOutputStream缓冲区
wb.write(byteArrayOutputStream);
//将wb工作簿所有内容刷新到缓冲区
byteArrayOutputStream.close();
downloadUtil.download(byteArrayOutputStream, response, "出货单.xls");
return NONE;
}

//大标题的样式
public CellStyle bigTitle(Workbook wb){
CellStyle style = wb.createCellStyle();
Font font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short)16);
font.setBoldweight(Font.BOLDWEIGHT_BOLD); //字体加粗

style.setFont(font);

style.setAlignment(CellStyle.ALIGN_CENTER); //横向居中
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //纵向居中

return style;
}
//小标题的样式
public CellStyle title(Workbook wb){
CellStyle style = wb.createCellStyle();
Font font = wb.createFont();
font.setFontName("黑体");
font.setFontHeightInPoints((short)12);

style.setFont(font);

style.setAlignment(CellStyle.ALIGN_CENTER); //横向居中
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //纵向居中

style.setBorderTop(CellStyle.BORDER_THIN); //上细线
style.setBorderBottom(CellStyle.BORDER_THIN); //下细线
style.setBorderLeft(CellStyle.BORDER_THIN); //左细线
style.setBorderRight(CellStyle.BORDER_THIN); //右细线

return style;
}

//文字样式
public CellStyle text(Workbook wb){
CellStyle style = wb.createCellStyle();
Font font = wb.createFont();
font.setFontName("Times New Roman");
font.setFontHeightInPoints((short)10);

style.setFont(font);

style.setAlignment(CellStyle.ALIGN_LEFT); //横向居左
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); //纵向居中

style.setBorderTop(CellStyle.BORDER_THIN); //上细线
style.setBorderBottom(CellStyle.BORDER_THIN); //下细线
style.setBorderLeft(CellStyle.BORDER_THIN); //左细线
style.setBorderRight(CellStyle.BORDER_THIN); //右细线

return style;
}


}

猜你喜欢

转载自blog.csdn.net/chenglaozong/article/details/72567191
今日推荐