本介绍两种Excle导出方法: JAVA 导出 Excle, JS 导出 Excle

1, js 根据 html 页面的 table > tr > td 标签导出

  js代码:

//导出
var idTmr;
function getExplorer() {
var explorer = window.navigator.userAgent ;
//ie
if (explorer.indexOf("MSIE") >= 0) {
return 'ie';
}
//firefox
else if (explorer.indexOf("Firefox") >= 0) {
return 'Firefox';
}
//Chrome
else if(explorer.indexOf("Chrome") >= 0){
return 'Chrome';
}
//Opera
else if(explorer.indexOf("Opera") >= 0){
return 'Opera';
}
//Safari
else if(explorer.indexOf("Safari") >= 0){
return 'Safari';
}
} function exportData(tableid) {
if(getExplorer()=='ie')
{
var curTbl = document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var xlsheet = oWB.Worksheets(1);
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl);
sel.select();
sel.execCommand("Copy");
xlsheet.Paste();
oXL.Visible = true; try {
var fname = oXL.Application.GetSaveAsFilename("Excel.xls", "Excel Spreadsheets (*.xls), *.xls");
} catch (e) {
print("Nested catch caught " + e);
} finally {
oWB.SaveAs(fname);
oWB.Close(savechanges = false);
oXL.Quit();
oXL = null;
idTmr = window.setInterval("Cleanup();", 1);
} }
else
{
tableToExcel(tableid);
}
} function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
} var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,',
template = '<html><head><meta charset="UTF-8"></head><body><table>{table}</table></body></html>',
base64 = function(s) {
return window.btoa(unescape(encodeURIComponent(s)));
},
format = function(s, c) {
return s.replace(/{(\w+)}/g,
function(m, p) {
return c[p]; });
};
return function(table, name) {
if (!table.nodeType) table = document.getElementById(table);
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML};
window.location.href = uri + base64(format(template, ctx));
};
})();

  html 代码:

<input class='btn_04'  type='button' id='exportData' value="导出数据" onclick="exportData('tableExcel')"/>

ps:js 导出可兼容主流浏览器,包括360. 缺陷为:只能导出千级别的数据量,亲测15000条数据不能导出

----------------------------------------------------------------------------------

2, java 导出 Excle文件,根据集合遍历生成excle,需要依赖 poi-3.8-20120326.jar 包(或其他版本,请自行下载)

  java 代码:

       List<ClUserinfo> regUsers = clSecurityService.findClUserinfos(clSecurityForm);

            List<TempUser> userList = new ArrayList<TempUser>();
for (ClUserinfo userinfo : regUsers) {
TempUser user = new TempUser(); user.setUserName(userinfo.getUserName());
user.setUserType(userinfo.getUserType());
user.setCompany(userinfo.getCompany());
user.setKeshi(userinfo.getKeshi());
user.setProvince(userinfo.getProvince());
user.setCreatedDate(userinfo.getCreatedDate());
user.setCreateDateStr(userinfo.getCreatedDate().toString()); userList.add(user);
} HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("注册用户表");
HSSFRow row = sheet.createRow((int) 0);
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("姓名");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("专委会");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("单位");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("科室");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("注册日期");
cell.setCellStyle(style); for (int i = 0; i < userList.size(); i++)
{
row = sheet.createRow((int) i + 1);
TempUser user = (TempUser) userList.get(i); row.createCell((short) 0).setCellValue(user.getUserName());
row.createCell((short) 1).setCellValue(user.getUserType());
row.createCell((short) 2).setCellValue(user.getCompany());
row.createCell((short) 3).setCellValue(user.getKeshi());
row.createCell((short) 4).setCellValue(user.getCreateDateStr());
} try {
FileSystemView fsv = FileSystemView.getFileSystemView();
File com=fsv.getHomeDirectory(); //桌面 FileOutputStream fout = new FileOutputStream(com.getPath() + "/注册用户列表-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + ".xls");
wb.write(fout);
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
}

JAVA 导出 Excel, JS 导出 Excel的更多相关文章

  1. JAVA web端JS下载excel文件

    JSP代码如下: JSP端引入jquery.easyui.min.js库: <script type="text/javascript" src="<c:ur ...

  2. java代码导出数据到Excel、js导出数据到Excel(三)

     jsp内容忽略,仅写个出发按钮:          <button style="width: 100px" onclick="expertExcel()&quo ...

  3. java实现excel模板导出

    一. 准备工作 1. 点击此下载相关开发工具 2. 将poi-3.8.jxls-core-1.0两个jar包放到工程中,并引用 3. 将excel模板runRecord.xls放到RunRecordB ...

  4. CefSharp中实现Chrome中jS导出Excel

    [前言] 在博客园闲逛了一年多,平时都是借鉴别人的成功经验,总觉得自己应该分享点什么,但是苦于自己技术有限,平时又不爱写东西,所以一直没有写过任何东西.毕业一年多,在现实工作中遇到各种问题,深切体会到 ...

  5. Java Excel导入导出(实战)

    一.批量导入(将excel文件转成list) 1. 前台代码逻辑 1)首先在html页面加入下面的代码(可以忽略界面的样式) <label for="uploadFile" ...

  6. 关于Java中excel表格导出的总结(Java程序导出模板和Java根据模板导出表格两种实现方式)

    导出excel通用模板(程序定义模板导出) 转载原文:https://www.jianshu.com/p/5c7b359a159c 如下代码,本方法主要用于程序定义模板格式,并导出文件.该方法将定义和 ...

  7. Java操作Jxl实现导出数据生成Excel表格数据文件

    实现:前台用的框架是Easyui+Bootstrap结合使用,需要引入相应的Js.Css文件.页面:Jsp.拦截请求:Servlet.逻辑处理:ClassBean.数据库:SQLserver. 注意: ...

  8. java+layui的Excel导入导出

    html: <button class="layui-btn" onclick="exportData();">导出</button> ...

  9. html js 导出excel表格

    这个使用js 导出excel,可以集成其他语言,可以html,php,asp ,java 等,自己喜欢用那种语言就用哪种,使用非常方便.js是使用tableExport.js ,jquery-3.2. ...

随机推荐

  1. NLayerAppV3-Infrastructure(基础结构层)的Data部分和Application(应用层)

    回顾:NLayerAppV3是一个使用.net 2.1实现的经典DDD的分层架构的项目. NLayerAppV3是在NLayerAppV2的基础上,使用.net core2.1进行重新构建的:它包含了 ...

  2. svn 连接超时,连接失败解决办法

    1.确认服务是否开启 2.Windows防火墙是否开启,如开启则关闭防火墙 3.安全软件是否将3306与443端口关闭. 关闭后无法连接

  3. 微信游戏《全民炫舞》开发公司h3d2 engine和QQ炫舞2 布料系统技术介绍

    H3D公司开发的<全民炫舞>上线了. 蝉联IOS榜首很多天. 整理了一下过去公司游戏引擎开发的历史.有兴趣可以去看看 公司游戏引擎开发历史介绍: http://www.h3d.com.cn ...

  4. socket agent统一模板

    # -*- coding: utf- -*- # data:-- : # user:DIY # file:agent_eay.py import socket def work(i): sock = ...

  5. Flask的WTforms

    一.简单介绍 WTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证. 类似于Django中的modelform 安装: pip3 install wtforms 二.简 ...

  6. python 爬图 helloworld

    最近发现 吾志 上用户的头像都很个性,另外,对于没有把日记设为私密的用户,最后一天的日记是公开的,谁都可以查看. 所以,如果每天把所有可查看的日记爬一遍,那么-- 哈哈 以前对爬虫只是了解一点点,没有 ...

  7. 使用VS Code开发.Net Core 2.0 MVC Web应用程序教程之三(配置文件读取)

    干了一天的活,还有点时间,给兄弟们写点东西吧. 大家有没有发现一个问题?那就是在.Net Core的MVC项目里面,没有.config文件了!!!同志们,没有config文件了啊,这样搞,我以后要做些 ...

  8. maven安装以及eclipse配置maven

    详细地址: http://jingyan.baidu.com/article/295430f136e8e00c7e0050b9.html 介绍安装maven,配置Maven环境变量,同时在Eclips ...

  9. js之math 对象

    Math 对象是js中使用数学公式计算的便捷方法,其方法运行起来比直接写的js运行是对要更快 1.Math.min(一组数值)  该方法可以比较一组数值的大小,并且返回较小的数值 用法: Math.m ...

  10. 扩展中国剩余定理(扩展CRT)详解

    今天在$xsy$上翻题翻到了一道扩展CRT的题,就顺便重温了下(扩展CRT模板也在里面) 中国剩余定理是用于求一个最小的$x$,满足$x\equiv c_i \pmod{m_i}$. 正常的$CRT$ ...