本文目录一览:
- 1、如何将jsp数据导出到excel中
- 2、jsp如何导出excel
- 3、如何把JSP页面导出到Excel中
- 4、怎么把JSP页面里的查询出来的数据导出到excel
- 5、jsp页面的数据怎么样导出到excel表格
- 6、怎么从jsp页面的表格导出到Excel
如何将jsp数据导出到excel中
jsp通过POI导入导出Excel。
步骤:
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-----------------
// 第一步,创建一个webbook
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,添加一个sheet
HSSFSheet sheet = wb.createSheet("表一");
// 第三步,在sheet中添加表头
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
----------------
生成单元格内容
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("内容");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
--------------
FileOutputStream fout = new FileOutputStream("D:/test.xls");
wb.write(fout);
fout.close();
jsp如何导出excel
类文件,导入poi包。package com.grs.exportexcel;import java.io.*;
import java.util.List;
import org.apache.poi.hssf.usermodel.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public final class ExcelUtils
{public ExcelUtils() {
}/**
* 导出excel对外接口
*
* @param title 标题 如:同业对标分段统计报表
* @param tableData 数据表数据 如:new String[][]{{"表头1","表头2"},{"data1","data2"}}
* @param exportFileName 导出后的文件名 如:data.xls
* @param request
* @param response
*/
public static void exportExcelData(
String title,
String[][] tableData,
String exportFileName,
HttpServletRequest request,
HttpServletResponse response) {
response.reset();
String fileName = "attachment;filename=\"" + exportFileName + "\";";
response.setHeader("Content-disposition", fileName);
response.setContentType("application/vnd.ms-excel");
try {
exportExcel(title,tableData,response.getOutputStream(),createWorkbook());
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception e) {
e.printStackTrace();
// log.error(e.getMessage(), e);
}
}
protected static void exportExcel(
String title,
String[][] tableData,
OutputStream output,
HSSFWorkbook workbook)
throws Exception {
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setFillBackgroundColor((short) 55);
HSSFFont hsFont = workbook.createFont();
hsFont.setBoldweight((short) 700);
String titles[] = tableData[0];
titleStyle.setFont(hsFont);
titleStyle.setAlignment((short) 2);
HSSFRow row = sheet.createRow(1); row.setHeight((short) 300);
HSSFCell cell = createCell(row, (short) 0, title, titleStyle); row = sheet.createRow(3);
row.setHeight((short) 250);
short i = 0;
for (int forI = 0; forI titles.length; forI++) {
createCell(row, i, titles[forI], titleStyle);
i++;
} int rowCount = 4; for (int rowPos = 1; rowPos tableData.length; rowPos++) {
row = sheet.createRow(rowCount++);
i = 0;
for (int colPos = 0; colPos tableData[rowPos].length; colPos++) {
createCell(row, i, tableData[rowPos][colPos], null);
i++;
}
}
try {
workbook.write(output);
} catch (Exception e) {
//throw new Exception(e);
}
} protected static HSSFCell createCell(
HSSFRow row,
short cellCount,
String value,
HSSFCellStyle titleStyle) {
HSSFCell cell = row.createCell(cellCount);
cell.setEncoding((short) 1);
if (titleStyle != null)
cell.setCellStyle(titleStyle);
cell.setCellValue(value); return cell;
}public static HSSFWorkbook createWorkbook() throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
return wb;
}public static HSSFWorkbook createWorkbook(int sheetCount)
throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
for (int i = 0; i sheetCount; i++) {
HSSFSheet si = wb.createSheet();
} return wb;
}public static HSSFWorkbook createWorkbook(int sheetCount, List sblxList)
throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();
for (int i = 0; i sheetCount; i++) {
HSSFSheet si = wb.createSheet((String) sblxList.get(i));
wb.setSheetName(i, (String) sblxList.get(i), (short) 1);
} return wb;
}}页面代码!%@ page contentType="text/html; charset=gb2312"%
%@ page import="java.util.*"%
%@ page import="com.grs.exportexcel.*"%%
//导出在数据
String queryData[][] = new String[3][2]; //[行][列]
//列名
/***
***在此处获得请求在参数,根据参数或者调用方法得到结果
***将结果存入queryDate[][]中
***/ String exportFileName = "data.xls";//导出Excel在名称可以根据参数命名。
//导出EXCEL
ExcelUtils.exportExcelData("san", queryData, exportFileName,
request, response);
if (out != null out.getBufferSize() != 0) {
out.clearBuffer();
out = pageContext.pushBody();
}
%
如何把JSP页面导出到Excel中
jsp页面导出excel的话,刚好有做这个功能,可以参考如下代码:
function getExplorer() {
var explorer = window.navigator.userAgent;
// ie
if (explorer.indexOf("MSIE") = 0 || (explorer.indexOf("Windows NT 6.1;") = 0 explorer.indexOf("Trident/7.0;") = 0) ) {
alert("识别你是IE浏览器1111======");
return 'ie';
}
// firefox
else if (explorer.indexOf("Firefox") = 0) {
return 'Firefox';
}
// Chrome
else if (explorer.indexOf("Chrome") = 0) {
return 'Chrome';
}
// Opera
else if (explorer.indexOf("Opera") = 0) {
return 'Opera';
}
// Safari
else if (explorer.indexOf("Safari") = 0) {
return 'Safari';
}
}
//设置导出的excel的标题
var excelTitle ="表格数据";
function toOutPutExcel(tableid,title) {// 整个表格拷贝到EXCEL中
excelTitle =title;
if (getExplorer() == 'ie') {
var curTbl = document.getElementById(tableid);
var oXL = new ActiveXObject("Excel.Application");
// 创建AX对象excel
var oWB = oXL.Workbooks.Add();
// 获取workbook对象
var xlsheet = oWB.Worksheets(1);
// 激活当前sheet
var sel = document.body.createTextRange();
sel.moveToElementText(curTbl);
// 把表格中的内容移到TextRange中
sel.select;
// 全选TextRange中内容
sel.execCommand("Copy");
// 复制TextRange中内容
xlsheet.Paste();
// 粘贴到活动的EXCEL中
oXL.Visible = true;
// 设置excel可见属性
try {
var fname = oXL.Application.GetSaveAsFilename("Excel.xls",
"Excel Spreadsheets (*.xls), *.xls");
} catch (e) {
print("Nested catch caught " + e);
} finally {
oWB.SaveAs(fname);
oWB.Close(savechanges = false);
// xls.visible = false;
oXL.Quit();
oXL = null;
// 结束excel进程,退出完成
// window.setInterval("Cleanup();",1);
idTmr = window.setInterval("Cleanup();", 1);
}
} else {
tableToExcel(tableid);
}
}
function Cleanup() {
window.clearInterval(idTmr);
CollectGarbage();
}
var tableToExcel = (function() {
var uri = 'data:application/vnd.ms-excel;base64,', template = 'html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns=""head!--[if gte mso 9]xmlx:ExcelWorkbookx:ExcelWorksheetsx:ExcelWorksheetx:Name{worksheet}/x:Namex:WorksheetOptionsx:DisplayGridlines//x:WorksheetOptions/x:ExcelWorksheet/x:ExcelWorksheets/x:ExcelWorkbook/xml![endif]--/headbodytable{table}/table/body/html', base64 = function(
s) {
return window.btoa(unescape(encodeURIComponent(s)))
}, format = function(s, c) {
return s.replace(/{(\w+)}/g, function(m, p) {
return c[p];
})
}
return function(table, name) {
if (!table.nodeType)
table = document.getElementById(table)
var ctx = {
worksheet : excelTitle || 'Worksheet',
table : table.innerHTML
}
//window.location.href = uri + base64(format(template, ctx))
var titleDate= new Date().Format("yyyy-MM-dd hh-mm-ss");
document.getElementById("dlink").href = uri + base64(format(template, ctx));
document.getElementById("dlink").download = excelTitle+titleDate+".xls";
document.getElementById("dlink").click();
}
})()
怎么把JSP页面里的查询出来的数据导出到excel
jsp页面导出excel的话,刚好有做这个功能,可以参考如下代码:
function getExplorer() {
var explorer = window.navigator.userAgent;
// ie
if (explorer.indexOf("MSIE") = 0 || (explorer.indexOf("Windows NT 6.1;") = 0 explorer.indexOf("Trident/7.0;") = 0) ) {
alert("识别你是IE浏览器1111======");
return 'ie';
}
// firefox
else if (explorer.indexOf("Firefox") = 0) {
return 'Firefox';
}
// Chrome
else if (explorer.indexOf("Chrome") = 0) {
return 'Chrome';
}
// Opera
else if (explorer.indexOf("Opera") = 0) {
return 'Opera';
}
// Safari
else if (explorer.indexOf("Safari") = 0) {
return 'Safari';
}
}
//设置导出的excel的标题
var excelTitle ="表格数据";
jsp页面的数据怎么样导出到excel表格
方法一:
function saveCode(obj) {
var winname = window.open('', '_blank', 'top=10000');
var strHTML = document.all.tableExcel.innerHTML;
winname.document.open('text/html', 'replace');
winname.document.writeln(strHTML);
winname.document.execCommand('saveas','','excel.xls');
winname.close();
}
方法二:
// Excel导出2
/**
如果javaScript报“Automation 服务器不能创建对象”,则按下方解决。
打开Internet Explorer “工具”菜单栏中的“选项”一栏,单击“安全”栏中的“自定义级别”选项卡,
将第三项“对没有标记为安全的activex控件进行初始化和脚本运行”设置成“启用”即可。
*/
function exportExecl(tableId) {
var table=document.getElementById(tableId);
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var sel=document.body.createTextRange();
sel.moveToElementText(table);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;
}
这里的tableId为表格的id
怎么从jsp页面的表格导出到Excel
无非是这样的一个过程。先通过数据库查询出数据,放到List里,然后把这个List发往页面,然后遍历这个List把数据显示到这个表格里。 要想把数据导出到execel,很简单,把页面接受的这个List用jxl写到Execel就行了。具体将List导出到Execel的类如下:
package cms.dao;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import cms.utlis.DbUtils;
public class ToExecelByQuery {
//方法接受两个参数,一个是list,这个地方我用了泛型。另一个参数是HttpServletResponse response
public static void toExcelBy(ListAccessLog list,HttpServletResponse response) {
// 创建工作表
WritableWorkbook book=null;
response.reset();
// 创建工作流
OutputStream os =null;
try {
// 设置弹出对话框
response.setContentType("application/DOWLOAD");
// 设置工作表的标题
response.setHeader("Content-Disposition",
"attachment; filename=****.xls");//设置生成的文件名字
os = response.getOutputStream();
// 初始化工作表
book = Workbook.createWorkbook(os);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
//以下是我做的导出日志的一个模版
int nCount = list.size();
WritableSheet sheet = book.createSheet("访问日志", 0);
// 生成名为"商品信息"的工作表,参数0表示这是第一页
int nI = 1;
// 表字段名
sheet.addCell(new jxl.write.Label(0, 0, "日志编号"));
sheet.addCell(new jxl.write.Label(1, 0, "用户ID"));
sheet.addCell(new jxl.write.Label(2, 0, "用户姓名"));
sheet.addCell(new jxl.write.Label(3, 0, "访问日期"));
sheet.addCell(new jxl.write.Label(4, 0, "访问时间"));
sheet.addCell(new jxl.write.Label(5, 0, "名片ID"));
sheet.addCell(new jxl.write.Label(6, 0, "名片名称"));
sheet.addCell(new jxl.write.Label(7, 0, "创建日期"));
sheet.addCell(new jxl.write.Label(8, 0, "更新日期"));
// 将数据追加
for(int i=1;ilist.size();i++){
sheet.addCell(new jxl.write.Label(0, i, list.get(i).toString()));
sheet.addCell(new jxl.write.Label(1, i, list.get(i).getUserId()));
sheet.addCell(new jxl.write.Label(2, i, list.get(i).getUsername()));
sheet.addCell(new jxl.write.Label(3, i, list.get(i).getCrtTim()));
sheet.addCell(new jxl.write.Label(4, i, list.get(i).getComplTime()));
sheet.addCell(new jxl.write.Label(5, i, list.get(i).getCopId()));
sheet.addCell(new jxl.write.Label(6, i, list.get(i).getFirstname()));
sheet.addCell(new jxl.write.Label(7, i, list.get(i).getCrtTim()));
sheet.addCell(new jxl.write.Label(8, i, list.get(i).getUpdTim()));
}
book.write();
book.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
大体思路就是这样的,别忘了在你的项目中导入JXL必要的jar包,这个包叫jxl.jar,你可以下载一个。希望能帮到你,欢迎追问。望采纳!