function AllAreaExcel(){
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var sel=document.body.createTextRange();
sel.moveToElementText(export);
sel.select();
sel.execCommand("Copy");
oSheet.Paste();
oXL.Visible = true;
}
<input type="button" value="表格导入excel" onclick="AllAreaExcel();">
<table id="export"><tr><td></td></tr></table>
测试的时候把ie的安全中心设置下~允许脚本运行和加内载
满意请容采纳。
❷ 如何把JSP页面导出到Excel中
导出excel,常用两种办法。
1、java后台用第三方包 比如POI, 把你想要展示的数据,填进去,导出excel文件。
2、调用第三方插件显示在jsp页面,比如金格控件。用户在页面可以在线编辑excel,然后 用户手动导出成excel文件。
个人建议你,画面jsp展示table之后,单独留一个“导出”按钮,用户点击之后,你后台java再把刚刚通过jsp展示的数据,封装成excel,给用户下载。也就是第一种方法。这种最常用。
❸ 怎么把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文件。
你点击导出也就是从你的服务器上把那个文件下载下来而已
1.直接把你的文件在服务器上的路径给客户端点击下载。
2.在服务端获取文件,用response使用流输出给客户端。
❺ 如何将jsp 中的数据导入到excel表格 中
可以用servlet实现跳转redirectnameExcel.xls就行了。
//拼凑excel文件名字
String filename = String.valueOf(year)+String.valueOf(month)+String.valueOf(day)+h+mise+".xls";
String path = getServletContext().getRealPath("excelexport");
System.out.println("path:"+path);
try{
FileOutputStream fos = new FileOutputStream(path+"/"+filename);
// 创建新的Excel 工作簿
HSSFWorkbook wb = new HSSFWorkbook();
// 在Excel 工作簿中建一工作表
HSSFSheet s = wb.createSheet();
String sheetName = year+"-"+month+"-"+day;
wb.setSheetName(0, sheetName);
//在索引0的位置创建行(第一行)
HSSFRow row = s.createRow((short)0);
HSSFCell cell0 = row.createCell((short) 0);// 第一列
HSSFCell cell1 = row.createCell((short) 1);
HSSFCell cell2 = row.createCell((short) 2);
HSSFCell cell3 = row.createCell((short) 3);
HSSFCell cell4 = row.createCell((short) 4);
HSSFCell cell5 = row.createCell((short) 5);
HSSFCell cell6 = row.createCell((short) 6);
// 定义单元格为字符串类型
cell0.setCellType(HSSFCell.CELL_TYPE_STRING);
cell1.setCellType(HSSFCell.CELL_TYPE_STRING);
cell2.setCellType(HSSFCell.CELL_TYPE_STRING);
cell3.setCellType(HSSFCell.CELL_TYPE_STRING);
cell4.setCellType(HSSFCell.CELL_TYPE_STRING);
cell5.setCellType(HSSFCell.CELL_TYPE_STRING);
cell6.setCellType(HSSFCell.CELL_TYPE_STRING);
cell0.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell3.setEncoding(HSSFCell.ENCODING_UTF_16);
cell4.setEncoding(HSSFCell.ENCODING_UTF_16);
cell5.setEncoding(HSSFCell.ENCODING_UTF_16);
cell6.setEncoding(HSSFCell.ENCODING_UTF_16);
// 在单元格中输入数据
cell0.setCellValue("科室名");
cell1.setCellValue("版组");
cell2.setCellValue("注册名");
cell3.setCellValue("问题题目");
cell4.setCellValue("问题内容");
cell5.setCellValue("发表时间");
cell6.setCellValue("Status");
KSuser ks =new KSuser();
HSSFRow[] rows = null;
if(list != null && list.size()>0){
rows = new HSSFRow[list.size()];
}
int j = 1;
for(int k=0;k <list.size();k++){
ks =list.get(k);
//设置行从第二行开始
rows[j-1] =s.createRow((short)(j));
String[] str =new String[7];
str[0]=ks.getKeshi();
str[1]=ks.getBanzu();
str[2]=ks.getReg_name();
str[3]=ks.getSubject();
str[4]=ks.getText();
str[5]=ks.getRe_day().toString();
str[6]=ks.getStatus();
for(int i=1;i <8;i++){
HSSFCell cell =rows[j-1].createCell((short)(i-1));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(str[i-1]);
}
j++;
}
wb.write(fos);
fos.close();
}catch(Exception e){
e.printStackTrace();
out.println(" <script>alert('异常操作,文件可能正保护中\\n请重试!');history.go(-1); </script>");
}
String urlname = request.getRequestURI();
urlname = urlname.replaceAll("excel", "excelexport");
urlname = urlname+"/"+filename;
System.out.println("urlname:"+urlname);
//request.getSession().setAttribute("excelfile", urlname);
response.sendRedirect(urlname);
❻ 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(); // 第四步,创建单元格,并设置值表头 设置表头居中
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> Excelutilru(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;
}
}