标签:
jsp 页面:
导出按钮:
<form id="excel" name="exportForm" method="post" action="" style="display: inline-block; height: 18px;"><!-- padding-left: 15px; -->
<input type="hidden" name="excelText" id="excelText"/>
<p align="center"><input type="button" class="btn btn-xs" name="exportBtn" onclick="excel()" value="导出"/></p>
</form>
导出按钮js:
$("#excel").bind("click", function() {
if(confirm("是否确认导出?")){
window.location.href="${contextPath}/activeCode/toExcel";
}else{
alert("导出失败!");
}
});
在java后台的代码:
//导出
@RequestMapping(value="/toExcel")
public ModelAndView exportExcel(){
ModelAndView mv = this.getModelAndView();
PageData pd = new PageData();
pd = this.getPageData();
try{
Map<String,Object> dataMap = new HashMap<String,Object>();
List<String> titles = new ArrayList<String>();
//titles.add("ID");
titles.add("激活码"); //1
titles.add("类别"); //2
titles.add("状态"); //3
titles.add("创建时间"); //4
dataMap.put("titles", titles);
@SuppressWarnings("unchecked") //我现在这里取值有一些问题,所以做了一直到for循环的一些设定
List<PageData> codeList = activeCodeservice.getActiveCodeList(pd);
int count = codeList.size();
String Slist=JSON.toJSONString(codeList);
Slist = Slist.substring(1, Slist.length()-1); //去掉第一位和最后一位
//System.out.println("String 类型 Slist的值: "+Slist);
String[] arr = Slist.split(",");
List<String> list = java.util.Arrays.asList(arr);
List<PageData> varList = new ArrayList<PageData>();
//System.out.println(" for list的长度: "+list.size()); //15
for(int j=0;j<count;j++){
PageData vpd = new PageData();
int i=5; //数据有几个字段名
//vpd.put("var1", list.get(j*i).substring(1, list.get(j*i).length()));
vpd.put("var1", list.get(j*i+1).substring(1, list.get(j*i+1).length()-1));
vpd.put("var2", list.get(j*i+2).substring(1, list.get(j*i+2).length()-1));
vpd.put("var3", list.get(j*i+3).substring(1, list.get(j*i+3).length()-1));
vpd.put("var4", list.get(j*i+4).substring(0, list.get(j*i+4).length()-1));
varList.add(vpd);
//System.out.println("varList "+varList);
}
dataMap.put("varList", varList);
System.out.println("dataMap "+dataMap);
ObjectExcelView erv = new ObjectExcelView(); //执行excel操作
mv = new ModelAndView(erv,dataMap);
//System.out.println("mv "+mv);
} catch(Exception e){
e.printStackTrace();
}
return mv;
}
当然了,必要的类和包还是需要的,
这是对导出都excel表格进行操作的类库文件:
@SuppressWarnings("deprecation")
public class ObjectExcelView extends AbstractExcelView{
@Override
protected void buildExcelDocument(Map<String, Object> model,
HSSFWorkbook workbook, HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
Date date = new Date();
String filename = Tools.date2Str(date, "yyyyMMddHHmmss");
HSSFSheet sheet;
HSSFCell cell;
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename="+filename+".xls");
sheet = workbook.createSheet("sheet1");
List<String> titles = (List<String>) model.get("titles");
int len = titles.size();
HSSFCellStyle headerStyle = workbook.createCellStyle(); //标题样式
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
HSSFFont headerFont = workbook.createFont(); //标题字体
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
headerFont.setFontHeightInPoints((short)11);
headerStyle.setFont(headerFont);
short width = 20,height=25*20;
sheet.setDefaultColumnWidth(width);
for(int i=0; i<len; i++){ //设置标题
String title = titles.get(i);
cell = getCell(sheet, 0, i);
cell.setCellStyle(headerStyle);
setText(cell,title);
}
sheet.getRow(0).setHeight(height);
HSSFCellStyle contentStyle = workbook.createCellStyle(); //内容样式
contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//List<PageData> varList = (List<PageData>) model.get("varList");
List<PageData> varList = (List<PageData>) model.get("varList");
int varCount = varList.size();
for(int i=0; i<varCount; i++){
PageData vpd = varList.get(i);
for(int j=0;j<len;j++){
String varstr = vpd.getString("var"+(j+1)) != null ? vpd.getString("var"+(j+1)) : "";
cell = getCell(sheet, i+1, j);
cell.setCellStyle(contentStyle);
setText(cell,varstr);
}
}
}
这个pageData,是对数据进行导用的,将渠道的数据通过pageData连接objectExcelView类进行导出操作
public class PageData extends HashMap implements Map{
private static final long serialVersionUID = 1L;
Map map = null;
HttpServletRequest request;
public PageData(HttpServletRequest request){
this.request = request;
Map properties = request.getParameterMap();
Map returnMap = new HashMap();
Iterator entries = properties.entrySet().iterator();
Map.Entry entry;
String name = "";
String value = "";
while (entries.hasNext()) {
entry = (Map.Entry) entries.next();
name = (String) entry.getKey();
Object valueObj = entry.getValue();
if(null == valueObj){
value = "";
}else if(valueObj instanceof String[]){
String[] values = (String[])valueObj;
for(int i=0;i<values.length;i++){
value = values[i] + ",";
}
value = value.substring(0, value.length()-1);
}else{
value = valueObj.toString();
}
returnMap.put(name, value);
}
map = returnMap;
}
public PageData() {
map = new HashMap();
}
@Override
public Object get(Object key) {
Object obj = null;
if(map.get(key) instanceof Object[]) {
Object[] arr = (Object[])map.get(key);
obj = request == null ? arr:(request.getParameter((String)key) == null ? arr:arr[0]);
} else {
obj = map.get(key);
}
return obj;
}
public String getString(Object key) {
return (String)get(key);
}
@SuppressWarnings("unchecked")
@Override
public Object put(Object key, Object value) {
return map.put(key, value);
}
@Override
public Object remove(Object key) {
return map.remove(key);
}
public void clear() {
map.clear();
}
public boolean containsKey(Object key) {
// TODO Auto-generated method stub
return map.containsKey(key);
}
public boolean containsValue(Object value) {
// TODO Auto-generated method stub
return map.containsValue(value);
}
public Set entrySet() {
// TODO Auto-generated method stub
return map.entrySet();
}
public boolean isEmpty() {
// TODO Auto-generated method stub
return map.isEmpty();
}
public Set keySet() {
// TODO Auto-generated method stub
return map.keySet();
}
@SuppressWarnings("unchecked")
public void putAll(Map t) {
// TODO Auto-generated method stub
map.putAll(t);
}
public int size() {
// TODO Auto-generated method stub
return map.size();
}
public Collection values() {
// TODO Auto-generated method stub
return map.values();
}
最后就是Tool类了,在我提交的文件中。
标签:
原文地址:http://www.cnblogs.com/lizyyt/p/5765465.html