首先,这是我对自己的需求而使用的逻辑,若有可以完美的地方方便告诉下小白。
apache的poi MAVEN
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>
</dependency>
1、前端页面,伪异步(页面不刷新)
为什么不用ajax呢?
JQuery的ajax函数的返回类型只有xml、text、json、html等类型,没有“流”类型。所以就用js做个form表单请求
上代码()
function exportExcel(){
var myurl="${context}/assetInLibrary/export";
var form=$("<form>");
form.attr("style","display:none");
form.attr("method","post");
form.attr("action",myurl);
$("body").append(form);
}
2、在工具包中创建ViewExcel,继承AbstractExcelView
先上代码
public class ViewExcel extends AbstractExcelView { private String[] titles; //传入指定的标题头
public ViewExcel(String[] titles) {
this.titles=titles;
} @Override
protected void buildExcelDocument(Map<String, Object> model,
HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
//获取数据
List<Map<String, String>> list = (List<Map<String, String>>) model.get("excelList");
//在workbook添加一个sheet
HSSFSheet sheet = workbook.createSheet();
sheet.setDefaultColumnWidth(15);
HSSFCell cell=null;
//遍历标题
for (int i = 0; i < titles.length; i++) {
//获取位置
cell = getCell(sheet, 0, i);
setText(cell, titles[i]);
}
//数据写出
for (int i = 0; i < list.size(); i++) {
//获取每一个map
Map<String, String> map=list.get(i);
//一个map一行数据
HSSFRow row = sheet.createRow(i+1);
for (int j = 0; j < titles.length; j++) {
//遍历标题,把key与标题匹配
String title=titles[j];
//判断该内容存在mapzhong
if(map.containsKey(title)){
row.createCell(j).setCellValue(map.get(title));
}
}
}
//设置下载时客户端Excel的名称
String filename = new SimpleDateFormat("yyyy-MM-dd").format(new Date())+".xls";
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + filename);
OutputStream ouputStream = response.getOutputStream();
workbook.write(ouputStream);
ouputStream.flush();
ouputStream.close();
} }
在构造函数中传进来需导出的titles,也就是excel中的标题头,这个逻辑会有点麻烦,因为我是创建Map,让dao中查出来的数据根据我的Map(‘title’,'value')进行封装,且title要存在于传进来的titles中,剩下看源码就能明白
3、service中的数据封装
public List<Map<String, String>> selectAllAssetInlibraryInfo() {
List<AssetInlibrary> list = assetInlibraryMapper.selectByExample(null);
List<Map<String, String>> mapList=new ArrayList<Map<String,String>>();
for (AssetInlibrary assetInlibrary : list) {
Map<String, String> map=new HashMap<String, String>();
map.put("编号", assetInlibrary.getId()+"");
map.put("资产名称", assetInlibrary.getTitle());
AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetInlibrary.getAssetTypeId());
map.put("资产类型", assetType.getTitle());
AssetBrand assetBrand = assetBrandMapper.selectByPrimaryKey(assetInlibrary.getAssetBrandId());
map.put("资产品牌", assetBrand.getTitle());
AssetStorage assetStorage = assetStorageMapper.selectByPrimaryKey(assetInlibrary.getAssetStorageId());
map.put("资产存放地点", assetStorage.getTitle());
AssetProvider assetProvider = assetProviderMapper.selectByPrimaryKey(assetInlibrary.getAssetProviderId());
map.put("资产供应商", assetProvider.getTitle());
mapList.add(map);
}
return mapList;
}
4、controller中的数据交互
@RequestMapping("/assetInLibrary/export")
public ModelAndView export(ModelMap map) throws Exception{
List<Map<String,String>> list = assetInLibraryService.selectAllAssetInlibraryInfo();
String[] titles={"编号","资产名称","资产类型","资产品牌","资产存放地点","资产供应商"};
ViewExcel excel=new ViewExcel(titles);
map.put("excelList", list);
return new ModelAndView(excel,map);
}
公众号
欢迎关注我的公众号“码上开发”,每天分享最新技术资讯、最优原创文章。关注获取最新资源
版权声明:本文为不会代码的小白原创文章,未经允许不得转载。