我这里做的比较简单,就点击导出按钮,到后台写一个导出地址
jsp代码如下
<%@ page import="mc.global.demo.Export" %>
<%@ page import="java.util.List" %>
<%@ page import="net.sf.json.JSONArray" %>
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2015/7/28
Time: 11:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String data = (String)request.getAttribute("data") ;
%>
<html>
<head>
<title>上传Excel</title>
<%@ include file="/global/commonPage/includeHead/head.jsp" %>
<script type="text/javascript" src="<%=baseUrl%>/global/scripts/jquery/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="<%=baseUrl%>/global/scripts/jsrender.js"></script>
</head>
<script>
function exportClik(){
var file=document.getElementById("fileName").value;
fileForm.action = "<%=baseUrl%>/upImageFile.do?method=exportExcel";
fileForm.submit();
}
</script>
<body>
<form action="" enctype="multipart/form-data" id="fileForm" name="fileForm" method="post" >
<div id="filePanel">
<table >
<tr>
<td width="100px" style="font-size:10px;text-align:center"></td>
<td><input type="file" id="fileName" name="fileName" /></td>
<td style="width:80px"><input type="button" value="导入" onclick="javascript:improtBudgetVersion()" id="import1"/></td>
<td><input type="button" value="导出" onclick="exportClik()" id="export" style="right: auto"></td>
</tr>
</table>
</div>
</form>
</body>
</html>
java 代码,需要jxl及io流jar包
import com.brainlong.framework.struts.BaseDispatchAction;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.write.*;
import jxl.write.biff.RowsExceededException;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class UpImageFileAction extends BaseDispatchAction {
ActionForward exportExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)IOException{
String outputFileName = ;
String title = ;
List<String> list =ArrayList<String>();
list.add();
list.add();
list.add();
list.add();
list.add();
WritableWorkbook testBook;
{
String path=;
OutputStream os = FileOutputStream(path);
testBook = Workbook.(os);
WritableSheet ws=testBook.createSheet(outputFileName,);
WritableFont font1 =WritableFont(WritableFont.(), ,WritableFont.);
WritableFont font2 =WritableFont(WritableFont.(), ,WritableFont.);
WritableCellFormat wcf = WritableCellFormat(font1);
WritableCellFormat wcf2 = WritableCellFormat(font2);
WritableCellFormat wcf3 = WritableCellFormat(font2);wcf.setBackground(Colour.);
wcf.setAlignment(Alignment.); wcf3.setAlignment(Alignment.); wcf.setAlignment(jxl.format.Alignment.);
(list!=&&list.size()>){
(i=;i<list.size();i++){
ws.addCell(Label(i,,list.get(i),wcf ));
}
}
ws.addCell(Label(,,));
ws.addCell(Label(,,));
ws.addCell(Label(,,));
ws.addCell(Label(,,));
testBook.write();
testBook.close();
os.flush();
os.close();
} (FileUploadException e) {
e.printStackTrace();
} (RowsExceededException e) {
e.printStackTrace();
} (WriteException e) {
e.printStackTrace();
}
;
}
}
原文:http://my.oschina.net/u/2415525/blog/486799