@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();
@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();