批量或者选择导出datagrid列表数据到表格
//导出项目信息
function exportXmxx(){
//判断是否选择站址
var index = $("#dgObj").datagrid('getChecked');
if(index.length == ){
alert('请选择您要导出的信息!');
return;
}
var sheetId = "";
if(!selectAllPageFlag){//是否全选
for (var i = ; i < index.length; i++) {
var row = index[i];
if(sheetId == ""){
sheetId = row.SHEETID;
}else{
sheetId = sheetId+","+row.SHEETID;
}
}
}
var data = {
xmmc:$("#ssxm").combobox("getText"),//所属项目(模糊)
xmlx:$("#xmlx").combobox("getText"),//项目类型(模糊)
xmzt:$("#xmzt").combobox("getValue"),//项目状态
sheetId:sheetId//选中记录的IDS
}; var url = window.location.protocol + "//" + window.location.host + resources.webappName + "/servlet/exportXmxx";
name = "导出项目信息表";
openPostWindow(url, JSON.stringify(data), name); //调用openPostWindow方法,请求JSP url post 传递参数
}
/*********请求JSP url post 传递参数**********************/
function openPostWindow(url, data, name) {
var tempForm = document.createElement("form");
data = encodeURI(encodeURI(data));//两次加密避免中文乱码
tempForm.id="tempForm1";
tempForm.method="post";
tempForm.action=url;
if(name != null && name!=undefined && name != ''){//当需要打开一个新窗口时必须要传递一个name
tempForm.target=name;
}else{
tempForm.target="_self";
}
var hideInput = document.createElement("input");
hideInput.type="hidden";
hideInput.name= "params";//接收的名字
hideInput.value= data;
tempForm.appendChild(hideInput);
if(name != null && name!=undefined && name != ''){//当需要打开一个新窗口时必须要传递一个name
tempForm.attachEvent("onsubmit",function(){ _openWindow(name); });
}else{
tempForm.attachEvent("onsubmit",function(){ return false; });
}
document.body.appendChild(tempForm);
tempForm.fireEvent("onsubmit");
tempForm.submit();
document.body.removeChild(tempForm);
} function _openWindow(name){
window.open('about:blank',name,'channelmode=1,fullscreen=1');
}
1.以上为前台部分
2.做好对应的配置
3.以下为后台代码参考示例:
package com.sdjxd.rsgxm.xmzfzr.dao; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdjxd.pms.platform.data.DbOper;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import sadmpweb.zzgl.Util; public class exportXmxxDao extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
Date dt = new Date();
String time = sdf.format(dt);
String path1 = System.getProperty("user.dir");
path1 = path1.substring(, path1.indexOf("bin")) + "webapps\\sadmpserver\\";
String sourcePath = path1 + "resource\\数据导出公用模板.xlsx";
String descPath = path1 + "resource";
File fileDir = new File(descPath);
if (!fileDir.exists()) {
fileDir.mkdir();
}
String targetFileName = "弱栅格—项目信息表导出—" + time + ".xlsx";
File targetFile = new File(fileDir, targetFileName);
String targetFilePath = targetFile.getPath(); String data = (request.getParameter("params") == null) ? "" : request.getParameter("params");
data = URLDecoder.decode(URLDecoder.decode(data, "utf-8"), "utf-8");
Gson gson = new Gson();
Map jsonObj = (Map)gson.fromJson(data, new TypeToken() { }
.getType()); String filter = getFilter(jsonObj);
try {
targetFile = new Util().copyFile(sourcePath, targetFilePath);
FileInputStream fis = new FileInputStream(targetFile); XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet hs1 = wb.getSheetAt();
XSSFRow row1 = hs1.createRow(); XSSFCell cell00 = row1.createCell();
XSSFCell cell01 = row1.createCell();
XSSFCell cell02 = row1.createCell();
XSSFCell cell03 = row1.createCell();
XSSFCell cell04 = row1.createCell();
XSSFCell cell05 = row1.createCell();
XSSFCell cell06 = row1.createCell();
XSSFCell cell07 = row1.createCell();
XSSFCell cell08 = row1.createCell();
XSSFCell cell09 = row1.createCell();
XSSFCell cell10 = row1.createCell();
XSSFCell cell11 = row1.createCell();
XSSFCell cell12 = row1.createCell();
XSSFCell cell13 = row1.createCell();
XSSFCell cell14 = row1.createCell();
XSSFCell cell15 = row1.createCell();
XSSFCell cell16 = row1.createCell();
XSSFCell cell17 = row1.createCell();
cell00.setCellValue("数据列名1");
cell01.setCellValue("数据列名2");
cell02.setCellValue("数据列名2");
cell03.setCellValue("数据列名4");
cell04.setCellValue("数据列名5");
cell05.setCellValue("数据列名6");
cell06.setCellValue("数据列名7");
cell07.setCellValue("数据列名8");
cell08.setCellValue("数据列名9");
cell09.setCellValue("数据列名10");
cell10.setCellValue("数据列名11");
cell11.setCellValue("数据列名12");
cell12.setCellValue("数据列名13");
cell13.setCellValue("数据列名14");
cell14.setCellValue("数据列名15");
cell15.setCellValue("数据列名16");
cell16.setCellValue("数据列名17");
cell17.setCellValue("数据列名18"); dcxx(fis, wb, filter); FileOutputStream out = new FileOutputStream(targetFile);
wb.write(out);
downloadFile(request, response, targetFileName, descPath + "\\" + targetFileName);
fis.close();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGet(request, response);
} public void downloadFile(HttpServletRequest request, HttpServletResponse response, String targetFileName, String path_filename) throws Exception
{
response.setContentType("text/html;charset=UTF-8");
BufferedInputStream in = null;
BufferedOutputStream out = null;
request.setCharacterEncoding("UTF-8");
try {
File f = new File(path_filename);
response.setContentType("application/x-excel");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + new String(targetFileName.getBytes("gbk"), "iso-8859-1"));
response.setHeader("Content-Length", String.valueOf(f.length()));
in = new BufferedInputStream(new FileInputStream(f));
out = new BufferedOutputStream(response.getOutputStream());
byte[] data = new byte[];
int len = ;
while (- != (len = in.read(data, , data.length)))
out.write(data, , len);
}
catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
in.close();
}
if (out != null)
out.close();
}
} private String getFilter(Map<String, String> jsonObj)
{
String res = "";
String xmmc = (String)jsonObj.get("xmmc");
String xmzt = (String)jsonObj.get("xmzt");
String xmlx = (String)jsonObj.get("xmlx");
String sheetId = (String)jsonObj.get("sheetId"); StringBuffer filter = new StringBuffer(); if ((xmmc != null) && (!"".equals(xmmc)) && (!xmmc.equals("--不限--"))) {
filter.append(" AND WYXMJC LIKE '%").append(xmmc).append("%' ");
}
if ((xmlx != null) && (!"".equals(xmlx)) && (!xmlx.equals("--不限--"))) {
filter.append(" AND XMSX LIKE '%").append(xmlx).append("%' ");
}
if ((xmzt != null) && (!"".equals(xmzt)) && (!xmzt.equals("--不限--"))) {
filter.append(" AND XMZT = '").append(xmzt).append("' ");
}
if ((sheetId != null) && (!"".equals(sheetId))) {
filter.append(" AND SHEETID IN ('").append(sheetId.replaceAll(",", "','")).append("') ");
}
res = filter.toString();
return res;
} private void dcxx(FileInputStream fis, XSSFWorkbook wb, String filter) throws SQLException {
StringBuffer sb = new StringBuffer();
sb.append(" SELECT *FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY X.WYXMBH DESC ) AS ROWNUMBER, X.* FROM SD_RSG_XMGL X"); sb.append(" WHERE 1=1");
sb.append(filter.toString());
sb.append(" ) A ORDER BY ROWNUMBER"); ResultSet rs = DbOper.executeQuery(sb.toString());
XSSFSheet hs1 = wb.getSheetAt();
int i = ;
while (rs.next()) {
XSSFRow row1 = hs1.createRow(i);
String WYXMMC = turn(rs.getString("WYXMMC"));
String WYXMJC = turn(rs.getString("WYXMJC"));
String WYXMBH = turn(rs.getString("WYXMBH"));
String XMFZR = turn(rs.getString("XMFZR"));
String JF = turn(rs.getString("JF"));
String JFXTBH = turn(rs.getString("JFXTBH"));
String JFXTMC = turn(rs.getString("JFXTMC"));
String YYS = turn(rs.getString("YYS"));
String YYSID = turn(rs.getString("YYSID"));
String ZS = turn(rs.getString("ZS"));
String ZSID = turn(rs.getString("ZSID"));
String XMZT = turn(rs.getString("XMZT"));
if ("".equals(XMZT))
XMZT = "未开始";
else if ("".equals(XMZT))
XMZT = "进行中";
else if ("".equals(XMZT)) {
XMZT = "已完成";
}
String XMSX = turn(rs.getString("XMSX"));
String XMRWZT = turn(rs.getString("XMRWZT"));
if ("".equals(XMRWZT))
XMRWZT = "未开始";
else if ("".equals(XMRWZT))
XMRWZT = "进行中";
else if ("".equals(XMRWZT)) {
XMRWZT = "已完成";
}
String XMSJZT = turn(rs.getString("XMSJZT"));
if ("".equals(XMSJZT))
XMSJZT = "未开始";
else if ("".equals(XMSJZT))
XMSJZT = "进行中";
else if ("".equals(XMSJZT)) {
XMSJZT = "已完成";
}
String XMKSSJ = turn(rs.getString("XMKSSJ"));
String XMJSSJ = turn(rs.getString("XMJSSJ"));
String XMFW = turn(rs.getString("XMFW"));
String XMSM = turn(rs.getString("XMSM"));
String XMBZ = turn(rs.getString("XMBZ"));
XSSFCell cell00 = row1.createCell();
XSSFCell cell01 = row1.createCell();
XSSFCell cell02 = row1.createCell();
XSSFCell cell03 = row1.createCell();
XSSFCell cell04 = row1.createCell();
XSSFCell cell05 = row1.createCell();
XSSFCell cell06 = row1.createCell();
XSSFCell cell07 = row1.createCell();
XSSFCell cell08 = row1.createCell();
XSSFCell cell09 = row1.createCell();
XSSFCell cell10 = row1.createCell();
XSSFCell cell11 = row1.createCell();
XSSFCell cell12 = row1.createCell();
XSSFCell cell13 = row1.createCell();
XSSFCell cell14 = row1.createCell();
XSSFCell cell15 = row1.createCell();
XSSFCell cell16 = row1.createCell();
XSSFCell cell17 = row1.createCell();
cell00.setCellValue(WYXMMC);
cell01.setCellValue(WYXMJC);
cell02.setCellValue(WYXMBH);
cell03.setCellValue(XMFZR);
cell04.setCellValue(JF);
cell05.setCellValue(JFXTBH);
cell06.setCellValue(JFXTMC);
cell07.setCellValue(YYS);
cell08.setCellValue(ZS);
cell09.setCellValue(XMZT);
cell10.setCellValue(XMSX);
cell11.setCellValue(XMRWZT);
cell12.setCellValue(XMSJZT);
cell13.setCellValue(XMKSSJ);
cell14.setCellValue(XMJSSJ);
cell15.setCellValue(XMFW);
cell16.setCellValue(XMSM);
cell17.setCellValue(XMBZ);
++i;
}
} public static String turn(String s) {
if (("NULL".equals(s)) || ("".equals(s)) || (s == null) || ("null".equals(s)) || ("无".equals(s))) {
s = " ";
}
return s;
}
}
批量或者选择导出datagrid列表数据到表格的更多相关文章
- PHP导出3w条数据成表格
亲测有效,三万条数据秒秒钟导出 先进行数据表插入数据 ini_set('memory_limit','1024M'); //设置程序运行的内存 ini_set('max_execution_time' ...
- JQuery EasyUI DataGrid列表所见所得随意导出excel
1.抽取DataGrid列表数据 function ExportNormal(strXlsName, exportGrid, postUrl, hiddenColumns) { /// <sum ...
- 批量快速的导入导出Oracle的数据(spool缓冲池、java实现)
1. Java代码实现思路 BufferedWriter writefile = new BufferedWriter(new FileWriter(file)); writefile.write( ...
- EasyUI datagrid-export 将datagrid的数据导出至Excel-解决科学计数法
通过EasyUI datagrid-export.js 将datagrid的数据导出至Excel的时候,如果有类似身份证一样很长的数字,需要在后台返回数据的时候在数字前增加一个 “ ”,将数字转为字符 ...
- 第五章 Odoo 12开发之导入、导出以及模块数据
大多数Odoo 模块的定义,如用户界面和安全规则,实际是存储在对应数据表中的数据记录.模块中的 XML 和 CSV 文件不是 Odoo 应用运行时使用,而是载入数据表的手段.正是因为这个原因,Odoo ...
- DataGrid列表信息保存为EXCEL到指定的路径 ---------继续DataGrid在WPF的数据绑定
一.许多WPF或者Winform程序在保存数据时,会以EXCEL或者文本的形式展现可视化界面的数据,本文就简单介绍下将DataGrid列表信息保存为EXCEL到指定的路径下,例子很简单,用的着的直接用 ...
- 用php导入10W条+ 级别的csv大文件数据到mysql。导出10W+级别数据到csv文件
转自:http://blog.csdn.net/think2me/article/details/12999907 1. 说说csv 和 Excel 这两者都是我们平时导出或者导入数据一般用到的载体. ...
- 个人永久性免费-Excel催化剂功能第77波-专业图表制作辅助之批量维护序列点颜色及数据标签
2018年最后一天工作日完成第77波,7是代表完美,2个7,双重的完美,Excel催化剂的2018年从始至终共77波都充满着完美接近极致的功能体验.感谢各位一路相随,陪伴成长.最后一波,再次让数据分析 ...
- 个人永久性免费-Excel催化剂功能第50波-批量打印、导出PDF、双面打印功能
在倡导无纸化办公的今天,是否打印是一个碍眼的功能呢,某些时候的确是,但对于数据的留存,在现在鼓吹区块链技术的今天,仍然不失它的核心价值,数据报表.单据打印出来留存,仍然是一种不可或缺的数据存档和防篡改 ...
随机推荐
- 洛谷P2827 蚯蚓
传送门 pts85/90(90应该是个意外,第一次交是90之后都是85了): 优先队列模拟题意 #include<iostream> #include<cstdio> #inc ...
- Leetcode151. Reverse Words in a String翻转字符串里的单词
给定一个字符串,逐个翻转字符串中的每个单词. 示例: 输入: "the sky is blue", 输出: "blue is sky the". 说明: 无空格 ...
- UI2Code智能生成Flutter代码--整体设计篇
摘要: UI2CODE项目是闲鱼技术团队研发的一款通过机器视觉理解+AI人工智能将UI视觉图片转化为端侧代码的工具. 背景: 随着移动互联网时代的到来,人类的科学技术突飞猛进.然而软件工程师们依旧需要 ...
- LUOGU P2441 角色属性树
题目描述 绪萌同人社是一个有趣的组织,该组织结构是一个树形结构.有一个社长,直接下属一些副社长.每个副社长又直接下属一些部长--. 每个成员都有一个萌点的属性,萌点属性是由一些质数的萌元素乘积构成(例 ...
- SSM-6nginx Linux下的安装
1.下载nginx: 官方网站: http://nginx.org/download/ 2. 要求的安装环境 yum install gcc-c++ yum -y install pcre-devel ...
- writing-mode,文字竖直书写,字符之间距离,单词之间距离
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- H5C3--属性选择器、兄弟选择器、伪类选择器
属性选择器 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- g++编译多个源原文件和头文件(转载)
(头文件名是:Sales_item.hpp 源文件名:7-31.cpp和Sales_item.cpp)(点击我下载源文件) 方法1: 我用的命令是:g++ -o 7-31 7-31.cpp Sales ...
- android 数据库存取图片
Android数据库中存取图片通常使用两种方式,一种是保存图片所在路径,二是将图片以二进制的形式存储(sqlite3支持BLOB数据类型).对于两种方法的使用,好像第二种方法不如第一种方法更受程序员欢 ...
- 如何在TypeScript中使用JS类库
使用流程 1.首先要清除类库是什么类型,不同的类库有不同的使用方式 2.寻找声明文件 JS类库一般有三类:全局类库.模块类库.UMD库.例如,jQuery是一种UMD库,既可以通过全局方式来引用,也可 ...