jsp页面导出excel的话,刚好有做这个功能,可以参考如下代码:fun" />
乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>jsp</em>页面内容导出到Excel中 怎么做-jsp 保存excel,jsp直接展

<em>jsp</em>页面内容导出到Excel中 怎么做-jsp 保存excel,jsp直接展

作者:乔山办公网日期:

返回目录:excel表格制作


获取表单数据后,通过poi库操作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) ) {
7a64e58685e5aeb9332alert("识别你是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="http:///TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{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();
}
})()
导出excel,常用两种办法。
1、java后台用第三方包 比如POI, 把你想要展示的数据,填进去,导出excel文件。
2、调用第三方插件显示在jsp页面,比如金格控件。用户在页面可以在线编辑excel,然后 用户手动导出成excel文件。

个人建议你,画面jsp展示table之后,单独留一个“导出”按钮,用户点击之后,你后台java再把刚刚通过jsp展示的数据,封装成excel,给用户下载。也就是第一种方法。这种最常用。

jsp页面上的表数据进行导出。
导出的是一个list对象。还是需要后台进行操作。
以下代码没有把excel进行附件下载操作。
Java代码如下:

package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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;

import entity.Car;
import jxl.Sheet;
import jxl.Workbook;

public class Excelutil {
private File upload;
/**
* 数据输出到excel
* @throws IOException
*时间2015年8月18日
*
* */
public void Excelutilout(List<Car> list, Class<Car> cal) throws IOException{

HSSFWorkbook wb= new HSSFWorkbook();// 第一步,创建一个webbook,对应一个Excel文件
HSSFSheet sheet=wb.createSheet("车辆表"); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFRow row=sheet.createRow((int)0);// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFCellStyle style=wb.createCellStyle(); // 第四步,创建单元格,7a64e58685e5aeb9332并设置值表头 设置表头居中
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 创建一个居中格式

/**
* 对于xecel到底要创建多少个表头由配置文件决定。每个实体类对应一个map。循环创建表头
* */
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("编号");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("车型");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("类型");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("接收日期");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("部品数");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("设变数");
cell.setCellStyle(style);

for (int i = 0; i < list.size(); i++)
{
row = sheet.createRow((int) i + 1);
Car car = (Car) list.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue(car.getId());
row.createCell((short) 1).setCellValue(car.getTypes());
row.createCell((short) 2).setCellValue(car.getKinds());
cell = row.createCell((short) 3);
cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(car.getRec_date()));
row.createCell((short) 4).setCellValue(car.getCom_num());
row.createCell((short) 5).setCellValue(car.getEc_num());
}
// 第六步,将文件存到指定位置
FileOutputStream fout=null;
try
{
fout = new FileOutputStream("E:/123.xls");
wb.write(fout);
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
}finally {
fout.close();
}
}

/**
* 导入。读取文件
* @throws Exception
* */
public List<Car> Excelutildaoru(String path) throws Exception{
File f = new File(path);
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd");
List<Car> list=new ArrayList<Car>();

try{

Workbook rwb=Workbook.getWorkbook(f);
Sheet rs=rwb.getSheet(0);
int clos=rs.getColumns();//得到所有的列
int rows=rs.getRows();//得到所有的行

for (int i = 1; i < rows; i++) {
for (int j = 0; j < clos; j++) {
//第一个是列数,第二个是行数
int id=Integer.parseInt(rs.getCell(j++, i).getContents());//默认最左边编号也算一列 所以这里得j++
String types=rs.getCell(j++, i).getContents();
String kinds=rs.getCell(j++, i).getContents();
Date rec_date=sim.parse(rs.getCell(j++, i).getContents());
int com_num=Integer.parseInt(rs.getCell(j++, i).getContents());
int ec_num=Integer.parseInt(rs.getCell(j++, i).getContents());
Car car = new Car();
car.setId(id);
car.setTypes(types);
car.setKinds(kinds);
car.setCom_num(com_num);
car.setEc_num(ec_num);
car.setRec_date(rec_date);
list.add(car);
}

}return list;
}catch(Exception e){

}
return null;

}

}

相关阅读

  • -jsp打印excel,jsp直接展现excel

  • 乔山办公网excel表格制作
  • 因为ms word和excel的文档都支持html文本格式,因此可以先用word或excel做好模版,另存为Web页,然后将该html改成jsp,将数据部分动态填入即可,不用很辛苦的调整格式 word页面只要在jsp头设
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网