poi的所需jar包下载
我这里以打印商品信息表为例
两种打印方式:
一种常规设置打印
一种使用自己做好的模板打印
1.在项目中导入poi的jar包
maven项目中:
2.打印需求八步:
1)创建一个工作簿workbook
2)创建一个工作表sheet
3)创建一个行对象row(下标起始值为0)
4)创建一个单元格对象cell(下标起始值为0)
5)给单元格设置内容
6)设置单元格的样式,设置字体和字体的大小
7)保存,关闭流对象
8)下载
3.提前准备好设置需要打印的数据和表格的样式
4.后台代码实例:
/**
* 打印出根据名称模糊查询到的商品信息
*/
@RequestMapping("/print/{goodName}")
public String print(@PathVariable String goodName,HttpServletResponse response,HttpServletRequest request) throws Exception {
goodName = new String(goodName.getBytes("ISO-8859-1"),"UTF-8");
//通用变量
int rowNo=0,cellNo=1;//行号 列号
Row nRow =null;//行对象
Cell nCell = null;//单元格对象
//1.创建工作簿
//Workbook wb = new HSSFWorkbook();//只支持excel2003版本 扩展名xls
//Workbook wb = new XSSFWorkbook(); //支持excel2007+版本 扩展名xlsx
Workbook wb = new SXSSFWorkbook(); //支持excel2007+版本 扩展名xlsx 不支持模板操作,可以将产生的一部分对象从内存中转移到磁盘。
//默认转移对象的个数为100,如果new SXSSFWorkbook(500)代表内存中对象个数达到500就转移到磁盘
//2.创建工作表
Sheet sheet = wb.createSheet();
//设置列宽 本身是个bug会出现一些偏差
sheet.setColumnWidth(cellNo++, 18*256);
sheet.setColumnWidth(cellNo++, 14*256);
sheet.setColumnWidth(cellNo++, 10*256);
sheet.setColumnWidth(cellNo++, 10*256);
sheet.setColumnWidth(cellNo++, 10*256);
sheet.setColumnWidth(cellNo++, 8*256);
sheet.setColumnWidth(cellNo++, 10*256);
sheet.setColumnWidth(cellNo++, 10*256);
cellNo=1;//重置
//3.创建行对象
//=========================================大标题=============================
nRow = sheet.createRow(rowNo++);//创建行对象
nRow.setHeightInPoints(36);//设置行高
nCell = nRow.createCell(cellNo);//创建单元格对象
//合并单元格
sheet.addMergedRegion(new CellRangeAddress(0,0,1,8));//横向合并单元格
//设置单元格的内容
nCell.setCellValue(goodName+"商品信息数据表");
//设置单元格样式
nCell.setCellStyle(this.bigTitle(wb));
//=======================================小标题=================================
String titles[] = {"名称","描述","价格","市场价格","发货地","已售","库存","上架时间"};
//创建小标题的行对象
nRow = sheet.createRow(rowNo++);
nRow.setHeightInPoints(26.25f);//设置行高
//创建单元格对象,并设置内容 ,并设置样式
for(String title :titles){
nCell = nRow.createCell(cellNo++);//创建单元格对象
nCell.setCellValue(title);//设置内容
nCell.setCellStyle(this.title(wb));//设置样式
}
//=======================================数据输出=================================================
//查询到我想要打印的商品信息的集合
List<Good> list = goodService.getByGoodName(goodName);
for(Good good :list){
nRow = sheet.createRow(rowNo++);//产生数据行
nRow.setHeightInPoints(24);//设置行高
cellNo=1;
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getName());//商品名称
nCell.setCellStyle(this.text(wb));//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getDescription());//商品描述
nCell.setCellStyle(this.text(wb));//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getPrice().toString()); //商品价格
nCell.setCellStyle(this.text(wb));//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getMarketprice().toString());//市场价格
nCell.setCellStyle(this.text(wb));//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getSource());//发货地
nCell.setCellStyle(this.text(wb));//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getSalenum());//已售
nCell.setCellStyle(this.text(wb));//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getStorage());//库存
nCell.setCellStyle(this.text(wb));//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(UtilFuns.dateTimeFormat(good.getAddtime()));//上架时间
nCell.setCellStyle(this.text(wb));//设置文本样式
}
//======================================输出到客户端(下载)========================================
DownloadUtil downUtil = new DownloadUtil();
ByteArrayOutputStream baos = new ByteArrayOutputStream();//流 内存中的缓存区
wb.write(baos);//将excel表格中的内容输出到缓存
baos.close();//刷新缓存
downUtil.download(baos, response,request, "商品信息表.xlsx");//如果是中文,下载时可能会产生乱码,如何解决?
return null;
}
//大标题的样式
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;
}
5.效果展示:
启动项目,找到商品名称中带有时尚的商品(我这里一共查询出来了八个)
点击打印后访问我后台的方法,打印成功:
6.查看文件:
使用模板打印
1.先准备好模板
2.将模板文件放入webapp下
3.编写后台打印方法:
/**
* 打印出根据名称查询到的商品信息(模板打印)
*/
@RequestMapping("/print/{goodName}")
public String print(@PathVariable String goodName,HttpServletResponse response,HttpServletRequest request) throws Exception {
goodName = new String(goodName.getBytes("ISO-8859-1"),"UTF-8");
//通用变量
int rowNo=0,cellNo=1;
Row nRow =null;
Cell nCell = null;
//1.读取工作簿(我放入webapp中的模板)
String path = request.getSession().getServletContext().getRealPath("/")+"/make/print/good.xlsx";
System.out.println(path);
InputStream is = new FileInputStream(path);
Workbook wb = new XSSFWorkbook(is);
//2.读取工作表
Sheet sheet = wb.getSheetAt(0);
cellNo=1;//重置
//3.创建行对象
//=========================================大标题=============================
nRow = sheet.getRow(rowNo++);//读取行对象
nCell = nRow.getCell(cellNo);
//设置单元格的内容
nCell.setCellValue(goodName+"商品信息数据表");
//=======================================小标题=================================
rowNo++;
//=======================================数据输出=================================================
nRow = sheet.getRow(rowNo);//读取第三行
CellStyle goodNameCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle descriptionCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle priceCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle marketPriceCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle sourceCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle saleNumCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle storageCellStyle = nRow.getCell(cellNo++).getCellStyle();
CellStyle addTimeCellStyle = nRow.getCell(cellNo++).getCellStyle();
List<Good> list = goodService.getByGoodName(goodName);
for(Good good : list){
nRow = sheet.createRow(rowNo++);//产生数据行
nRow.setHeightInPoints(24);//设置行高
cellNo=1;
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getName());//商品名称
nCell.setCellStyle(goodNameCellStyle);//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getDescription());//描述
nCell.setCellStyle(descriptionCellStyle);//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getPrice().toString());//价格
nCell.setCellStyle(priceCellStyle);//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getMarketprice().toString());//市场价格
nCell.setCellStyle(marketPriceCellStyle);//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getSource());//发货地
nCell.setCellStyle(sourceCellStyle);//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getSalenum());//已售
nCell.setCellStyle(saleNumCellStyle);//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(good.getStorage());//库存
nCell.setCellStyle(storageCellStyle);//设置文本样式
nCell = nRow.createCell(cellNo++);//产生单元格对象
nCell.setCellValue(UtilFuns.dateTimeFormat(good.getAddtime()));//上架时间
nCell.setCellStyle(addTimeCellStyle);//设置文本样式
}
//======================================输出到客户端(下载)========================================
DownloadUtil downUtil = new DownloadUtil();
ByteArrayOutputStream baos = new ByteArrayOutputStream();//流 内存中的缓存区
wb.write(baos);//将excel表格中的内容输出到缓存
baos.close();//刷新缓存
downUtil.download(baos, response,request, "商品信息表.xls");//如果是中文,下载时可能会产生乱码,如何解决?
return null;
}
//大标题的样式
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;
}
4.效果展示:
同上,查询商品名称中包含时尚的商品进行打印
打开文件查看成功: