//打印-XSSF
@RequestMapping("/cargo/outproduct/print.action")
public void print2(String inputDate,HttpServletResponse response) throws IOException{ //inputDate格式:yyyy-MM
List<OutProductVO> dataList=outProductService.find(inputDate);
/*System.out.println(dataList.size());
System.out.println(inputDate);*/
Workbook wb=new XSSFWorkbook();//打开一个模板文件,2007Excel以上版本
Sheet sheet=wb.createSheet();
Row nRow=null;
Cell nCell=null;
int rowNo=0; //行号
int cellNo=1;//列号
//声明样式对象和字体对象
CellStyle nStyle=wb.createCellStyle();
Font nFont=wb.createFont();
//设置表格宽度
sheet.setColumnWidth(0,1*300);//列宽
sheet.setColumnWidth(1,26*300);//列宽
sheet.setColumnWidth(2,12*300);//列宽
sheet.setColumnWidth(3,29*300);//列宽
sheet.setColumnWidth(4,10*300);//列宽
sheet.setColumnWidth(5,12*300);//列宽
sheet.setColumnWidth(6,8*300);//列宽
sheet.setColumnWidth(7,10*300);//列宽
sheet.setColumnWidth(8,10*300);//列宽
sheet.setColumnWidth(9,8*300);//列宽
//大标题,合并单元格
sheet.addMergedRegion(new CellRangeAddress(0,0,1,9));//开会行,结束行,开始列,结束列
//合并单元格的内容是写在合并前第一个单元格中
nRow=sheet.createRow(rowNo++);
nRow.setHeightInPoints(36);//行高
nCell=nRow.createCell(1);
nCell.setCellValue(inputDate.replaceFirst("-0","-").replaceFirst("-","年")+"月份出货表");//yyyy-MM
nCell.setCellStyle(this.bigTitle(wb, nStyle, nFont));
//配置标题行
String [] title=new String[]{"客户","订单号","货号","数量","工厂","附件","工厂交期","船期","贸易条款"};
nRow=sheet.createRow(rowNo++);
nRow.setHeightInPoints(26.25f);//标题的高
nStyle=wb.createCellStyle();//重新初始化样式
nFont=wb.createFont();//重新初始化字体
for (int i = 0; i < title.length; i++) {
nCell=nRow.createCell(i+1);
nCell.setCellValue(title[i]);
nCell.setCellStyle(this.Title(wb, nStyle, nFont));
}
nStyle=wb.createCellStyle();//重新初始化样式
nFont=wb.createFont();//重新初始化字体
//处理数据
for (int i = 0; i < dataList.size(); i++) {
OutProductVO op=dataList.get(i);
nRow=sheet.createRow(rowNo++);
nRow.setHeightInPoints(24);//数据框的高
cellNo=1;//列号初始化
nCell=nRow.createCell(cellNo++);
nCell.setCellValue(op.getCustomName());
nCell.setCellStyle(this.Text(wb, nStyle, nFont));
nCell=nRow.createCell(cellNo++);
nCell.setCellValue(op.getcontractNo());
nCell.setCellStyle(this.Text(wb, nStyle, nFont));
nCell=nRow.createCell(cellNo++);
nCell.setCellValue(op.getProductNo());
nCell.setCellStyle(this.Text(wb, nStyle, nFont));
nCell=nRow.createCell(cellNo++);
nCell.setCellValue(op.getCnumber());
nCell.setCellStyle(this.Text(wb, nStyle, nFont));
nCell=nRow.createCell(cellNo++);
nCell.setCellValue(op.getFactoryName());
nCell.setCellStyle(this.Text(wb, nStyle, nFont));
nCell=nRow.createCell(cellNo++);
nCell.setCellValue(op.getExts());
nCell.setCellStyle(this.Text(wb, nStyle, nFont));
nCell=nRow.createCell(cellNo++);
nCell.setCellValue(op.getDelivery_preriod());
nCell.setCellStyle(this.Text(wb, nStyle, nFont));
nCell=nRow.createCell(cellNo++);
nCell.setCellValue(op.getShip_time());
nCell.setCellStyle(this.Text(wb, nStyle, nFont));
nCell=nRow.createCell(cellNo++);
nCell.setCellValue(op.getTradeTerms());
nCell.setCellStyle(this.Text(wb, nStyle, nFont));
}
/*OutputStream os=new FileOutputStream(new File("F:\\outproduct.xls"));
wb.write(os);
os.flush();
os.close();*/
//下载(使用了我们的工具类)
DownloadUtil du=new DownloadUtil();
ByteArrayOutputStream bs=new ByteArrayOutputStream();
wb.write(bs);
du.download(bs, response, "出货表.xlsx");
}
下一次我们将里了解百万数据的导入以及导出
转载请注明出处:http://blog.csdn.net/acmman/article/details/48781829
版权声明:本文为博主原创文章,未经博主允许不得转载。
【springmvc+mybatis项目实战】杰信商贸-27.POI由HSSF升级为XSSF
原文:http://blog.csdn.net/acmman/article/details/48781829