java jsp页面数据导入到excel中的问题
现在比如说数据库中有个学生表,现在吧表中的数据都查出来了放到jsp的表格中现在在jsp页面上设置一个按钮,点击按钮后就把表格中的数据导入到excel中了,用到的JXL技术...
现在比如说数据库中有个学生表 ,现在吧表中的数据都查出来了放到jsp的表格中
现在在jsp页面上设置一个按钮,点击按钮后就把表格中的数据导入到excel中了,
用到的JXL技术 ,有人能帮我 该如何做 最好具体到 js方法 本人新手 越详细越好 展开
现在在jsp页面上设置一个按钮,点击按钮后就把表格中的数据导入到excel中了,
用到的JXL技术 ,有人能帮我 该如何做 最好具体到 js方法 本人新手 越详细越好 展开
展开全部
一、列表页面的部分代码
[html] view plaincopy
<input type="button" class="cxBut2" value="导出数据" onclick="_export()"/>
[javascript] view plaincopy
function _export(){
document.location.href="download.jsp?czId=<%=czId%>&czNum=<%=czNum%>&beginIssue=<%=beginIssue%>&endIssue=<%=endIssue%>&pageNum=<%=pageNum%>&czName="+$("#czId").find("option:selected").text();
}
二、download.jsp
[html] view plaincopy
<%@ page contentType="application/vnd.ms-excel" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.zhcw.kaijiang.service.ExportExcal"%>
<%@page import="java.io.OutputStream"%>
<%
String czNum = request.getParameter("czNum");
String czId = request.getParameter("czId");
String czName = request.getParameter("czName");
String beginIssue = request.getParameter("beginIssue");
String endIssue = request.getParameter("endIssue");
String pageNum = request.getParameter("pageNum");
response.resetBuffer();
response.setHeader("Content-Disposition", "attachment;filename="+ new String(czName.getBytes("UTF-8"), "iso8859-1")+".xls");//指定下载的文件名
response.setContentType("application/vnd.ms-excel");
try{
ExportExcal exportExcal = new ExportExcal(beginIssue, czId, czName, czNum, endIssue, pageNum);
exportExcal.export(response.getOutputStream());
}catch(Exception ex){
ex.printStackTrace();
}
%>
3.这个页面主要负责接收上一个页面传来的数据,然后调用后台相应的导出方法来实现导出功能。
注意:这两行代码必须要写:
[html] view plaincopy
response.resetBuffer();<pre name="code" class="html">response.setContentType("application/vnd.ms-excel");</pre>
<pre></pre>
<p></p>
<pre></pre>
<p></p>
<p></p>
三、后台处理类<pre name="code" class="java">package com.zhcw.kaijiang.service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.List;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import com.common.PropertiesOperator;
import com.crawler.entity.KaiJiangInfo;
import com.zhcw.kaijiang.util.StringUtil;
/**
* 将查询出来的信息导出到excel中
*
* @author zhcw
*
*/
public class ExportExcal {
private String czNum = "";
private String czId = "";
private String czName = "";
private String beginIssue = "";
private String endIssue = "";
private String pageNum = "";
public ExportExcal(String beginIssue, String czId, String czName, String czNum, String endIssue, String pageNum) {
super();
this.beginIssue = (beginIssue == null ? "" : beginIssue);
this.endIssue = (endIssue == null ? "" : endIssue);
this.czId = (czId == null ? "" : czId);
this.czName = (czName == null ? "" : czName);
this.czNum = (czNum == null || czNum.trim().equals("") || czNum.trim().equals("null")) ? "30" : czNum;
this.pageNum = (pageNum == null || pageNum.trim().equals("") || pageNum.trim().equals("null")) ? "1" : pageNum;
}
public List<KaiJiangInfo> kaiJiangInfoList() {
KaijiangInfoService kaijiangInfoService = new KaijiangInfoService();
if (this.beginIssue!=null && this.beginIssue.trim().length() > 0) {
return kaijiangInfoService.getSplitPageByCzIdNew(Long.valueOf(this.czId), Integer.valueOf(pageNum), 400, this.beginIssue, this.endIssue);
} else {
return kaijiangInfoService.getSplitPageByCzIdNew(Long.valueOf(this.czId), Integer.valueOf(pageNum), Integer.parseInt(czNum));
}
}
public void export(OutputStream output) throws Exception {
PropertiesOperator propertiesOperator = new PropertiesOperator();
String cz_id_css_1 = propertiesOperator.getMessage("cz_id_css_1") == null ? "" : propertiesOperator.getMessage("cz_id_css_1").trim();
String cz_id_css_2 = propertiesOperator.getMessage("cz_id_css_2") == null ? "" : propertiesOperator.getMessage("cz_id_css_2").trim();
String cz_id_css_3 = propertiesOperator.getMessage("cz_id_css_3") == null ? "" : propertiesOperator.getMessage("cz_id_css_3").trim();
String cz_id_css_4 = propertiesOperator.getMessage("cz_id_css_4") == null ? "" : propertiesOperator.getMessage("cz_id_css_4").trim();
String cz_id_css_5 = propertiesOperator.getMessage("cz_id_css_5") == null ? "" : propertiesOperator.getMessage("cz_id_css_5").trim();
if (StringUtil.contains(this.czId, cz_id_css_1)) {
this.exportSSQExcel(output);
} else if (StringUtil.contains(this.czId, cz_id_css_2)) {
this.export3DExcel(output);
}
}
private void exportExcel(String type,OutputStream output) throws Exception {
WritableWorkbook workbook = Workbook.createWorkbook(output);// 创建工作薄
WritableSheet sheet = workbook.createSheet(this.czName, 0);// 创建第一个工作表,name:工作表名称
//设置列宽度
sheet.setColumnView(0,7);
sheet.setColumnView(1,15);
sheet.setColumnView(2,13);
sheet.setColumnView(3,25);
sheet.setColumnView(4,15);
sheet.setColumnView(6,15);
sheet.setColumnView(8,15);
sheet.setColumnView(10,15);
sheet.setColumnView(11,15);
WritableCellFormat format = new WritableCellFormat();
format.setAlignment(Alignment.CENTRE);
format.setBorder(Border.ALL, BorderLineStyle.THIN);
int row = 0;
//合并标题行
sheet.mergeCells(0, row, 11, row);
// 合并
sheet.mergeCells(0, row + 1, 0, row + 2);
sheet.mergeCells(1, row + 1, 1, row + 2);
sheet.mergeCells(2, row + 1, 2, row + 2);
sheet.mergeCells(3, row + 1, 3, row + 2);
sheet.mergeCells(4, row + 1, 4, row + 2);
sheet.mergeCells(11, row + 1, 11, row + 2);
sheet.mergeCells(5, row + 1, 6, row + 1);
sheet.mergeCells(7, row + 1, 8, row + 1);
sheet.mergeCells(9, row + 1, 10, row + 1);
Label label = null;// 用于写入文本内容到工作表中去
// 开始写入第一行,即标题栏
if(this.beginIssue.length()>0){
label = new Label(0, row, czName+"从"+this.beginIssue+" 到 "+this.endIssue+"期的中奖信息",format);// 参数依次代表列数、行数、内容
sheet.addCell(label);// 写入单元格
}else{
label = new Label(0, row, czName+"的前 "+this.czNum+" 期的中奖信息",format);// 参数依次代表列数、行数、内容
sheet.addCell(label);// 写入单元格
}
//第二行写表头
label = new Label(10, num, df.format(kaiJiangInfo.getBonusThree()),format);// 参数依次代表列数、行数、内容
sheet.addCell(label);// 写入单元格
//只有双色球有奖池金额
if(type!=null && type.trim().equals("ssq")){
label = new Label(11, num, df.format(kaiJiangInfo.getBonusPool()),format);// 参数依次代表列数、行数、内容
sheet.addCell(label);// 写入单元格
}
}
workbook.write();
workbook.close();
}
private void exportSSQExcel(OutputStream output) throws Exception {
this.exportExcel("ssq", output);
}
private void export3DExcel(OutputStream output) throws Exception {
this.exportExcel("3d", output);
}
}
</pre><br>
<br>
<div style="padding-top:20px">
</div>
[html] view plaincopy
<input type="button" class="cxBut2" value="导出数据" onclick="_export()"/>
[javascript] view plaincopy
function _export(){
document.location.href="download.jsp?czId=<%=czId%>&czNum=<%=czNum%>&beginIssue=<%=beginIssue%>&endIssue=<%=endIssue%>&pageNum=<%=pageNum%>&czName="+$("#czId").find("option:selected").text();
}
二、download.jsp
[html] view plaincopy
<%@ page contentType="application/vnd.ms-excel" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.zhcw.kaijiang.service.ExportExcal"%>
<%@page import="java.io.OutputStream"%>
<%
String czNum = request.getParameter("czNum");
String czId = request.getParameter("czId");
String czName = request.getParameter("czName");
String beginIssue = request.getParameter("beginIssue");
String endIssue = request.getParameter("endIssue");
String pageNum = request.getParameter("pageNum");
response.resetBuffer();
response.setHeader("Content-Disposition", "attachment;filename="+ new String(czName.getBytes("UTF-8"), "iso8859-1")+".xls");//指定下载的文件名
response.setContentType("application/vnd.ms-excel");
try{
ExportExcal exportExcal = new ExportExcal(beginIssue, czId, czName, czNum, endIssue, pageNum);
exportExcal.export(response.getOutputStream());
}catch(Exception ex){
ex.printStackTrace();
}
%>
3.这个页面主要负责接收上一个页面传来的数据,然后调用后台相应的导出方法来实现导出功能。
注意:这两行代码必须要写:
[html] view plaincopy
response.resetBuffer();<pre name="code" class="html">response.setContentType("application/vnd.ms-excel");</pre>
<pre></pre>
<p></p>
<pre></pre>
<p></p>
<p></p>
三、后台处理类<pre name="code" class="java">package com.zhcw.kaijiang.service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.List;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import com.common.PropertiesOperator;
import com.crawler.entity.KaiJiangInfo;
import com.zhcw.kaijiang.util.StringUtil;
/**
* 将查询出来的信息导出到excel中
*
* @author zhcw
*
*/
public class ExportExcal {
private String czNum = "";
private String czId = "";
private String czName = "";
private String beginIssue = "";
private String endIssue = "";
private String pageNum = "";
public ExportExcal(String beginIssue, String czId, String czName, String czNum, String endIssue, String pageNum) {
super();
this.beginIssue = (beginIssue == null ? "" : beginIssue);
this.endIssue = (endIssue == null ? "" : endIssue);
this.czId = (czId == null ? "" : czId);
this.czName = (czName == null ? "" : czName);
this.czNum = (czNum == null || czNum.trim().equals("") || czNum.trim().equals("null")) ? "30" : czNum;
this.pageNum = (pageNum == null || pageNum.trim().equals("") || pageNum.trim().equals("null")) ? "1" : pageNum;
}
public List<KaiJiangInfo> kaiJiangInfoList() {
KaijiangInfoService kaijiangInfoService = new KaijiangInfoService();
if (this.beginIssue!=null && this.beginIssue.trim().length() > 0) {
return kaijiangInfoService.getSplitPageByCzIdNew(Long.valueOf(this.czId), Integer.valueOf(pageNum), 400, this.beginIssue, this.endIssue);
} else {
return kaijiangInfoService.getSplitPageByCzIdNew(Long.valueOf(this.czId), Integer.valueOf(pageNum), Integer.parseInt(czNum));
}
}
public void export(OutputStream output) throws Exception {
PropertiesOperator propertiesOperator = new PropertiesOperator();
String cz_id_css_1 = propertiesOperator.getMessage("cz_id_css_1") == null ? "" : propertiesOperator.getMessage("cz_id_css_1").trim();
String cz_id_css_2 = propertiesOperator.getMessage("cz_id_css_2") == null ? "" : propertiesOperator.getMessage("cz_id_css_2").trim();
String cz_id_css_3 = propertiesOperator.getMessage("cz_id_css_3") == null ? "" : propertiesOperator.getMessage("cz_id_css_3").trim();
String cz_id_css_4 = propertiesOperator.getMessage("cz_id_css_4") == null ? "" : propertiesOperator.getMessage("cz_id_css_4").trim();
String cz_id_css_5 = propertiesOperator.getMessage("cz_id_css_5") == null ? "" : propertiesOperator.getMessage("cz_id_css_5").trim();
if (StringUtil.contains(this.czId, cz_id_css_1)) {
this.exportSSQExcel(output);
} else if (StringUtil.contains(this.czId, cz_id_css_2)) {
this.export3DExcel(output);
}
}
private void exportExcel(String type,OutputStream output) throws Exception {
WritableWorkbook workbook = Workbook.createWorkbook(output);// 创建工作薄
WritableSheet sheet = workbook.createSheet(this.czName, 0);// 创建第一个工作表,name:工作表名称
//设置列宽度
sheet.setColumnView(0,7);
sheet.setColumnView(1,15);
sheet.setColumnView(2,13);
sheet.setColumnView(3,25);
sheet.setColumnView(4,15);
sheet.setColumnView(6,15);
sheet.setColumnView(8,15);
sheet.setColumnView(10,15);
sheet.setColumnView(11,15);
WritableCellFormat format = new WritableCellFormat();
format.setAlignment(Alignment.CENTRE);
format.setBorder(Border.ALL, BorderLineStyle.THIN);
int row = 0;
//合并标题行
sheet.mergeCells(0, row, 11, row);
// 合并
sheet.mergeCells(0, row + 1, 0, row + 2);
sheet.mergeCells(1, row + 1, 1, row + 2);
sheet.mergeCells(2, row + 1, 2, row + 2);
sheet.mergeCells(3, row + 1, 3, row + 2);
sheet.mergeCells(4, row + 1, 4, row + 2);
sheet.mergeCells(11, row + 1, 11, row + 2);
sheet.mergeCells(5, row + 1, 6, row + 1);
sheet.mergeCells(7, row + 1, 8, row + 1);
sheet.mergeCells(9, row + 1, 10, row + 1);
Label label = null;// 用于写入文本内容到工作表中去
// 开始写入第一行,即标题栏
if(this.beginIssue.length()>0){
label = new Label(0, row, czName+"从"+this.beginIssue+" 到 "+this.endIssue+"期的中奖信息",format);// 参数依次代表列数、行数、内容
sheet.addCell(label);// 写入单元格
}else{
label = new Label(0, row, czName+"的前 "+this.czNum+" 期的中奖信息",format);// 参数依次代表列数、行数、内容
sheet.addCell(label);// 写入单元格
}
//第二行写表头
label = new Label(10, num, df.format(kaiJiangInfo.getBonusThree()),format);// 参数依次代表列数、行数、内容
sheet.addCell(label);// 写入单元格
//只有双色球有奖池金额
if(type!=null && type.trim().equals("ssq")){
label = new Label(11, num, df.format(kaiJiangInfo.getBonusPool()),format);// 参数依次代表列数、行数、内容
sheet.addCell(label);// 写入单元格
}
}
workbook.write();
workbook.close();
}
private void exportSSQExcel(OutputStream output) throws Exception {
this.exportExcel("ssq", output);
}
private void export3DExcel(OutputStream output) throws Exception {
this.exportExcel("3d", output);
}
}
</pre><br>
<br>
<div style="padding-top:20px">
</div>
展开全部
function AllAreaExcel(){
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var sel=document.body.createTextRange();
sel.moveToElementText(export);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;
}
<input type="button" value="表格导入excel" onclick="AllAreaExcel();">
<table id="export"><tr><td></td></tr></table>
测试的时候把ie的安全中心设置下~允许脚本运行和加载
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var sel=document.body.createTextRange();
sel.moveToElementText(export);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;
}
<input type="button" value="表格导入excel" onclick="AllAreaExcel();">
<table id="export"><tr><td></td></tr></table>
测试的时候把ie的安全中心设置下~允许脚本运行和加载
追问
谢谢 有一个表格 然后下面设置个按钮 点击后 直接调用上面js代码就可以实现吗?不用牵扯到java类中的问题吗?
追答
不用~这纯粹的是脚本运行,和后台程序不搭边~
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
----学生表----------
package com.util;
public class Employee {
String empNo;
String empName;
String sex;
public String getEmpNo() {
return empNo;
}
public void setEmpNo(String empNo) {
this.empNo = empNo;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
-------------------excel 导出------
package com.util;
import java.io.File;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelTools {
public String exportExcle(String ttype, List<Employee> list, String url) {
java.util.Random rd = new java.util.Random();
String excelFile = ""+rd.nextLong();
String upload = excelFile + ".xls";
String exclepath = url + upload;
try {
// 打开文件
WritableWorkbook book = Workbook
.createWorkbook(new File(exclepath));
// 生成名为“第一页”的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet(ttype, 0);
// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
// 以及单元格内容为test
Label lb = new Label(0,0,"学生号");
sheet.addCell(lb);
lb = new Label(0,1,"姓名");
sheet.addCell(lb);
lb = new Label(0,2,"性别");
sheet.addCell(lb);
int num = 1;
//集合写入excel
for (Employee ep : list){
lb = new Label(num,0,ep.getEmpNo());
sheet.addCell(lb);
lb = new Label(num,1,ep.getEmpName());
sheet.addCell(lb);
lb = new Label(num,2,ep.getSex());
sheet.addCell(lb);
num++;
}
book.write();
book.close();
} catch (Exception e) {
System.out.println(e);
}
return excelFile; //把excelFile放入session就可以到处excel了
}
}
package com.util;
public class Employee {
String empNo;
String empName;
String sex;
public String getEmpNo() {
return empNo;
}
public void setEmpNo(String empNo) {
this.empNo = empNo;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
-------------------excel 导出------
package com.util;
import java.io.File;
import java.util.List;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class ExcelTools {
public String exportExcle(String ttype, List<Employee> list, String url) {
java.util.Random rd = new java.util.Random();
String excelFile = ""+rd.nextLong();
String upload = excelFile + ".xls";
String exclepath = url + upload;
try {
// 打开文件
WritableWorkbook book = Workbook
.createWorkbook(new File(exclepath));
// 生成名为“第一页”的工作表,参数0表示这是第一页
WritableSheet sheet = book.createSheet(ttype, 0);
// 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
// 以及单元格内容为test
Label lb = new Label(0,0,"学生号");
sheet.addCell(lb);
lb = new Label(0,1,"姓名");
sheet.addCell(lb);
lb = new Label(0,2,"性别");
sheet.addCell(lb);
int num = 1;
//集合写入excel
for (Employee ep : list){
lb = new Label(num,0,ep.getEmpNo());
sheet.addCell(lb);
lb = new Label(num,1,ep.getEmpName());
sheet.addCell(lb);
lb = new Label(num,2,ep.getSex());
sheet.addCell(lb);
num++;
}
book.write();
book.close();
} catch (Exception e) {
System.out.println(e);
}
return excelFile; //把excelFile放入session就可以到处excel了
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |