poi导出用户资料基本操作
来源:互联网 发布:大圣数据 编辑:程序博客网 时间:2024/06/05 15:46
在action中的代码
//前台访问这个方法会弹出一个下载public void exportExcel(){ try { //1、查找用户列表 userList = userService.findObjects(); //2、导出 HttpServletResponse response = ServletActionContext.getResponse(); //浏览器无法解析的类型都使用2进制流下载 response.setContentType("application/x-execl"); //浏览器会执行下载操作 response.setHeader("Content-Disposition", "attachment;filename=" + new String("用户列表.xls".getBytes(), "ISO-8859-1")); ServletOutputStream outputStream = response.getOutputStream(); userService.exportExcel(userList, outputStream); if(outputStream != null){ outputStream.close(); } } catch (Exception e) { e.printStackTrace(); } }
service中的代码
public void exportExcel(List<User> userList, ServletOutputStream outputStream) { ExcelUtil.exportUserExcel(userList, outputStream); }
util中的代码
public class ExcelUtil { /** * 导出用户的所有列表到excel * @param userList 用户列表 * @param outputStream 输出流 */ public static void exportUserExcel(List<User> userList, ServletOutputStream outputStream) { try { //1、创建工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); //1.1、创建合并单元格对象 CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 4);//起始行号,结束行号,起始列号,结束列号 //1.2、头标题样式 HSSFCellStyle style1 = createCellStyle(workbook, (short)16); //1.3、列标题样式 HSSFCellStyle style2 = createCellStyle(workbook, (short)13); //2、创建工作表 HSSFSheet sheet = workbook.createSheet("用户列表"); //2.1、加载合并单元格对象 sheet.addMergedRegion(cellRangeAddress); //设置默认列宽 sheet.setDefaultColumnWidth(25); //3、创建行 //3.1、创建头标题行;并且设置头标题 HSSFRow row1 = sheet.createRow(0); HSSFCell cell1 = row1.createCell(0); //加载单元格样式 cell1.setCellStyle(style1); cell1.setCellValue("用户列表"); //3.2、创建列标题行;并且设置列标题 HSSFRow row2 = sheet.createRow(1); String[] titles = {"用户名","帐号", "所属部门", "性别", "电子邮箱"}; for(int i = 0; i < titles.length; i++){ HSSFCell cell2 = row2.createCell(i); //加载单元格样式 cell2.setCellStyle(style2); cell2.setCellValue(titles[i]); } //4、操作单元格;将用户列表写入excel if(userList != null){ for(int j = 0; j < userList.size(); j++){ HSSFRow row = sheet.createRow(j+2); HSSFCell cell11 = row.createCell(0); cell11.setCellValue(userList.get(j).getName()); HSSFCell cell12 = row.createCell(1); cell12.setCellValue(userList.get(j).getAccount()); HSSFCell cell13 = row.createCell(2); cell13.setCellValue(userList.get(j).getDept()); HSSFCell cell14 = row.createCell(3); cell14.setCellValue(userList.get(j).isGender()?"男":"女"); HSSFCell cell15 = row.createCell(4); cell15.setCellValue(userList.get(j).getEmail()); } } //5、输出 workbook.write(outputStream); workbook.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 创建单元格样式 * @param workbook 工作簿 * @param fontSize 字体大小 * @return 单元格样式 */ private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontSize) { HSSFCellStyle style = workbook.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平居中 style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直居中 //创建字体 HSSFFont font = workbook.createFont(); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//加粗字体 font.setFontHeightInPoints(fontSize); //加载字体 style.setFont(font); return style; }}
0 0
- poi导出用户资料基本操作
- 用POI导出Excel的基本操作
- POI导出word文档的基本操作
- 试卷导出,poi操作
- poi操作导出excel、
- poi导出excel操作
- poi导出excel基本步骤
- POI----基本操作DOME
- POI 的基本操作
- POI----基本操作DOME
- POI----基本操作DOME
- java操作poi导出Excel
- java操作poi导出Excel2
- poi操作excel导入\导出
- POI操作 导出Excel表格
- 用户列表Excel导出实例(POI)
- POI操作excel基本使用
- POI操作excel基本使用
- 文件上传(io流、fileupload 组件)、下载(动静态方式)
- 归并排序的个人学习记录
- 迭代器模式
- HDU 3501【欧拉函数拓展求一个数的所有质因子之和】
- 文章标题
- poi导出用户资料基本操作
- Material Design学习:CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout
- 原型模式
- MFC中创建多线程
- 【OpenGL】直线生成算法DDA+Bresenham
- 手把手教你Hibernate工程
- mysql5.7 upgrade issue GROUP BY incompatible with sql_mode=only_full_group_by
- Hibernate深入理解----Hibernate 检索方式(HQL,QBC,本地SQL)
- (一)Solr的使用安装