项目框架:SpringMVC Spring Mybatis
权限框架:Shiro
一、创建对应的formbean 不是model,里面属性为需要导出的属性,这里我只导出编码和名称
public class StoreBillInfoForm extends BaseForm {
private static final long serialVersionUID = 1L;
private String store_no; 编码
private String store_name; 名称
public String getStore_no() {
return store_no;
}
public void setStore_no(String store_no) {
this.store_no = store_no;
}
public String getStore_name() {
return store_name;
}
public void setStore_name(String store_name) {
this.store_name = store_name;
}
}
二、点击导出按钮,跳转到action对应的方法
findShopBillInfoByCondition方法要在service实习 和 在对应的xml里面写,将在下面写上。
@Controller
public class StoreBillInfoAction extends BaseManageAction {
private final String FILE_PATH = this.getClass().getClassLoader().getResource("").getPath();
@RequestMapping(value="/control/store/billinfo/export")
public void export(StoreBillInfoForm formbean, HttpServletResponse response) throws IOException{
Condition condition = new Condition();
QueryResult queryresult = to(StoreBillInfoService.class).findShopBillInfoByCondition(condition);
String fileName = "供应商编码名称表(" + DateTimeUtils.fullStrFormat() + ").xls"; //导出excel文件名称
List list = queryresult.getResultList();
if(list != null && !list.isEmpty()){
BillSheet.billInfoExport(FILE_PATH, list, formbean, fileName, response);
}
}
}
三、同formbean一样 只写需要导出的属性
public class StoreBillInfo extends AbstractEntity{
private static final long serialVersionUID = 1L;
private String store_no;
private String store_name;
public String getStore_no() {
return store_no;
}
public void setStore_no(String store_no) {
this.store_no = store_no;
}
public String getStore_name() {
return store_name;
}
public void setStore_name(String store_name) {
this.store_name = store_name;
}
}
上面model对应的配置文件 xml
resultType="com.ikcsoft.model.store.StoreBillInfo">
SELECT store_no,store_name FROM gl_store
${queryCondition}
SELECT COUNT(*) FROM gl_store
${queryCondition}
四、第二步里面最后的方法 BillSheet.billInfoExport
public static void billInfoExport(String path, List list, StoreBillInfoForm formbean, String fileName, HttpServletResponse response){
try {
String templateName = "xls/storebilllist.xls"; // 这个excel文件是导出excel的模板,需要放在指定位置,比如说第二行第一列写 编码 第二列写名称
InputStream is = new FileInputStream(path + templateName);
HSSFWorkbook workbook = new HSSFWorkbook(is);// 创建 一个excel文档对象
HSSFSheet sheet = workbook.getSheetAt(0);
sheet.setDefaultColumnWidth(15);
sheet.getRow(0).createCell(1).setCellValue(DateTimeUtils.fullFormat()); // 第一行第一列 设置一个时间
if(list != null && !list.isEmpty()){
for (int i = 0; i < list.size(); i ++) {
StoreBillInfo storeBillInfo = list.get(i);
HSSFRow sheetRow = sheet.createRow(i + 2);// 创建一个行对象 从第3行开始 跳过第1行第2行
sheetRow.createCell(0).setCellValue(storeBillInfo.getStore_no());
sheetRow.createCell(1).setCellValue(storeBillInfo.getStore_name());
}
}
response.reset();
response.setHeader("Content-Disposition", "attachment;fileName= " + new String(fileName.getBytes("GBK"),"ISO8859-1"));
response.setContentType("application/x-download");
OutputStream outExcel = response.getOutputStream();
workbook.write(outExcel);
outExcel.close();
workbook.close();
} catch (Exception e) {
logger.error("导出供应商编码名称:", e);
}
}
}