SSM框架 (Spring+SpringMVC+Mybatis) Excel导出
实现从数据库(MySql)查寻数据,写入excle中并导出
第一种方式(推荐)
<不会再项目下生成多于文件夹,代码更简便>
// 导出图书
@RequestMapping(value = "/outPutBook/{ids}", method = RequestMethod.GET)
public void
outPutBook(@PathVariable(value = "ids") String ids1,HttpServletResponse response) throws IOException {
List<BookAndFenlei> list = null;
String key = "";
if (ids1.equals("a")) {
//传入a 表示导出全部
list = this.bookService.outPutBookAll();
key = "全部";
}else{
//System.out.println(ids1);
list = this.bookService.outPutBookIds(ids1);
key = "勾选";
}
//创件一个工作蒲
HSSFWorkbook Workbook = new HSSFWorkbook();
//创建一个工作表
HSSFSheet sheet = Workbook.createSheet(key + "图书信息表");
sheet.setColumnWidth(7, 15 * 256); //设定列宽度
//设置样式
HSSFCellStyle style = Workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
HSSFFont font = Workbook.createFont();
font.setBold(true);
font.setColor(HSSFColor.DARK_RED.index);
style.setFont(font);
String[] title = {
"编号", "分类编号", "图书名", "图书价格", "出版社", "作者", "库存" };
HSSFRow row = sheet.createRow(0);//从0开始
for (int i = 0; i < title.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(style);
cell.setCellValue(title[i]);
}
HSSFCellStyle style1 = Workbook.createCellStyle();
style1.setAlignment(HSSFCellStyle