poi写出报表

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class PoiUtils {
@Autowired
private  BookInfoService bookInfoService;

@Test
public void test1() throws Exception{
@SuppressWarnings("resource")
Workbook wb=new HSSFWorkbook();
/*CellStyle cellStyle=wb.createCellStyle();
cellStyle.setVerticalAlignment(arg0);*/

Sheet sheet = wb.createSheet("sheet1");
sheet.setDefaultColumnWidth(20);
System.out.println(bookInfoService);
List<BookInfo> list = bookInfoService.getAllBooks();

/*for(int i=0;i<list.size();i++){
Row row = sheet.createRow((short)i);
BookInfo bookInfo = list.get(i);
String[] ss = bookInfo.toString().split(",");
for(int j=0;j<ss.length;j++){
Cell cell = row.createCell(j);
//System.out.println(ss[j]);
cell.setCellValue(ss[j]);
}
}*/

CellRangeAddress cra=new CellRangeAddress(0, 0, 0, 5);
sheet.addMergedRegion(cra);

Row row2 = sheet.createRow(0);
row2.setHeightInPoints(30);
row2.createCell(0).setCellValue("报表");

Row row3 = sheet.createRow(1);
row3.setHeightInPoints(20);
row3.createCell(0).setCellValue("图书ID");
row3.createCell(1).setCellValue("图书编号");
row3.createCell(2).setCellValue("图书名称");
row3.createCell(3).setCellValue("图书类型");
row3.createCell(4).setCellValue("图书作者");
row3.createCell(5).setCellValue("是否借阅");

for(int i=2;i<list.size()+2;i++){
Row row = sheet.createRow((short)i);
row.setHeightInPoints(20);
BookInfo bookInfo=list.get(i-2);
/*Field[] fields = BookInfo.class.getDeclaredFields();
for(int j=0;j<fields.length;j++){
row.createCell(j).setCellValue(bookInfo.getBookId());
}*/
row.createCell(0).setCellValue(bookInfo.getBookId());
row.createCell(1).setCellValue(bookInfo.getBookCde());
row.createCell(2).setCellValue(bookInfo.getBookName());
row.createCell(3).setCellValue(bookInfo.getBookTp().getTypeName());
row.createCell(4).setCellValue(bookInfo.getBookAuthor());
if(bookInfo.getBorrowed()==0){
row.createCell(5).setCellValue("已借阅");
}else{
row.createCell(5).setCellValue("未借阅");
}

//row.createCell(i+6).setCellValue(bookInfo.getb);
}
OutputStream stream=new FileOutputStream("e://workbook.xls");
wb.write(stream);
stream.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值