先导入几个poi包
poi-3.8-20120326.jar
poi-excelant-3.8-20120326.jar
poi-ooxml-3.8-20120326.jar
poi-ooxml-schemas-3.8-20120326.jar
poi-scratchpad-3.8-20120326.jar
Controller.java
//点击按钮下载表格
@RequestMapping("/excel.do")
public ModelAndView downExcel(HttpServletResponse response){
String filename;
try {
filename = new String("excel文件名称".getBytes(),"iso8859-1");
response.setHeader("Content-Disposition","attachment;filename="+filename+".xls");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Map<String,List> dataMap = new HashMap<String,List>();
List<excelinfo> logList =excel要显示的所有数据
dataMap.put("excelList", logList);
ExcelView excelview=new ExcelView();
return new ModelAndView(excelview,dataMap);
}
ExcelViewExcelView继承AbstractExcelView,,excelinfo作为实体类存放一行数据
package com.lu.Dao;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
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.springframework.web.servlet.view.document.AbstractExcelView;
import com.lu.entity.excelinfo;
public class ExcelView extends AbstractExcelView {
@Override
protected void buildExcelDocument(Map<String, Object> excelList,
HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)
throws Exception {
HSSFCell cell;
List<excelinfo> loglist = (List<excelinfo>) excelList.get("excelList");//从控制器Controller中返回的业务数据
int len=loglist.size();
HSSFSheet sheet = workbook.createSheet("报名表"); //创建表格
HSSFCellStyle headerStyle = workbook.createCellStyle(); //标题样式
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont headerFont = workbook.createFont(); //标题字体
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerFont.setFontHeightInPoints((short)11);
headerStyle.setFont(headerFont);
HSSFRow title = sheet.createRow(0);//定义表格的表头第一行
String[] headname={"xx","xx","xx","xx","xx","xx","xx","xx"};
for (int i = 0; i < headname.length; i++) {
cell = title.createCell(i); //定义第一行的第i列
HSSFRichTextString text = new HSSFRichTextString(headname[i]);
cell.setCellValue(text);
//设置单元格格式为字符串类型
cell.setCellType(HSSFCell.ENCODING_UTF_16);
}
HSSFRow row;
int num=1;
for(excelinfo excelnum:loglist){
row= sheet.createRow(num++); //创建表格行
row.createCell(0).setCellValue(excelnum.getId());
row.createCell(1).setCellValue(excelnum.getProduction_name());
row.createCell(2).setCellValue(excelnum.getSchool());
row.createCell(3).setCellValue(excelnum.getClassify());
row.createCell(4).setCellValue(excelnum.getTeam());
row.createCell(5).setCellValue(excelnum.getAuthor_one());
row.createCell(6).setCellValue(excelnum.getAuthor_sec());
row.createCell(7).setCellValue(excelnum.getAuthor_third());
}
}
}