所需jar包:poi-3.8-20120326.jar
package excl; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import javax.servlet.http.HttpServletResponse; 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.HSSFRichTextString; 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.hssf.util.HSSFColor; import org.apache.struts2.ServletActionContext; public class TestExcl { public static void exportExcel() throws IOException{ //声明一个工作簿 HSSFWorkbook workbook= new HSSFWorkbook(); //生成一个表格 HSSFSheet sheet = workbook.createSheet("我的表格"); //设置表格的默认宽度为20个字节 sheet.setDefaultColumnWidth(20); //生成一个样式 HSSFCellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(HSSFColor.GOLD.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //生成一个字体 HSSFFont font = workbook.createFont(); font.setColor(HSSFColor.VIOLET.index); //font.setFontHeightInPoints((short) 12); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //把字体应用到当前样式 style.setFont(font); //指定当前单元格内容显示不下时自动换行 style.setWrapText(true); /* 以下可以用于设置导出的数据的样式 // 生成并设置另一个样式 HSSFCellStyle style2 = workbook.createCellStyle(); style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一个字体 HSSFFont font2 = workbook.createFont(); font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL); // 把字体应用到当前的样式 style2.setFont(font2); // 声明一个画图的顶级管理器 HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); // 定义注释的大小和位置,详见文档 HSSFComment comment = patriarch.createComment(new HSSFClientAnchor(0, 0, 0, 0, (short) 4, 2, (short) 6, 5)); // 设置注释内容 comment.setString(new HSSFRichTextString("可以在POI中添加注释!")); // 设置注释作者,当鼠标移动到单元格上是可以在状态栏中看到该内容. comment.setAuthor("leno"); */ //设置标题行 HSSFRow row = sheet.createRow(0); List<String> list = new ArrayList<String>(); list.add("姓名"); list.add("性别"); list.add("年龄"); list.add("身高"); for(int i=0;i<list.size();i++){ HSSFCell cell=row.createCell(i); cell.setCellStyle(style); HSSFRichTextString text = new HSSFRichTextString(list.get(i)); cell.setCellValue(text); } //遍历集合数据,产生数据行 List<LinkedHashMap<String,Object>> lists = new ArrayList<LinkedHashMap<String,Object>>(); LinkedHashMap<String,Object> l=new LinkedHashMap<String,Object>(); l.put("xm", "zhangsan"); l.put("xb", "nv"); l.put("nl", "25"); l.put("sg", "165"); lists.add(l); String[] columns = {"xm","xb","nl","sg"}; int index=1; for(LinkedHashMap<String,Object> link:lists){ row = sheet.createRow(index); int cellIndex=0; for(String s:columns){ HSSFCell cell = row.createCell(cellIndex); HSSFRichTextString text = new HSSFRichTextString(link.get(s)==null?"":link.get(s).toString()); cell.setCellValue(text); cellIndex++; } index++; } HttpServletResponse response = ServletActionContext.getResponse(); OutputStream out = response.getOutputStream(); workbook.write(out); } public String onHead(){ try { HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("octets/stream"); response.addHeader("Content-Disposition","attachment;filename=test.xls"); OutputStream out = response.getOutputStream(); exportExcel(); out.close(); } catch (Exception e) { } return null; } }
response的用法:
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType("octets/stream");
response.addHeader("Content-Disposition","attachment;filename=test.xls");
OutputStream out = response.getOutputStream();
exportExcel();
out.close();