导出Excel功能分sheet页处理数据:

/*导出EXCEL*/
public void createExcel() {
log.info("导出Excel功能已经启动-BEGIN");
JxlUtil jsl = new JxlUtil();
List<Device> dataList =new ArrayList<Device>();
List<DeviceExport> list = new ArrayList<DeviceExport>();
// 构建路径
String downLoadPath = "/WEB-INF/download/asset/";
String rootPath = getServletContext().getRealPath(downLoadPath); String fileName = "";
File file=new File(rootPath); try {
if(!(file.exists()||file.isDirectory())){
file.mkdirs();
} if (null != getRequest().getParameter("ids")) {
String ids[] = getRequest().getParameter("ids").split(",");
for (int i = ; i < ids.length; i++) {
if(null!=ids[i]){
device = deviceService.queryById(ids[i].trim());
dataList.add(device);
}
}
}else{
dataList = deviceService.queryForExcel();
} if(dataList!=null){
for (Device device : dataList) {
DeviceExport deviceExport= new DeviceExport();
if(null!=device.getId()){
deviceExport.setId(device.getId());
}
if(null!=device.getIp()){
deviceExport.setIp(device.getIp());
}
if(null!=device.getMac()){
deviceExport.setMac(device.getMac());
}
if(null!=device.getName()){
deviceExport.setName(device.getName());
}
if(null!=device.getOrganization()){
Organization organization=device.getOrganization();
String Aname =organization.getName();
String name= getAname(organization, Aname);
deviceExport.setOrganizationName(name);
}
if(null!=device.getRegState()){
deviceExport.setRegState(device.getRegState());
}
if(null!=device.getUser()){
deviceExport.setUserName(device.getUser().getName());
}
if(null!=device.getProtectState()){
deviceExport.setProtectState(device.getProtectState());
}
if(null!=device.getIsOpened()){
deviceExport.setIsOpened(device.getIsOpened());
}
String osName = MessageUtils.getMessage(device.getOs().getName());
deviceExport.setOsName(osName);
String deviceType = MessageUtils.getMessage(device.getDeviceType().getName());
deviceExport.setDeviceTypeName(deviceType);
list.add(deviceExport);
}
} String interBase = "sys.column.name.device";
//String inter_value_key = "#isOpened#roamState#protectState#";
String inter_value_key = "#isOpened#protectState#regState#";
String[] inter_value_ary = { "isOpened.0", "isOpened.1", "protectState.0", "protectState.1","regState.0","regState.1"};
//导出不显示漫游状态
//String[] inter_value_ary = { "isOpened.0", "isOpened.1","roamState.0", "roamState.1","protectState.0", "protectState.1"}; fileName = jsl.getInter(interBase.replace("column", "table"))
+ new Date().getTime();
String targetfile = rootPath + System.getProperty("file.separator")
+ fileName + ".xls"; //分sheet页处理
int total = dataList.size();//总数
int max = ;//每sheet页允许最大数
int avg = total / max;//sheet页个数 // 创建可写入的Excel工作薄
WritableWorkbook wwb;
wwb = Workbook.createWorkbook(new File(targetfile)); for(int i=;i<avg+;i++){
// 创建Excel工作表
WritableSheet ws = wwb.createSheet("已注册设备"+(i+), i);
int num = i * max;
int index = ;
List<DeviceExport> exportList = new ArrayList<DeviceExport>();
for(int m = num; m < total; m++){//m即为每个sheet页应该开始的数
if(index == max){//判断 index = max 的时候跳出里层的for循环
break;
}
DeviceExport deviceExport=list.get(m);
exportList.add(deviceExport);//从总的list数据里面取出该处于哪个sheet页的数据,然后加进exportList,exportList即为当前sheet页应该有的数据
index++;
}
// 获取需要内容国际化的字段
jsl.creatDeviceExcel(ws, exportList, interBase, inter_value_key,inter_value_ary);
} // 写入Exel工作表
wwb.write();
// 关闭Excel工作薄对象
wwb.close(); getResponse().setContentType(getServletContext().getMimeType(fileName));
getResponse().setHeader("Content-Disposition", "attachment;fileName="+new String(fileName.getBytes("gb2312"), "ISO8859-1")+".xls");
String fullFileName = getServletContext().getRealPath(downLoadPath + fileName+ ".xls");
InputStream in = new FileInputStream(fullFileName);
OutputStream out = getResponse().getOutputStream();
int b;
while((b=in.read())!= -){
out.write(b);
}
in.close();
out.close();
/*ServletActionContext.getRequest().setAttribute("downLoadPath",
downLoadPath);
ServletActionContext.getRequest().setAttribute("fileName",
fileName + ".xls");*/
this.msg = RESULT_SUCCESS;
log.info("导出EXCEL提示信息为:"+this.msg);
} catch (Exception e) {
log.error("导出EXCEL失败:" + e.getMessage());
}
log.info("导出Excel功能已经启动-END");
/*return "downLoadUI";*/
}

  导出效果如下:

项目笔记:导出Excel功能分sheet页插入数据的更多相关文章

  1. 公司项目笔记-导出excel

    一.asp.net中导出Excel的方法: 在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出流写给浏览 ...

  2. 项目笔记:导出Excel功能

    1.前台这块: var ids=""; $.post("${basePath}/assets/unRegDeviceAction_getDeviceIds.do" ...

  3. vue项目导出EXCEL功能

    因为一些原因导出EXCEL功能必须前端来做,所以就研究了一下,在网上也找了一些文章来看,有一些不完整,我做完了就记录下来,供大家参考: 1.首先先安装依赖: npm install file-save ...

  4. Vue通过Blob对象实现导出Excel功能

    不同的项目有不同的导出需求,有些只导出当前所显示结果页面的表格进入excel,这个时候就有很多插件,比如vue-json-excel或者是Blob.js+Export2Excel.js来实现导出Exc ...

  5. Atitit.导出excel功能的设计 与解决方案

    Atitit.导出excel功能的设计 与解决方案 1.1. 项目起源于背景1 1.2. Js  jquery方案(推荐)jquery.table2excel1 1.3. 服务器方案2 1.4. 详细 ...

  6. CRM 报表导出excel时指定sheet名

    如图所示,设置PageName即可: 这样导出excel时,sheet的名就有了:

  7. 多张报表导出到一个多sheet页excel

     业务需求: 通过勾选不同的报表名称,然后直接执行导出excel.并且这些报表需要统一导入到一个excel的多个sheet页中,并且对某些报表可能需要增加一些类似'已审核'之类的图片(展现时并没有 ...

  8. 【angularjs】pc端使用angular搭建项目,实现导出excel功能

    此为简单demo. <!DOCTYPE html> <html ng-app="myApp"> <head> <meta charset= ...

  9. spring mvc项目中导出excel表格简单实现

    查阅了一些资料,才整理出spring mvc 项目导出excel表格的实现,其实很是简单,小计一下,方便以后查阅,也希望帮助有需要的朋友. 1.导入所需要依赖(Jar包).我使用的是maven,所以坐 ...

随机推荐

  1. hdu 6102 GCDispower

    多校6 GCDispower(容斥) 题意: 给一个长度为\(n\)的排列 给q组询问 每次查询\(L,R\)内的答案 \(\sum_{i=L}^{R}\sum_{j=i+1}^{R}\sum_{k= ...

  2. Java基础复习--java.util.Timer定时任务

    在java中,Timer类主要用于定时性.周期性任务 的触发,这个类中有两个方法比较难理解,那就是schedule和scheduleAtFixedRate方法,在这里就用实例分析一下. (1)sche ...

  3. ubuntu 解压

    .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ---------------------- ...

  4. tips server ssh 正向 反向 代理

    1. ssh userxxxxname@115.28.87.102           (直接使用ssh的连接方式连接到远程主机,而不是使用http,ftp等方式连接到具体远程主机)          ...

  5. 小红帽的画笔(NOIP模拟赛Round 7)

    又到了神奇的模拟赛时间~ 真是丧~ 好吧我们来看看题目 小红帽是Pop star上最著名的人类画家,她可以将任何画出的东西变成真实的物品.赋予她这样神奇能力的正是她手上的画笔. 小红帽每次作画时,都需 ...

  6. cocos2d-iphone心得

    源码下载地址: http://code.google.com/p/cocos2d-iphone/downloads/list https://github.com/cocos2d/cocos2d-ip ...

  7. 如何在Android 或Linux 下,做Suspend /Resume 的Debug【转】

    转自:http://blog.csdn.net/jacobywu/article/details/24735521 目录(?)[-] Question Answer 加boot 參數 no_conso ...

  8. NetStream论文

    https://max.book118.com/html/2016/0102/32573670.shtm http://www.docin.com/p-1568348795.html

  9. Codeforces Beta Round #4 (Div. 2 Only) C. Registration system【裸hash/map】

    C. Registration system time limit per test 5 seconds memory limit per test 64 megabytes input standa ...

  10. Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence【构造/GCD】

    C. Marco and GCD Sequence time limit per test 1 second memory limit per test 256 megabytes input sta ...