急急急啊!!
请问大家,我已经把数据库中得数据查询出来放在了我的一个jsp页面的表格中了.
现在我要点击一个按钮"导出表格内容到excel中",就能把这个表格中得内容导出到excel里面!!
如何实现啊?希望能有代码讲解!!谢谢!万分感谢!~
[解决办法]
可以用js导出excel
/** * 生成Excel文件并保存 * @param {String} 要生成的数据所在的表单名称 * @param {String} 要生成的数据所在的table名称 */function exportexcel(formName, tableName){ var oXL = new ActiveXObject("Excel.Application"); oXL.Visible = true; var oWB = oXL.Workbooks.Add(); var oSheet = oWB.ActiveSheet; var div1 = document.all.item(formName); var table1 = div1.all.item(tableName); var table = new Array(); var retArr = getTableRowsCols(table1); var rlen = retArr[0]; var clen = retArr[1]; var flagTable = new Array(rlen); for (var i = 0; i < rlen; i++) { flagTable[i] = new Array(clen); for (var j = 0; j < clen; j++) { flagTable[i][j] = 0; } } var rowBegin = 1, colBegin = 1; //导出的表格的起始位置 var c1 = oSheet.Cells(rowBegin, colBegin); var c2 = oSheet.Cells(rlen + rowBegin - 1, clen + colBegin - 1); oSheet.Range(c1, c2).VerticalAlignment = -4108; oSheet.Range(c1, c2).HorizontalAlignment = -4108; exportTable(oSheet, flagTable, table1, rowBegin, colBegin); oSheet.Range(oSheet.Cells(rowBegin, colBegin), oSheet.Cells(rlen + rowBegin - 1, clen + colBegin - 1)).EntireColumn.AutoFit(); oXL.UserControl = true; oXL.Quit();}/** * 返回当前表格的行数和列数 * 被exportexcel调用 * @param {Object} 表格对象 * @param {Array} 数组retArr, retArr[0], 行数;retArr[1], 列数 */function getTableRowsCols(objTable){ var rowCnt = 0, colCnt = 0; for (var i = 0; i < objTable.rows.length; i++) { var row = objTable.rows[i]; //当前行 var rowRows = 1, rowCols = 0; //当前行的初始行数和列数 for (var j = 0; j < row.cells.length; j++) { var cell = row.cells[j]; //当前单元格 if (cell.firstChild != null && cell.firstChild.tagName == "TABLE") { var inRetArr = getTableRowsCols(cell.firstChild); rowCols += inRetArr[1]; rowRows = rowRows < inRetArr[0] ? inRetArr[0] : rowRows; } else { //不是表格 rowCols += parseInt(cell.colSpan); rowRows = rowRows < parseInt(cell.rowSpan) ? parseInt(cell.rowSpan) : rowRows; } } colCnt = colCnt < rowCols ? rowCols : colCnt; rowCnt += rowRows; } var retArr = new Array(); retArr[0] = rowCnt; retArr[1] = colCnt; return retArr;}/** * 导出表格 * 被exportexcel调用 */function exportTable(oSheet, flagTable, objTable, rowBegin, colBegin){ var flagRow = 0, flagCol = 0; //跟踪当前的表示表中的相对位置 for (var i = 0; i < objTable.rows.length; i++) { var row = objTable.rows[i]; //当前行 flagCol = 0; var subTableRows = 1; //当前行若有表格,则记录其中最大的行数 for (var j = 0; j < row.cells.length; j++) { while (flagTable[rowBegin - 1 + flagRow][colBegin - 1 + flagCol] == 1) { flagCol++; } var cell = row.cells[j]; //当前单元格 if (cell.firstChild != null && cell.firstChild.tagName == "TABLE") { var retArrRows = getTableRowsCols(cell.firstChild)[0]; subTableRows = subTableRows < retArrRows ? retArrRows : subTableRows; exportTable(oSheet, flagTable, cell.firstChild, rowBegin + flagRow, colBegin + flagCol); } else { oSheet.Cells(rowBegin + flagRow, colBegin + flagCol).Font.Bold = 1; var rs = parseInt(cell.rowSpan); var cs = parseInt(cell.colSpan); oSheet.Cells(rowBegin + flagRow, colBegin + flagCol).NumberFormatLocal = "@"; oSheet.Cells(rowBegin + flagRow, colBegin + flagCol).Value = cell.innerText; oSheet.Range(oSheet.Cells(rowBegin + flagRow, colBegin + flagCol), oSheet.Cells(rowBegin + flagRow + rs - 1, colBegin + flagCol + cs - 1)).MergeCells = 1; //填写当前单元格在标志表格中的标志 for (var k = 0; k < rs; k++) { for (var l = 0; l < cs; l++) { flagTable[rowBegin - 1 + flagRow + k][colBegin - 1 + flagCol + l] = 1; } } } flagCol = flagCol + cs - 1; //加速标志表格的列的移动,因为当前表格已经标识了cs个标志单元 } flagRow += subTableRows; }}