采用的是poi技术
框架式 ssh 使用的是struts1
/*
*
* 导出联系人
*
* @param request
* @param response
*/
public void outExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
ServletContext servletContext = this.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
TraceLxrService service = getTraceLxrService();
List<AbstractTraceLxr> list = service.findAllQX();
try {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("区县工作小组名单");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 创建一个居中格式 HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("区县");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("分工");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("部门");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("职务");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("电话");
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setCellValue("手机");
cell.setCellStyle(style);
cell = row.createCell((short) 7);
cell.setCellValue("传真");
cell.setCellStyle(style);
cell = row.createCell((short) 8);
cell.setCellValue("Email");
cell.setCellStyle(style);
cell = row.createCell((short) 9);
cell.setCellValue("修改");
cell.setCellStyle(style);
cell = row.createCell((short) 10);
cell.setCellValue("删除");
cell.setCellStyle(style);
List<AbstractTraceLxr> newlist=new ArrayList<AbstractTraceLxr>();
List<String> list1 = service.findAllQXDz();
for (int i = 0; i < list1.size(); i++) {
for (int p = 0; p < list.size(); p++) {
if(list1.get(i).equals(list.get(p).getSsqx())){
TraceLxr sf=new TraceLxr();
sf.setSsqx(list.get(p).getSsqx());
sf.setFg(list.get(p).getFg());
sf.setName(list.get(p).getName());
sf.setDept(list.get(p).getDept());
sf.setJob(list.get(p).getJob());
sf.setTel(list.get(p).getTel());
sf.setMobile(list.get(p).getMobile());
sf.setFax(list.get(p).getFax());
sf.setEmail(list.get(p).getEmail());
sf.setDelflag("修改");
sf.setDelflag("删除");
newlist.add(sf);
}
}
}
for (int k1 = 0; k1 < newlist.size(); k1++) {
row = sheet.createRow((int) k1 + 1);
TraceLxr bgMultiLevel = (TraceLxr) newlist.get(k1);
row.createCell((short) 0).setCellValue(bgMultiLevel.getSsqx());
row.createCell((short) 1).setCellValue(bgMultiLevel.getFg());
row.createCell((short) 2).setCellValue(bgMultiLevel.getName());
row.createCell((short) 3).setCellValue(bgMultiLevel.getDept());
row.createCell((short) 4).setCellValue(bgMultiLevel.getJob());
row.createCell((short) 5).setCellValue(bgMultiLevel.getTel());
row.createCell((short) 6).setCellValue(bgMultiLevel.getMobile());
row.createCell((short) 7).setCellValue(bgMultiLevel.getFax());
row.createCell((short) 8).setCellValue(bgMultiLevel.getEmail());
row.createCell((short) 9).setCellValue("0");
row.createCell((short) 10).setCellValue("1");
if(k1!=0){
if(newlist.get(k1-1).getSsqx().equals(newlist.get(k1).getSsqx())){
//重点在这里动态合并
CellRangeAddress cra=new CellRangeAddress(k1, 1+k1, 0, 0);
//在sheet里增加合并单元格
sheet.addMergedRegion(cra);
}
}
}
response.reset();
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + new String(("exportExcel".toString() + ".xls").getBytes(), "iso-8859-1"));
OutputStream os = response.getOutputStream();
wb.write(os); os.close();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
来源:https://www.cnblogs.com/liuy1/p/5672898.html