java、jsp导出excel功能备份

问题踩坑:

ajax请求不能下载文件

必须这样:

<a href="/media">点击下载Excel</a>

或者

location.href = ‘/media‘;

js取表单数据:

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #788d93 }
span.s1 { color: #c39900 }
span.s2 { color: #2fafa9 }

var selectbatch = $("selectbatch").value

java代码:

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #788d93 }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; min-height: 23.0px }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #6a8188 }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #de1f00 }
p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #788d93; min-height: 23.0px }
p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #2fafa9 }
p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #a4b0b1 }
p.p8 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #c39900 }
span.s1 { color: #c39900 }
span.s2 { color: #de1f00 }
span.s3 { color: #6a8188 }
span.s4 { text-decoration: underline }
span.s5 { color: #788d93 }
span.s6 { color: #2fafa9 }
span.Apple-tab-span { white-space: pre }

package com.action;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.IHttpAction;

import com.sql.ExchangeSql;

import jxl.Workbook;

import jxl.write.Label;

import jxl.write.NumberFormats;

import jxl.write.WritableCell;

import jxl.write.WritableCellFormat;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import jxl.write.WriteException;

import jxl.write.biff.RowsExceededException;

/**

* @FileName:     ExchangeSelectAction.java

* @Description:

* @Author  :     xingchong

* @CreateTime:   Nov 15, 2018 8:42:45 PM

* @Copyright:    Copyright (c) 2017

* @Version:      1.0

*/

public class ExchangeExportAction implements IHttpAction {

private ExchangeSql exchangeSql = new ExchangeSql();

private Logger logger = Logger.getLogger(ExchangeExportAction.class);

/* (non-Javadoc)

* @see com.gm.console.IHttpAction#action(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

*/

@Override

public void action(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

int batch = Integer.parseInt(request.getParameter("selectbatch"));

logger.info("selectbatch = "+ batch);

try {

InputStream is = this.exportExchangeCode(batch);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");

String fileName = "batch_"+batch+"_"+sdf.format(new Date()) + ".xls";

response.setContentType("applicaiton/x-download");

response.addHeader("Content-Disposition", "attachment;filename="+fileName);

ServletOutputStream out=response.getOutputStream();

int len=0;

byte[] buffer=new byte[1024];

while((len=is.read(buffer))>0){

out.write(buffer,0,len);

}

is.close();

out.close();

// response.getWriter().write("<span>export excel ok...</span>");

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 获取兑换码html信息

*

* @param batch

* @return

* @throws Exception

*/

private InputStream exportExchangeCode(int batch) throws Exception {

List<List<String>> dataList = null;

if (batch > 0) {

dataList = this.exchangeSql.getExchangeData(batch, Integer.MAX_VALUE);

} else {

dataList = this.exchangeSql.getSimpleExchangeData(Integer.MAX_VALUE);

}

List<String> titleList = dataList.get(0);

dataList.remove(0);

InputStream is = null;

try {

ByteArrayOutputStream os = new ByteArrayOutputStream();

WritableWorkbook wwb = Workbook.createWorkbook(os);

int step = 50000;

int size = dataList.size();

int sheetIndex = 0;

for(int start = 0; start < size; start+=step){

int end = start + step;

end = end > size? size:end;

List<List<String>> dataListSub = new ArrayList<List<String>>();

dataListSub.add(titleList);

dataListSub.addAll(dataList.subList(start, end));

this.writeDataToSheet(wwb, dataListSub, sheetIndex++);

}

wwb.write();

wwb.close();

is = new ByteArrayInputStream(os.toByteArray());

} catch (IOException e) {

e.printStackTrace();

} catch (WriteException e) {

e.printStackTrace();

}

return is;

}

/**

* 写一页excel

* @param wwb

* @param dataList

* @param sheetIndex

* @throws RowsExceededException

* @throws WriteException

*/

private void writeDataToSheet(WritableWorkbook wwb, List<List<String>> dataList, int sheetIndex)

throws RowsExceededException, WriteException {

WritableSheet sheet = wwb.createSheet("data_"+sheetIndex, sheetIndex);

WritableCellFormat wcf = new WritableCellFormat(NumberFormats.TEXT);

for (int r = 0; r < dataList.size(); r++) {

List<String> list = dataList.get(r);

for (int c = 0; c < list.size(); c++) {

String string = list.get(c);

WritableCell writableCell = new Label(c, r, string, wcf);

sheet.addCell(writableCell);

}

}

}

}

jsp代码:

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #c39900 }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #2fafa9 }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #788d93 }
p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; min-height: 23.0px }
p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #788d93; min-height: 23.0px }
p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #a4b0b1 }
p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 20.0px Consolas; color: #6a8188 }
span.s1 { color: #788d93 }
span.s2 { color: #000000 }
span.s3 { color: #4e9192 }
span.s4 { color: #2fafa9 }
span.s5 { color: #c39900 }
span.s6 { color: #a4b0b1 }
span.s7 { color: #0f446a }
span.s8 { text-decoration: underline }
span.Apple-tab-span { white-space: pre }

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

<HEAD>

<title>My JSP ‘buildinglist.jsp‘ starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="Content-type" content="text/html charset=utf-8">

<meta http-equiv="expires" content="0">

<LINK href="/console/style/default.css" type=text/css rel=stylesheet>

<LINK href="/console/style/style.css" type=text/css rel=stylesheet>

<LINK href="/console/style/style3.css" type=text/css rel=stylesheet>

<LINK href="/console/style/tab.webfx.css" type=text/css rel=StyleSheet>

<link href="/console/style/calendar.css" type=text/css rel=StyleSheet>

<script type="text/javascript" src="/console/js/prototype.js"></script>

<script type="text/javascript" src="/console/js/tools.js"></script>

<script type="text/javascript" src="/console/js/calendar.js"

charset="utf-8"></script>

<script type="text/javascript" src="/console/js/swfobject.js"

charset="utf-8"></script>

<script type="text/javascript">

function btnAjax() {

$("resultTable").innerHTML = "<span style=\"color: red;\">正在紧张生成中...请勿重复点击...</span>";

var myAjax = new Ajax.Request("customRequest.htm?catalog=exchange_add",

{

method : ‘post‘,

parameters : Form.serialize(‘Form1‘),

onComplete : function(x) { //提交成功回调

$("resultTable").innerHTML = x.responseText;

},

onError : function(x) { //提交失败回调

alert(x.statusText);

}

});

}

function selectAjax() {

$("selectTable").innerHTML = "<span style=\"color: green;\">正在查询中...请稍等...</span>";

var myAjax = new Ajax.Request(

"customRequest.htm?catalog=exchange_select", {

method : ‘post‘,

parameters : Form.serialize(‘Form1‘),

onComplete : function(x) { //提交成功回调

$("selectTable").innerHTML = x.responseText;

},

onError : function(x) { //提交失败回调

alert(x.statusText);

}

});

}

//export excel

function exportExcel() {

var selectbatch = $("selectbatch").value

location.href = ‘/customRequest.htm?catalog=exchange_export&selectbatch=‘+selectbatch;

}

</script>

<style type="text/css">

</style>

</HEAD>

<body>

<form id="Form1" name="Form1" method="post">

<div style="clear: both; height: 1px; overflow: hidden"></div>

<div width="98%" align="left" valign="left">

<font size="6px"> <br>兑换码生成 <br>

<br>

</font> ${result}

</div>

<table align="left" class=detail>

<tbody>

<tr>

<td class="list_td01">类型</td>

<td><select name="codeType">

<option value="1">单人单次</option>

<option value="0">多人共享单次</option>

</select> <span style="color: red;">1-单人单次 0-多人单次</span></td>

<td class=list_td01>生成数量</td>

<td class=list_td02><select name="num">

<option value="10">10</option>

<option value="100">100</option>

<option value="1000">1000</option>

<option value="10000">10000</option>

<option value="100000">100000</option>

</select></td>

</tr>

<tr align="left">

<td class="list_td01">开始时间</td>

<td class="list_td02"><input type="text"

onclick="displayCalendar(this,‘yyyy-mm-dd hh:ii:00‘,this,true);"

class="datetime" id="startTime" name="startTime"> <span

style="color: red;">(必须)</span></td>

<td class="list_td01">结束时间</td>

<td class="list_td02"><input type="text"

onclick="displayCalendar(this,‘yyyy-mm-dd hh:ii:00‘,this,true);"

class="datetime" id="endTime" name="endTime"> <span

style="color: red;">(必须)</span></td>

</tr>

<tr>

<td class=list_td01>奖励Json</td>

<td class=list_td02 colspan="3"><input type="text" size=90

name="rewardJson" id="rewardJson" value=""> <span

style="color: red;">(必须)</span></td>

</tr>

<tr>

<td class=list_td01>等级限制</td>

<td class=list_td02 colspan="1"><select name="level">

<option value="1">1级</option>

<option value="10">10级</option>

<option value="20">20级</option>

<option value="30">30级</option>

<option value="40">40级</option>

<option value="50">50级</option>

<option value="60">60级</option>

<option value="70">70级</option>

<option value="80">80级</option>

<option value="90">90级</option>

<option value="100">100级</option>

<option value="110">110级</option>

<option value="120">120级</option>

</select></td>

<td class=list_td01>生成批次</td>

<td class=list_td02 colspan="1"><input type="text" size=20

name="batch" id="batch" value="1"> <span

style="color: red;">(必须)</span></td>

</tr>

<tr>

<td align="left"><input type="button" class="butcom"

onclick="btnAjax()" value=" 生成提交 " /></td>

</tr>

</table>

<TABLE id="resultTable" align="left" class=common></TABLE>

<!-- select about -->

<table align="left" class=detail>

<tr>

<td class=list_td01>批次 </td>

<td class=list_td02><input type="text" size=10 name="selectbatch"

id="selectbatch" value="0"> <span

style="color: green;">(0表求所有批次)</span></td>

<td align="left"><input type="button" class="butcom"

onclick="selectAjax()" value="  查询  " /></td>

<td align="left"><input type="button" class="butcom"

onclick="exportExcel()" value="  导出excel  " /></td>

</tr>

</table>

<TABLE id="selectTable" align="left" class=common></TABLE>

</form>

</body>

</HTML>

原文地址:https://www.cnblogs.com/xingchong/p/10003243.html

时间: 2024-11-09 19:30:07

java、jsp导出excel功能备份的相关文章

重构:以Java POI 导出EXCEL为例

重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的了解重构的意义. 什么是重构? <重构:改善既有代码的设计>这本书中将重构以名词和动词形式进行解释,如下 重构(名词):对软件内部结构的一种调整,目的是在不改变软件可观察行为的前提下,提高其可理解性,降低其修改成本. 重构(动词):使用一系列重构的手法,在不改变软件可观察行为的前提下,调整其结构.

JSP导出Excel文件

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@ taglib uri="http://java.sun.c

扩展GridView导出Excel功能

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Runtime.CompilerServices;using System.Text;using System.Web;using System.Web.Compilation;using System.Web.UI;using System.Web.UI.WebControls; namespace Web{ 

Java中读取Excel功能实现_POI

这里使用apache的poi进行读取excel 1,新建javaproject 项目:TestExcel 2,导入包 包下载地址:http://poi.apache.org/download.html#POI-3.10-FINAL 百度网盘下载:http://pan.baidu.com/s/1i365mQT 导入根目录下.lib.ooxml-lib下的所有jar 4,操作读取excel import java.io.File; import java.io.IOException; import

flex 导出Excel功能实现

方法一: 1.Excel导出主要代码: try   {    var bytes: ByteArray = new ByteArray();    bytes.writeMultiByte(DataGridExporter.exportSCV(dataGrid),"cn-gb");    var fr:FileReference = new FileReference();    var date:Date = new Date();    var dateTime:String =

项目笔记:导出Excel功能分sheet页插入数据

导出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

Java POI 导出EXCEL经典实现 Java导出Excel弹出下载框

在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.而数据导出的格式一般是EXCEL或者PDF,我这里就用两篇文章分别给大家介绍下.(注意,我们这里说的数据导出可不是数据库中的数据导出!么误会啦^_^) 呵呵,首先我们来导出EXCEL格式的文件吧.现在主流的操作Excel文件的开源工具有很多,用得比较多的就是Apache的POI及JExcelAPI.这里我们用Apache POI!我们先去Apach

Java POI 导出EXCEL经典实现 Java导出Excel

转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.而数据导出的格式一般是EXCEL或者PDF,我这里就用两篇文章分别给大家介绍下.(注意,我们这里说的数据导出可不是数据库中的数据导出!么误会啦^_^) 呵呵,首先我们来导出EXCEL格式的文件吧.现在主流的操作Excel文件的开源工具有

[ExtJS5学习笔记]第三十四节 sencha extjs 5 grid表格之java后台导出excel

继上次使用js前端导出excel之后,还有一个主要大家比较关注的是后台实现导出excel,因为本人开发使用的java所以这里使用apache的开源项目poi进行后台excel的导出. 本文目录 本文目录 poi项目下载及加载 extjs前端导出设置 extjs后台对应的解决方案 创建excel工作簿 创建一个excel页签 生成excel样式并初始化 产生表格标题行build headers 构造数据行build rows poi项目下载及加载 POI项目是apache官网的一个开源项目,其主要