jsp实现不了, 后台,首先你要有个exportToExcel来设定Excel的的一大堆属性,基本上算还复杂的了,写定了exportToExcel后接下来页面点击导出就调用方法,先是查询然后写入你要导入的字段名称、表格大小、字符类型等,通过循环导出
解决方案 »
- 为什么我这一段在FF中可以实现,但是IE中就不行,求高手解答
- 有关贪吃蛇的问题
- 如何用js实现动态画数据关系图?
- FileUpload 对象 怎么用js 操作 请高手帮忙看下
- 一个基础的xml问题 不知道错哪里 请大虾们指教!!!!
- 如何获得控件所属的FROM名?
- meizz版主及高手请进,求一javascript正则表达式
- 谁有http://www.etang.com那样的下拉菜单,感谢了,
- 怎么取得层的坐标?高手帮忙啊
- 又一个javascript脚本(能使)打开后是乱码,谁知道为什么???????
- 为什么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{
}
}