1.当然是导包,小编就不粘贴了
2.Excel视图解析器配置
<!-- Excel视图解析器配置 -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"
p:order="10" />
<bean id="ProductionExcel" class="com.zking.vo.ProductionExcel"></bean>
3.拿到数据库中的数据,并把集合给ModelMap
@RequestMapping("ProductionExcel.xhtml")
public String BonusListInExcel(ModelMap mm,HttpServletRequest request) throws Exception{
List<Production> myl=ud.getallexcl();
System.out.println(111);
mm.addAttribute("ProductionExcel",myl);
return "ProductionExcel";
}
4.建立一个VO类,创建EXcel,并把值放进去
package com.zking.vo;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.zking.entity.Production;
public class ProductionExcel extends AbstractExcelView{
@Override
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
// private Integer Product_id;
// private String ProductName;
// private String Spec;
// private String Unit;
// private String Productmemo;
response.setHeader("Content-Disposition", "inline; filename="+
new String("商品.xls".getBytes(), "iso8859-1"));
@SuppressWarnings({ "unchecked" })
List<Production> humanFileslist=(List<Production>) model.get("ProductionExcel");
HSSFSheet sheet=workbook.createSheet("商品列表");
sheet.setDefaultColumnWidth(13);
HSSFRow header=sheet.createRow(0);
for (int i = 0; i <4; i++) {
header.createCell(i);
sheet.getRow(0).getCell(i).setCellValue((i==0)?"档案编号":((i==1)?"文件名":((i==2)?"文件类型":((i==3)?"文件类容":"出勤情况"))));
}
int rowNum=1;
for (int i = 0; i <humanFileslist.size(); i++) {
HSSFRow row=sheet.createRow(rowNum++);
Production human=humanFileslist.get(i);
row.createCell(0);
row.getCell(0).setCellValue(human.getProduct_id());
row.createCell(1);
row.getCell(1).setCellValue(human.getProductName());
row.createCell(2);
row.getCell(2).setCellValue(human.getSpec());
row.createCell(3);
row.getCell(3).setCellValue(human.getUnit());
}
}
}