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;
}
}