偷个懒,直接上代码吧,大家每一行我都写注释了的,更具需求可以自己设计自己的表格
@Action(value="outProductAction_print")
public String print() throws Exception{
//HSSF 2003版 , XSSF 2007 版,07可以加载03,但03加载不来07 SXSSF
HSSFWorkbook workbook = new HSSFWorkbook(); //创建笔记本
HSSFSheet sheet = workbook.createSheet();//创建表空间
sheet.setColumnWidth(1,25*256);//设置宽度 第一行 宽度25
sheet.setColumnWidth(2,10*256);
sheet.setColumnWidth(3,25*256);
sheet.setColumnWidth(4,10*256);
sheet.setColumnWidth(5,25*256);
sheet.setColumnWidth(6,10*256);
sheet.setColumnWidth(7,25*256);
sheet.setColumnWidth(8,10*256);
HSSFCell createCell=null; //提升单元格的作用域
//大标题
int rownum=0; //设置行数为0,创建一次加一次,循环增加,就可以循环创建行数了
HSSFRow row = sheet.createRow(rownum++);//创建一行
row.setHeightInPoints(36); //设置高度
createCell = row.createCell(1); //创建一个单元格,重第2个算
sheet.addMergedRegion(new CellRangeAddress(0,0,1,8));//合并单元格
String replace = inputDate.replace("-0", "-").replace("-", "年")+"月出货单";//把页面传过来的时间装换成XXXX年X月格式
createCell.setCellValue(replace); //赋值到大标题内容中
createCell.setCellStyle(bigTitle(workbook));//调用下面大标题的样式
//小标题
String[] smartStrs = {"客户","订单号","货号","数量","工厂","工厂交期","船期","贸易条款"};
HSSFRow createRow = sheet.createRow(rownum++); //创建一行
createRow.setHeightInPoints(26); //设立高度
for (int i = 0; i < smartStrs.length; i++) {//循环数据
createCell = createRow.createCell(i+1);//创建单元格,循环一次,创建一个单元格
createCell.setCellValue(smartStrs[i]);//赋值
createCell.setCellStyle(title(workbook));//调用标题样式
}
//内容
//通过选择的时间 查询数据库 由于默认语法中没有我们要的语法,这里我自定义了一个更具时间查询的语法 ContractProduct:货物表,其对应了合同表Contract 详情表结构看一看做到物流云项目
List<ContractProduct> findDateAll = contractProductServiceimpl.findDateAll(inputDate);
System.out.println(findDateAll.size());
for (ContractProduct contractProduct : findDateAll) {
row = sheet.createRow(rownum++); //创建一行
createRow.setHeightInPoints(26);//设置高度26
int i1=1;
createCell = row.createCell(i1++);//客户名 创建一个单元格
createCell.setCellValue(contractProduct.getContract().getCustomName());//赋值
createCell.setCellStyle(text(workbook));//引入样式
createCell = row.createCell(i1++);//订单号
createCell.setCellValue(contractProduct.getContract().getContractNo());
createCell.setCellStyle(text(workbook));
createCell = row.createCell(i1++);//货号
createCell.setCellValue(contractProduct.getProductNo());
createCell.setCellStyle(text(workbook));
createCell = row.createCell(i1++);//数量
createCell.setCellValue(contractProduct.getCnumber());
createCell.setCellStyle(text(workbook));
createCell = row.createCell(i1++);//工厂
createCell.setCellValue(contractProduct.getFactoryName());
createCell.setCellStyle(text(workbook));
createCell = row.createCell(i1++);//工厂交期
//根据工具类UtilFuns的dateTimeFormat方法,把日期格式转换成 String
createCell.setCellValue(UtilFuns.dateTimeFormat(contractProduct.getContract().getDeliveryPeriod()));
createCell.setCellStyle(text(workbook));
createCell = row.createCell(i1++);//船期
createCell.setCellValue(UtilFuns.dateTimeFormat(contractProduct.getContract().getShipTime()));
createCell.setCellStyle(text(workbook));
createCell = row.createCell(i1++);//贸易条款
createCell.setCellValue(contractProduct.getContract().getTradeTerms());
createCell.setCellStyle(text(workbook));
}
//输出
ByteArrayOutputStream os = new ByteArrayOutputStream();//new 一个io流
HttpServletResponse response = ServletActionContext.getResponse(); //创建一个Response
DownloadUtil util = new DownloadUtil();//调用下载工具类 DownloadUtil
workbook.write(os);//把工作薄里的内容写入io流中
util.download(os, response, replace + ".xls");//调用DownloadUtil工具类的download方法,设立下载文件名
os.close();//关闭流
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;
}
}