就是将form中查询出来的数据,导出一个excel文件 这个怎么弄啊 求教
------解决方案--------------------
点击导出按钮,后台再执行之前form查询的sql,然后把结果用控件,比如jxl写成excel文件
------解决方案--------------------
//在service层写的,在action直接调用此方法就行了
//导出Excel
public boolean exportExcel(HttpServletResponse response,List<cityinfo> list)
{
try
{
OutputStream os = response.getOutputStream();// 取得输出流
response.reset();// 清空输出流
response.setHeader("Content-disposition", "attachment; filename=fine.xls");// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
WritableWorkbook wbook = Workbook.createWorkbook(os); // 建立excel文件
String tmptitle = "财务报表"; // 标题
WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
// 设置excel标题
WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,WritableFont.BOLD,
false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
WritableCellFormat wcfFC = new WritableCellFormat(wfont);
wcfFC.setBackground(Colour.AQUA);
wsheet.addCell(new Label(1, 0, tmptitle, wcfFC));
wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 14,WritableFont.BOLD,
false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
wcfFC = new WritableCellFormat(wfont);
// 开始生成主体内容
wsheet.addCell(new Label(0, 2, "城市代码"));
wsheet.addCell(new Label(1, 2, "城市名"));
for(int i=0;i<list.size();i++) <br="">{
wsheet.addCell(new Label(0, i+3, list.get(i).getCityid())); //数据库的城市代码字段
wsheet.addCell(new Label(1, i+3, list.get(i).getName())); //数据库的城市名字段
}
// 主体内容生成结束
wbook.write(); // 写入文件
wbook.close();
os.close(); // 关闭流
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
------解决方案--------------------
poi或者jxl,楼主自选
------解决方案--------------------
poi或者jxl..我用JXL做过,还可以支持超链接什么的
------解决方案--------------------
3楼正解
------解决方案--------------------
action到你写的servlet,接收请求,然后响应;使用POI或JXL进行导出Excel
------解决方案--------------------
给你一段导出excel吧
package com.zds.emis.employee.action;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import jxl.Workbook;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.zds.emis.employee.service.IEmployeeManageBs;
import com.zds.emis.employee.service.imp.EmployeeManageBs;
import com.zds.emis.employee.vo.EmployeeVo;
public class ExportExcelAction extends ActionSupport{
//获取需要导出的员工编号
private String empCode;
//获取需要导出的员工姓名
private String empName;
private IEmployeeManageBs employeeManageBs;
public void setEmployeeManageBs(IEmployeeManageBs employeeManageBs) {
this.employeeManageBs = employeeManageBs;