項目框架:SpringMVC Spring Mybatis
權(quán)限框架:Shiro
一长已、創(chuàng)建對應(yīng)的formbean 不是model恬汁,里面屬性為需要導(dǎo)出的屬性,這里我只導(dǎo)出編碼和名稱
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;
}
}
二顽铸、點擊導(dǎo)出按鈕娜膘,跳轉(zhuǎn)到action對應(yīng)的方法
findShopBillInfoByCondition方法要在service實習(xí) 和 在對應(yīng)的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<StoreBillInfo> queryresult = to(StoreBillInfoService.class).findShopBillInfoByCondition(condition);
String fileName = "供應(yīng)商編碼名稱表(" + DateTimeUtils.fullStrFormat() + ").xls"; //導(dǎo)出excel文件名稱
List<StoreBillInfo> list = queryresult.getResultList();
if(list != null && !list.isEmpty()){
BillSheet.billInfoExport(FILE_PATH, list, formbean, fileName, response);
}
}
}
三竣贪、同formbean一樣 只寫需要導(dǎo)出的屬性
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對應(yīng)的配置文件 xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ikcsoft.service.store.impl.StoreBillInfoServiceImpl">
<resultMap id="entityResultMap" type="com.ikcsoft.model.store.StoreBillInfo">
<result property="store_no" column="store_no" />
<result property="store_name" column="store_name" />
</resultMap>
<!-- 導(dǎo)出商家編號 商家名 -->
<select id="findShopBillInfoByCondition" parameterType="com.ikcsoft.utils.Condition"
resultType="com.ikcsoft.model.store.StoreBillInfo">
SELECT store_no,store_name FROM gl_store
<if test="queryCondition !=null and queryCondition != ''" >
${queryCondition}
</if>
</select>
<!-- 導(dǎo)出商家編號 商家名 -->
<select id="findStoreBillInfoTotalRecord" parameterType="com.ikcsoft.utils.Condition" resultType="long">
SELECT COUNT(*) FROM gl_store
<if test="queryCondition !=null and queryCondition != ''" >
${queryCondition}
</if>
</select>
</mapper>
四、第二步里面最后的方法 BillSheet.billInfoExport
public static void billInfoExport(String path, List<StoreBillInfo> list, StoreBillInfoForm formbean, String fileName, HttpServletResponse response){
try {
String templateName = "xls/storebilllist.xls"; // 這個excel文件是導(dǎo)出excel的模板巩螃,需要放在指定位置演怎,比如說第二行第一列寫 編碼 第二列寫名稱
InputStream is = new FileInputStream(path + templateName);
HSSFWorkbook workbook = new HSSFWorkbook(is);// 創(chuàng)建 一個excel文檔對象
HSSFSheet sheet = workbook.getSheetAt(0);
sheet.setDefaultColumnWidth(15);
sheet.getRow(0).createCell(1).setCellValue(DateTimeUtils.fullFormat()); // 第一行第一列 設(shè)置一個時間
if(list != null && !list.isEmpty()){
for (int i = 0; i < list.size(); i ++) {
StoreBillInfo storeBillInfo = list.get(i);
HSSFRow sheetRow = sheet.createRow(i + 2);// 創(chuàng)建一個行對象 從第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("導(dǎo)出供應(yīng)商編碼名稱:", e);
}
}
}