2015/01/06 备忘excel导出,方便自己以后查阅.
工具类 :ExportExcelUtils.java
package com.hmnet.common; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import net.sf.ehcache.CacheOperationOutcomes.GetAllOutcome; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.struts2.ServletActionContext; import com.hmnet.etrade.record.domain.CompanyRecord; import com.hmnet.etrade.record.domain.GoodsRecord; public class ExportExcelUtils{ public String exportExcel(Map map,String modelFlag){ try { HttpServletResponse response = ServletActionContext.getResponse(); /* * POI 操作Excel - 步骤: * */ HSSFWorkbook workbook = new HSSFWorkbook(); ServletOutputStream out = response.getOutputStream(); HSSFSheet sheet = workbook.createSheet("sheet1"); sheet.setColumnWidth(0, 1500); //设置序号列宽,其他列暂时一样宽 sheet.setDefaultColumnWidth ((short)12); // 设置工作表列宽 sheet.setDefaultRowHeight((short)250); // 设置工作表行高 /* * sheet样式定义【getColumnTopStyle()/getStyle()均为自定义方法 - 在下面 - 可扩展】 * */ HSSFCellStyle columnTopStyle = ExportExcelUtilsStyle.getColumnTopStyle(workbook);//获取列头样式对象 HSSFCellStyle style = ExportExcelUtilsStyle.getStyle(workbook); //单元格样式对象 //列头数组定义 String[] label = ExportExcelUtilsModel.getExportMoedel(modelFlag); // 定义所需列数 int columnNum = label.length; //设置列头 HSSFRow row1 = sheet.createRow((short)0); // 在索引0的位置创建行(最顶端的行) HSSFCell cell1 = null; // 在索引0的位置创建单元格(左上端) // 将列头设置到sheet的单元格中 for(int n=0;n<columnNum;n++){ cell1 = row1.createCell((short)(n)); //创建列头对应个数的单元格 cell1.setCellType(HSSFCell.CELL_TYPE_STRING); //设置列头单元格的数据类型 cell1.setCellValue(label[n]); //设置列头单元格的值 cell1.setCellStyle(columnTopStyle); //设置列头单元格样式 } /* * 将查询出的数据设置到sheet对应的单元格中 * */ //企业信息备案 if(modelFlag == "companyRecord"){ List<CompanyRecord> exportList = (List<CompanyRecord>) map.get("exportList"); if(null==exportList||exportList.size()==0){ return "exportList size is zero"; } int i=1; for(CompanyRecord cr : exportList){ //将属性转化成字符串数组的格式以便于写到sheet中 String c[] = {i+"", cr.getCustomMaster(), cr.getCompanyCode(), cr.getCompanyName(), cr.getCompanyCatagory(), cr.getOrganizationCode(), cr.getCompanyAbbreviation(), cr.getCustomLinkName(), cr.getCustomLinkNameTel(), cr.getRegistrationAddress(), cr.getTradePlatformName(), cr.getTradePlatformWww(), cr.getCompanyWww(), cr.getApplyDate()+"", this.getApplyTypeLabel(cr.getApplyType()), this.getDocStatusLabel(cr.getDocStatus()), cr.getCreateUser(), cr.getCreateDate()+""}; //创建行(从下面的i+1要注意,第0行是列头,因此创建新行要从下一行开始) HSSFRow row = sheet.createRow(i); //创建所需的行数 for(short j=0;j<columnNum;j++){ HSSFCell cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);//设置单元格的数据类型 cell.setCellValue(c[j]); //设置单元格的值 cell.setCellStyle(style); //设置单元格样式 } i++; } } //海关商品备案 if(modelFlag == "goodsRecord"){ List<GoodsRecord> exportList = (List<GoodsRecord>) map.get("exportList"); if(null==exportList||exportList.size()==0){ return "exportList size is zero"; } int i=1; for(GoodsRecord cr : exportList){ //将属性转化成字符串数组的格式以便于写到sheet中 String c[] = {i+"", cr.getCustomMasterLabel(), cr.getIeFlagLabel(), cr.getTradeCode(), cr.getTradeName(), cr.getPlatformCode(), cr.getPlatformName(), cr.getAgentCode(), cr.getAgentName(), cr.getPreclassifyCode(), cr.getPreclassifyName(), cr.getApplyDate()+"", cr.getCreateUser(), cr.getCreateDate()+""}; //创建行(从下面的i+1要注意,第0行是列头,因此创建新行要从下一行开始) HSSFRow row = sheet.createRow(i); //创建所需的行数 for(short j=0;j<columnNum;j++){ HSSFCell cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);//设置单元格的数据类型 cell.setCellValue(c[j]); //设置单元格的值 cell.setCellStyle(style); //设置单元格样式 } i++; } } if(workbook !=null){ // 获取当前时间用作文件名 String filename = modelFlag + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); response.setContentType("application/ms-excel"); response.setHeader("Content-Disposition", "filename="+new String(filename.toString().concat(".xls").getBytes(),"UTF-8")); workbook.write(out); out.flush(); // 缓冲 out.close(); // 关闭流(养成好的习惯打开了就别忘记关闭) } } catch (Exception e) { // TODO: handle exception return "export fail"; } return "export success"; } private String getApplyTypeLabel(String value){ if("1".equals(value)){ return "新增"; }else if("2".equals(value)){ return "变更"; }else if("3".equals(value)){ return "删除"; }else{ return ""; } } private String getDocStatusLabel(String value){ if("1".equals(value)){ return "暂存"; }else if("2".equals(value)){ return "提交"; }else if("Y".equals(value)){ return "新增、修改、删除成功"; }else if("N".equals(value)){ return "新增、修改、删除失败"; }else{ return ""; } } }
模板类:ExportExcelUtilsModel.java(仅仅只列出了两个模块)
package com.hmnet.common; public class ExportExcelUtilsModel{ //企业信息备案 static String[] companyRecord = {"序号","主管海关代码","企业海关编码","企业中文名称","企业经营类别", "企业组织机构代码","企业简称标识","海关业务联系人","海关业务联系人电话","工商注册地址","网络平台名称", "网络平台网址","企业网址","申请日期","申报类型","业务状态","创建人员","创建日期"}; //海关商品备案 static String[] goodsRecord = {"序号","海关简称","进出口标志","电商企业代码","电商企业名称","电商平台企业代码","电商平台企业名称", "申报企业代码","申报企业名称","预归类企业代码","预归类企业名称","申报日期","创建人员","创建日期"}; //产品管理 public static String[] getExportMoedel(String modelFlag) { if(modelFlag == "companyRecord"){ return companyRecord ; }else if(modelFlag == "goodsRecord"){ return goodsRecord ; }else{ return null; } } }
样式类:ExportExcelUtilsStyle.java
package com.hmnet.common; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.HSSFColor; public class ExportExcelUtilsStyle{ /* * 列头单元格样式 */ public static HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) { // 设置字体 HSSFFont font = workbook.createFont(); //设置字体大小 font.setFontHeightInPoints((short)11); //字体加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置字体名字 font.setFontName("Courier New"); //设置样式; HSSFCellStyle style = workbook.createCellStyle(); //设置底边框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底边框颜色; style.setBottomBorderColor(HSSFColor.BLACK.index); //设置左边框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边框颜色; style.setLeftBorderColor(HSSFColor.BLACK.index); //设置右边框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边框颜色; style.setRightBorderColor(HSSFColor.BLACK.index); //设置顶边框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置顶边框颜色; style.setTopBorderColor(HSSFColor.BLACK.index); //在样式用应用设置的字体; style.setFont(font); //设置自动换行; style.setWrapText(false); //设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } /* * 列数据信息单元格样式 */ public static HSSFCellStyle getStyle(HSSFWorkbook workbook) { // 设置字体 HSSFFont font = workbook.createFont(); //设置字体大小 //font.setFontHeightInPoints((short)10); //字体加粗 //font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //设置字体名字 font.setFontName("Courier New"); //设置样式; HSSFCellStyle style = workbook.createCellStyle(); //设置底边框; style.setBorderBottom(HSSFCellStyle.BORDER_THIN); //设置底边框颜色; style.setBottomBorderColor(HSSFColor.BLACK.index); //设置左边框; style.setBorderLeft(HSSFCellStyle.BORDER_THIN); //设置左边框颜色; style.setLeftBorderColor(HSSFColor.BLACK.index); //设置右边框; style.setBorderRight(HSSFCellStyle.BORDER_THIN); //设置右边框颜色; style.setRightBorderColor(HSSFColor.BLACK.index); //设置顶边框; style.setBorderTop(HSSFCellStyle.BORDER_THIN); //设置顶边框颜色; style.setTopBorderColor(HSSFColor.BLACK.index); //在样式用应用设置的字体; style.setFont(font); //设置自动换行; style.setWrapText(false); //设置水平对齐的样式为居中对齐; style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //设置垂直对齐的样式为居中对齐; style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); return style; } }
调用代码:
List<GoodsRecord> goodsRecordList = goodsRecordService .selectByCriteria(goodsRecordCriteria, offset, limit); ExportExcelUtils exportExcelUtils= new ExportExcelUtils(); Map<String, List> map = new HashMap<String, List>(); map.put("exportList", goodsRecordList); String result = exportExcelUtils.exportExcel(map,"goodsRecord");
注:以上代码所需要的jar包和具体的VO我就不提供了,另外有些细节不完整,稍微改下参数什么的就可以运行的。
原文地址:http://blog.csdn.net/xlb744868186/article/details/42461597