easyui datagrid导出Excel 有什么好的解决方法吗 jsp实现不了, 后台,首先你要有个exportToExcel来设定Excel的的一大堆属性,基本上算还复杂的了,写定了exportToExcel后接下来页面点击导出就调用方法,先是查询然后写入你要导入的字段名称、表格大小、字符类型等,通过循环导出 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 一般是通过服务器来导出文件,js客户端导出限制太多。。导出excel直接生成table的html代码就行,后缀起为xls excel也能打开这种格式的文件 主要是excelBean这一块不怎么好写,要写得如:设定文件头、创建新的Excel 工作簿、标题居中、设置网格标题(字段)、等 ,你去网上找找吧,controller那还好就把文件名、字段名、大小、类型、然后把你要导出的字段从下标0开始去get,最后执行Bean里 你写好的方法名(response, 文件名, 标题名, 字段,,表格大小,类型); 你说的是真部分吧 我昨天下了一个了package com.wanxin.upload.excle.action;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.net.URL;import java.util.ArrayList;import java.util.List;import javax.servlet.http.HttpServletResponse;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 com.wanxin.upload.excle.bean.indent;import com.wanxin.upload.excle.dao.DAOException;import com.wanxin.upload.excle.dao.DAOFactory;import com.wanxin.upload.excle.dao.impl.ExcelFileDAO;import com.wanxin.upload.excle.util.AbstractAction;public class importAction extends AbstractAction{ private List<indent> list=new ArrayList<indent>(); private String ids; //查询所有 public String search() throws IOException{ ExcelFileDAO dao=DAOFactory.getExcelFilemdao(); try { list=dao.searchAll(); } catch (DAOException e) { e.printStackTrace(); } return "search"; } //导出 public String export() throws IOException, DAOException{ ExcelFileDAO dao=DAOFactory.getExcelFilemdao(); // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook wb = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet HSSFSheet sheet = wb.createSheet("订单表"); // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short HSSFRow row = sheet.createRow((int) 0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 HSSFCell cell = row.createCell((short) 0); cell.setCellValue("A"); cell.setCellStyle(style); cell = row.createCell((short) 1); cell.setCellValue("B"); cell.setCellStyle(style); cell = row.createCell((short) 2); cell.setCellValue("C"); cell.setCellStyle(style); cell = row.createCell((short) 3); cell.setCellValue("D"); cell.setCellStyle(style); cell = row.createCell((short) 4); cell.setCellValue("E"); cell.setCellStyle(style); cell = row.createCell((short) 5); cell.setCellValue("F"); cell.setCellStyle(style); cell = row.createCell((short) 6); cell.setCellValue("G"); cell.setCellStyle(style); cell = row.createCell((short) 7); cell.setCellValue("H"); cell.setCellStyle(style); cell = row.createCell((short) 8); cell.setCellValue("I"); cell.setCellStyle(style); List<indent> list=new ArrayList<indent>(); // 第五步,写入实体数据 实际应用中这些数据从数据库得到, list=dao.searchByids(ids); for (int i = 0; i < list.size(); i++) { row = sheet.createRow((int) i + 1); indent stu = list.get(i); // 第四步,创建单元格,并设置值 row.createCell((short) 0).setCellValue(stu.getState()); row.createCell((short) 1).setCellValue(stu.getOrderid()); row.createCell((short) 2).setCellValue(stu.getItem()); row.createCell((short) 3).setCellValue(stu.getOrderType()); row.createCell((short) 4).setCellValue(stu.getOrderUnit()); row.createCell((short) 5).setCellValue(stu.getOrderDate()); row.createCell((short) 6).setCellValue(stu.getOrderNumber()); row.createCell((short) 7).setCellValue(stu.getOrderVarieties()); row.createCell((short) 8).setCellValue((double)stu.getOrderMinistrial()); } // 第六步,将文件存到指定位置 try { FileOutputStream fout = new FileOutputStream("E:/students.xls"); wb.write(fout); fout.flush(); fout.close(); } catch (Exception e) { e.printStackTrace(); } try { downLoad("E:/students.xls",response,false); } catch (Exception e) { e.printStackTrace(); } return null; } //下载 public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception { File f = new File(filePath); if (!f.exists()) { response.sendError(404, "File not found!"); return; } BufferedInputStream br = new BufferedInputStream(new FileInputStream(f)); byte[] buf = new byte[1024]; int len = 0; response.reset(); // 非常重要 if (isOnLine) { // 在线打开方式 URL u = new URL("file:///" + filePath); response.setContentType(u.openConnection().getContentType()); response.setHeader("Content-Disposition", "inline; filename=" + f.getName()); // 文件名应该编码成UTF-8 } else { // 纯下载方式 response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment; filename=" + f.getName()); } OutputStream out = response.getOutputStream(); while ((len = br.read(buf)) > 0) out.write(buf, 0, len); out.flush(); br.close(); out.close(); } public List<indent> getList() { return list; } public void setList(List<indent> list) { this.list = list; } public String getIds() { return ids; } public void setIds(String ids) { this.ids = ids; } public static void main(String[] args) throws DAOException{ }} baidu_maps_api查询地图信息问题 鼠标滚动使图片放大或缩小 抢分!js 利用正则判断输入内容是否符合条件 求人改一段跳转代码 关于js的删除按钮问题 server端写script到客户端 这条js语句有什么错误long extended = quan * price*1.08; 百分求个载入.js文件的方法!(在线等,速度结) 我用year-month-day为什么得不到date的值 我想做一个载入数据的进度条,但总是先把数据都读出来之后,才显示进度条 为什么jQuery内部定义的方法可以用呢? html页面上没有script标签,也没有“=”,如何执行一个alert(1)
package com.wanxin.upload.excle.action;import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
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 com.wanxin.upload.excle.bean.indent;
import com.wanxin.upload.excle.dao.DAOException;
import com.wanxin.upload.excle.dao.DAOFactory;
import com.wanxin.upload.excle.dao.impl.ExcelFileDAO;
import com.wanxin.upload.excle.util.AbstractAction;
public class importAction extends AbstractAction{
private List<indent> list=new ArrayList<indent>();
private String ids;
//查询所有
public String search() throws IOException{
ExcelFileDAO dao=DAOFactory.getExcelFilemdao();
try {
list=dao.searchAll();
} catch (DAOException e) {
e.printStackTrace();
}
return "search";
}
//导出
public String export() throws IOException, DAOException{
ExcelFileDAO dao=DAOFactory.getExcelFilemdao();
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("订单表");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("A");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("B");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("C");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("D");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
cell.setCellValue("E");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("F");
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setCellValue("G");
cell.setCellStyle(style);
cell = row.createCell((short) 7);
cell.setCellValue("H");
cell.setCellStyle(style);
cell = row.createCell((short) 8);
cell.setCellValue("I");
cell.setCellStyle(style);
List<indent> list=new ArrayList<indent>();
// 第五步,写入实体数据 实际应用中这些数据从数据库得到,
list=dao.searchByids(ids);
for (int i = 0; i < list.size(); i++)
{
row = sheet.createRow((int) i + 1);
indent stu = list.get(i);
// 第四步,创建单元格,并设置值
row.createCell((short) 0).setCellValue(stu.getState());
row.createCell((short) 1).setCellValue(stu.getOrderid());
row.createCell((short) 2).setCellValue(stu.getItem());
row.createCell((short) 3).setCellValue(stu.getOrderType());
row.createCell((short) 4).setCellValue(stu.getOrderUnit());
row.createCell((short) 5).setCellValue(stu.getOrderDate());
row.createCell((short) 6).setCellValue(stu.getOrderNumber());
row.createCell((short) 7).setCellValue(stu.getOrderVarieties());
row.createCell((short) 8).setCellValue((double)stu.getOrderMinistrial());
}
// 第六步,将文件存到指定位置
try
{
FileOutputStream fout = new FileOutputStream("E:/students.xls");
wb.write(fout);
fout.flush();
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
}
try {
downLoad("E:/students.xls",response,false);
} catch (Exception e) {
e.printStackTrace();
} return null;
}
//下载
public void downLoad(String filePath, HttpServletResponse response, boolean isOnLine) throws Exception {
File f = new File(filePath);
if (!f.exists()) {
response.sendError(404, "File not found!");
return;
}
BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
byte[] buf = new byte[1024];
int len = 0;
response.reset(); // 非常重要
if (isOnLine) { // 在线打开方式
URL u = new URL("file:///" + filePath);
response.setContentType(u.openConnection().getContentType());
response.setHeader("Content-Disposition", "inline; filename=" + f.getName());
// 文件名应该编码成UTF-8
} else { // 纯下载方式
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());
}
OutputStream out = response.getOutputStream();
while ((len = br.read(buf)) > 0)
out.write(buf, 0, len);
out.flush();
br.close();
out.close();
}
public List<indent> getList() {
return list;
} public void setList(List<indent> list) {
this.list = list;
} public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
public static void main(String[] args) throws DAOException{
}
}