就是将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吧
- Java code
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; } public String getEmpCode() { return empCode; } public void setEmpCode(String empCode) { this.empCode = empCode; } public String getEmpName() { return empName; } public void setEmpName(String empName) { this.empName = empName; } public String execute() throws Exception{ EmployeeVo employeeVo=new EmployeeVo(); // 获得员工编号 if(null !=empCode){ employeeVo.setEmpCode(empCode.trim()); System.out.println("Action empCode:"+empCode.trim()); }else{ employeeVo.setEmpCode(empCode); } // 获得员工姓名 if(null !=empName){ employeeVo.setEmpName(empName.trim()); System.out.println("Action empName:"+empName.trim()); }else{ employeeVo.setEmpName(empName); } List employeeList=null; employeeList=employeeManageBs.queryByCondition(employeeVo); // 用日期构建输出文件名称 SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd"); String date = sd.format(new Date()); String excelName = "员工信息_" + date + ".xls"; // 解决导出员工数据时汉字乱码问题 try { excelName = new String(excelName.getBytes(), "iso8859-1"); } catch (UnsupportedEncodingException e3) { // TODO Auto-generated catch block e3.printStackTrace(); } // 选择地址点击保存或者直接点击查看 ServletActionContext.getResponse().setContentType("application/vnd.ms-excel"); ServletActionContext.getResponse().addHeader("Content-Disposition", "attachment; filename=\""+ excelName + "\""); OutputStream os=null; try { os = (OutputStream)(ServletActionContext.getResponse()).getOutputStream(); } catch (IOException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } //利用OutputStream创建WritableWorkbook对象 WritableWorkbook wwb = null; try { wwb = Workbook.createWorkbook(os); } catch (IOException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } //设置字体 、颜色 、大小 WritableFont font1=new WritableFont(WritableFont.TIMES,11,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.GREEN); WritableFont font2=new WritableFont(WritableFont.TIMES,18,WritableFont.BOLD); WritableFont font3=new WritableFont(WritableFont.ARIAL,9,WritableFont.BOLD); WritableFont font4=new WritableFont(WritableFont.TIMES,11,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.RED); WritableCellFormat format1=new WritableCellFormat(font1); WritableCellFormat format2=new WritableCellFormat(font2); WritableCellFormat format3=new WritableCellFormat(font3); WritableCellFormat format4=new WritableCellFormat(font4); try { //把水平对齐方式指定为居中 format1.setAlignment(jxl.format.Alignment.CENTRE); format2.setAlignment(jxl.format.Alignment.CENTRE); format3.setAlignment(jxl.format.Alignment.CENTRE); format4.setAlignment(jxl.format.Alignment.CENTRE); } catch (WriteException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // 生成名为"员工信息"的工作表,参数0表示这是第一页 WritableSheet sheet=wwb.createSheet("员工信息",0); //设置单元表格的宽度为:13 sheet.setColumnView(0,13); sheet.setColumnView(1,13); sheet.setColumnView(2,13); sheet.setColumnView(3,13); sheet.setColumnView(4,15); sheet.setColumnView(5,15); sheet.setColumnView(6,15); sheet.setColumnView(7,13); sheet.setColumnView(8,13); sheet.setColumnView(9,13); sheet.setColumnView(10,13); sheet.setColumnView(11,13); try { //合并单元表格 sheet.mergeCells(0,0,11,0); //向表格里插入数据 并设置 format属性 //在Label对象的构造之中指名单元格位置是第一列第一行(0,0) Label label1 = new Label(0, 0, "员工信息表",format2); Label label2 = new Label(0, 1, "工号",format4); Label label3 = new Label(1, 1, "姓名",format1); Label label4 = new Label(2, 1, "性别",format1); Label label14 = new Label(3, 1, "年龄",format1); Label label15 = new Label(4, 1, "电话",format1); Label label16 = new Label(5, 1, "手机",format1); Label label17 = new Label(6, 1, "邮件",format1); Label label18 = new Label(7, 1, "学历",format1); Label label19 = new Label(8, 1, "地址",format1); Label label20 = new Label(9, 1, "出生日期",format1); Label label21 = new Label(10, 1, "入职日期",format1); Label label22 = new Label(11, 1, "状态",format1); // 将定义好的单元格添加到工作表中 sheet.addCell(label1); sheet.addCell(label2); sheet.addCell(label3); sheet.addCell(label4); sheet.addCell(label14); sheet.addCell(label15); sheet.addCell(label16); sheet.addCell(label17); sheet.addCell(label18); sheet.addCell(label19); sheet.addCell(label20); sheet.addCell(label21); sheet.addCell(label22); EmployeeVo[] empVo=new EmployeeVo[employeeList.size()]; for (int i = 0; i <employeeList.size(); i++){ empVo[i] =(EmployeeVo) employeeList.get(i); Label label5 = new Label(0,i+2,""+empVo[i].getEmpCode(),format4); sheet.addCell(label5); Label label6=new Label(1,i+2,""+empVo[i].getEmpName(),format3); sheet.addCell(label6); Label label7=new Label(2,i+2,"1".equals(empVo[i].getEmpSex())?"男":"女",format3); sheet.addCell(label7); jxl.write.Number label8=new jxl.write.Number(3,i+2,empVo[i].getEmpAge(),format3); sheet.addCell(label8); Label label9=new Label(4,i+2,""+empVo[i].getEmpPhone(),format3); sheet.addCell(label9); Label label10=new Label(5,i+2,""+empVo[i].getEmpMobile(),format3); sheet.addCell(label10); Label label11 = new Label(6,i+2,""+empVo[i].getEmpEmail(),format3); sheet.addCell(label11); Label label12=new Label(7,i+2,""+empVo[i].getEmpDegree(),format3); sheet.addCell(label12); Label label13=new Label(8,i+2,""+empVo[i].getEmpAddress(),format3); sheet.addCell(label13); Label label23=new Label(9,i+2,""+empVo[i].getEmpBirthDate().substring(0, 10),format3); sheet.addCell(label23); Label label24=new Label(10,i+2,""+empVo[i].getEmpEnterDate().substring(0, 10),format3); sheet.addCell(label24); Label label25=new Label(11,i+2,"1".equals(empVo[i].getEmpState())?"在职":"离职",format3); sheet.addCell(label25); } System.out.println("创建成功"); // 写入数据并关闭文件 try { wwb.write(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { wwb.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } return SUCCESS; }}