package com.itiptop.jxl; import java.io.BufferedOutputStream; import java.io.OutputStream; import java.util.Calendar; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import com.itiptop.portal.action.SecurityAction; public class ExportTableToExcel extends SecurityAction { @Override public ActionForward service(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String cellsStr = new String(request.getParameter("arrayStr").getBytes( "iso-8859-1"), "UTF-8"); String[] cells = cellsStr.split(":"); System.out.println(cellsStr); String filename = Calendar.getInstance().getTimeInMillis() + ".xls"; filename = new String(filename.getBytes("UTF-8"), "iso-8859-1"); response.addHeader("Content-Disposition", (new StringBuilder()).append( "attachment;filename=").append(filename).toString()); OutputStream toClient = new BufferedOutputStream(response .getOutputStream()); exportToJxlExcel(filename, "測試", cells, toClient); // toClient.write(cellsStr.toString().getBytes()); toClient.flush(); toClient.close(); return null; } /** * //將Excel對象直接寫入到輸出資料流,使用者通過瀏覽器來訪問Web伺服器, * 如果HTTP頭設定正確的話,瀏覽器自動調用用戶端的Excel應用程式, * 來顯示動態產生的Excel試算表 * @param fileName * @param sheetName * @param cellStrArray * @param os * @return */ public String exportToJxlExcel(String fileName, String sheetName, String[] cellStrArray, OutputStream os) { try { // FileOutputStream os = new FileOutputStream(fileName); if (cellStrArray != null && cellStrArray.length > 0) { WritableWorkbook wwb = Workbook.createWorkbook(os); WritableSheet ws = wwb.createSheet(sheetName, 0); for (int i = 0; i < 20; i++) ws.setColumnView(i, 20); Label lbl = null; String[] objProps = null; int col; int row; int rowSpan; int colSpan; for (String objStr : cellStrArray) {// 每個對象以row,col,rowSpan,colSpan,value形式 objProps = objStr.split(","); if (objProps.length > 4) { col = Integer.parseInt(objProps[1]); row = Integer.parseInt(objProps[0]); rowSpan = Integer.parseInt(objProps[2]); colSpan = Integer.parseInt(objProps[3]); lbl = new Label(col, row, objProps[4]); ws.addCell(lbl); if (rowSpan > 1 || colSpan > 1) ws.mergeCells(col, row, col + colSpan - 1, row + rowSpan - 1); } } wwb.write(); wwb.close(); return fileName; } } catch (Exception e) { e.printStackTrace(); } return null; } }
js:
/** 瀏覽器判斷 */ var Sys = {}; var ua = navigator.userAgent.toLowerCase(); if (window.ActiveXObject) Sys.ie = ua.match(/msie ([/d.]+)/)[1]; else if (document.getBoxObjectFor) Sys.firefox = ua.match(/firefox//([/d.]+)/)[1]; function containsArray(array, obj) { for (var i = 0; i < array.length; i++) { if (array[i] == obj) { return i; break; } } return -1; } Array.prototype.contains = function(obj) { return containsArray(this, obj); } function exportTableToJxlExcel(tableId) { var offsetLeftArray = new Array(); var cell;// 儲存格Dom var col;// 儲存格實際所在列 var cellStr;// 每個cell以row,col,rowSpan,colSpan,value形式 var cellStrArray = []; var objTab = document.getElementById(tableId); // 遍曆第一次取出offsetLeft集合 for (var i = 0; i < objTab.rows.length; i++) { for (var j = 0; j < objTab.rows[i].cells.length; j++) { cell = objTab.rows[i].cells[j]; if (offsetLeftArray.contains(cell.offsetLeft) == -1) offsetLeftArray.push(cell.offsetLeft); } } offsetLeftArray.sort(function(x, y) { return parseInt(x) - parseInt(y); }); //alert("offsetLeft集合:" + offsetLeftArray.join(',')); // 遍曆第二次產生cellStrArray for (var i = 0; i < objTab.rows.length; i++) { for (var j = 0; j < objTab.rows[i].cells.length; j++) { cell = objTab.rows[i].cells[j]; col = offsetLeftArray.contains(cell.offsetLeft); cellStr = i + ',' + col + ',' + cell.rowSpan + ',' + cell.colSpan + "," + (Sys.firefox?cell.textContent:cell.innerText); cellStrArray.push(cellStr); } } // 顯示 //var str = "行,列,rowSpan,colSpan,值/n"; //str += cellStrArray.join('/n'); //alert(str); //把cellStrArray傳到後台 }
jsp請求:
function exportExl(){ var arrayStr=exportTableToJxlExcel('exportTable'); var url="<%=request.getContextPath()%>/exportToExl.do?arrayStr="+arrayStr+"sheetName"+sheetName; window.open(url,'new4','left=220,top=40,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes'); };