50,531
社区成员
发帖
与我相关
我的任务
分享
public String outBook() {
List<Book> bookList = findBookService(0,100,null);
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("图书档案表");
HSSFRow hssfRow = sheet.createRow(0);
HSSFCell headCell = hssfRow.createCell(0);
headCell.setCellValue("图书档案");
// 设置单元格格式居中
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 添加表头行
hssfRow = sheet.createRow(1);
// 添加表头内容
headCell = hssfRow.createCell(0);
headCell.setCellValue("序号");
headCell.setCellStyle(cellStyle);
headCell = hssfRow.createCell(1);
headCell.setCellValue("图书条形码");
headCell.setCellStyle(cellStyle);
headCell = hssfRow.createCell(2);
headCell.setCellValue("图书名称");
headCell.setCellStyle(cellStyle);
headCell = hssfRow.createCell(3);
headCell.setCellValue("作者");
headCell.setCellStyle(cellStyle);
headCell = hssfRow.createCell(4);
headCell.setCellValue("出版社");
headCell.setCellStyle(cellStyle);
headCell = hssfRow.createCell(5);
headCell.setCellValue("价格");
headCell.setCellStyle(cellStyle);
// 添加数据内容
for (int i = 0; i < bookList.size(); i++) {
hssfRow = sheet.createRow((int) i + 2);
Book book = bookList.get(i);
// 创建单元格,并设置值
HSSFCell cell = hssfRow.createCell(0);
cell.setCellValue(i);
cell.setCellStyle(cellStyle);
cell = hssfRow.createCell(1);
cell.setCellValue(book.getBookbarcode());
cell.setCellStyle(cellStyle);
cell = hssfRow.createCell(2);
cell.setCellValue(book.getBookname());
cell.setCellStyle(cellStyle);
cell = hssfRow.createCell(3);
cell.setCellValue(book.getAuthor());
cell.setCellStyle(cellStyle);
cell = hssfRow.createCell(4);
cell.setCellValue(book.getPublisher());
cell.setCellStyle(cellStyle);
cell = hssfRow.createCell(5);
cell.setCellValue(book.getPrice());
cell.setCellStyle(cellStyle);
}
// 保存Excel文件
try {
OutputStream outputStream = new FileOutputStream("C://new//bookinfo.xls");
workbook.write(outputStream);
outputStream.close();
return "导出成功";
} catch (Exception e) {
e.printStackTrace();
}
return "导出失败";
}