标签:outputStream org 导出 poi apache import excle response
package com.ittax.core.util; import java.util.List; import javax.servlet.ServletOutputStream; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFHeader; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.util.CellRangeAddress; import com.ittax.nsfw.user.entity.User; /** * excel工具类,支持批量导出 * @author lizewu * */ public class ExcelUtil { /** * 将用户的信息导入到excel文件中去 * @param userList 用户列表 * @param out 输出表 */ public static void exportUserExcel(List<User> userList,ServletOutputStream out) { try{ //1.创建工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); //1.1创建合并单元格对象 CellRangeAddress callRangeAddress = new CellRangeAddress(0,0,0,4);//起始行,结束行,起始列,结束列 //1.2头标题样式 HSSFCellStyle headStyle = createCellStyle(workbook,(short)16); //1.3列标题样式 HSSFCellStyle colStyle = createCellStyle(workbook,(short)13); //2.创建工作表 HSSFSheet sheet = workbook.createSheet("用户列表"); //2.1加载合并单元格对象 sheet.addMergedRegion(callRangeAddress); //设置默认列宽 sheet.setDefaultColumnWidth(25); //3.创建行 //3.1创建头标题行;并且设置头标题 HSSFRow row = sheet.createRow(0); HSSFCell cell = row.createCell(0); //加载单元格样式 cell.setCellStyle(headStyle); cell.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(colStyle); cell2.setCellValue(titles[i]); } //4.操作单元格;将用户列表写入excel if(userList != null) { for(int j=0;j<userList.size();j++) { //创建数据行,前面有两行,头标题行和列标题行 HSSFRow row3 = sheet.createRow(j+2); HSSFCell cell1 = row3.createCell(0); cell1.setCellValue(userList.get(j).getName()); HSSFCell cell2 = row3.createCell(1); cell2.setCellValue(userList.get(j).getAccount()); HSSFCell cell3 = row3.createCell(2); cell3.setCellValue(userList.get(j).getDept()); HSSFCell cell4 = row3.createCell(3); cell4.setCellValue(userList.get(j).isGender()?"男":"女"); HSSFCell cell5 = row3.createCell(4); cell5.setCellValue(userList.get(j).getEmail()); } } //5.输出 workbook.write(out); workbook.close(); //out.close(); }catch(Exception e) { e.printStackTrace(); } } /** * * @param workbook * @param fontsize * @return 单元格样式 */ private static HSSFCellStyle createCellStyle(HSSFWorkbook workbook, short fontsize) { // TODO Auto-generated method stub 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; } }
//导出用户列表 public void exportExcel() { try { //1.查找用户列表 userList = userService.findObjects(); //2.导出 HttpServletResponse response = ServletActionContext.getResponse(); //这里设置的文件格式是application/x-excel response.setContentType("application/x-excel"); 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(); } } public String importExcel() { if(userExcel!= null) { //判断是否是Excel文件 if(userExcelFileName.matches("^.+\\.(?i)((xls)|(xlsx))$")) { userService.importExcel(userExcel, userExcelFileName); } } return"list"; }
//导出用户列表 public void exportExcel() { try { //1.查找用户列表 userList = userService.findObjects(); //2.导出 HttpServletResponse response = ServletActionContext.getResponse(); //这里设置的文件格式是application/x-excel response.setContentType("application/x-excel"); 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(); } } public String importExcel() { if(userExcel!= null) { //判断是否是Excel文件 if(userExcelFileName.matches("^.+\\.(?i)((xls)|(xlsx))$")) { userService.importExcel(userExcel, userExcelFileName); } } return"list"; }
标签:outputStream,org,导出,poi,apache,import,excle,response 来源: https://www.cnblogs.com/xiufengchen/p/10400538.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。