java+jxl+jsp导出excel问题

ivyaiivy ivyaiivy 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
能导出excel,但是没有数据,请大神帮我看下,谢谢,感觉应该是这一段
List list = new ArrayList();
//System.out.println(list.size());
for(int i=3;i<list.size()+3;i++){
Datathplyyvch data = (Datathplyyvch) list.get(i-3);
ws.insertRow(i);
ws.addCell(new Label(0, i, data.getVfdept_code()));
ws.addCell(new Label(1, i, data.getVfdept_name()));
有问题,但是不知道该怎么处理。请大神点修改意见。


dfd.java
package com.jagie.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;


public class dfd{

public Connection getConnection(){
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@10.0.3.6:1521:MSQJGL", "msqjgl", "msqjgl");
} catch (Exception e) {
System.out.println("数据库链接异常!");
e.printStackTrace();
}
return conn;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public List getData(){
ResultSet rs = null;
List list = new ArrayList();
try {
String sql = "select * from jl_verifi_dept";
Statement st =this.getConnection().createStatement();
rs = st.executeQuery(sql);
Datathplyyvch data = null;
while(rs.next()){
data = new Datathplyyvch();
data.setVfdept_code(rs.getString("vfdept_code"));
data.setVfdept_name(rs.getString("vfdept_name"));

list.add(data);
System.out.println(rs.getString("vfdept_NAME"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}

@SuppressWarnings({ "unused", "rawtypes" })
public static void writeExcel(OutputStream os) throws Exception {
try{
jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);
jxl.write.WritableSheet ws = wwb.createSheet("TestSheet1", 0);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat date2Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date date = new java.util.Date();
String dateStr = dateFormat.format(date);

WritableFont wf = new WritableFont(WritableFont.TIMES,16, WritableFont.BOLD,false);
WritableCellFormat wcf = new WritableCellFormat(wf);//实例化文字格式化
wcf.setAlignment(jxl.format.Alignment.CENTRE);//左右居中
wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//上下居中

//打印日期行格式化
WritableFont wf2 = new WritableFont(WritableFont.TIMES,10, WritableFont.NO_BOLD,false);
WritableCellFormat wcf2 = new WritableCellFormat(wf2);//实例化文字格式化
wcf2.setAlignment(jxl.format.Alignment.LEFT);//左右居中
//wcf2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//上下居中

//列名格式化
WritableFont wf3 = new WritableFont(WritableFont.TIMES,13, WritableFont.BOLD,false);
WritableCellFormat wcf3 = new WritableCellFormat(wf3);//实例化文字格式化
wcf2.setAlignment(jxl.format.Alignment.CENTRE);//左右居中
//wcf2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//上下居中

ws.insertRow(0);
ws.mergeCells(0, 0, 1, 0);//合并单元格
ws.addCell(new Label(0, 0, "部门表",wcf));
ws.setRowView(0, 800);//设置高度

ws.insertRow(1);
ws.mergeCells(0, 1, 1, 1);
ws.addCell(new Label(0, 1, "导出日期:"+date2Format.format(date),wcf2));

ws.insertRow(2);
ws.addCell(new Label(0,2,"编码",wcf3));
ws.setColumnView(0, 10);//设置列宽度
ws.addCell(new Label(1,2,"名称",wcf3));
ws.setColumnView(1, 20);//设置列宽度

List list = new ArrayList();
//System.out.println(list.size());
for(int i=3;i<list.size()+3;i++){
Datathplyyvch data = (Datathplyyvch) list.get(i-3);
ws.insertRow(i);
ws.addCell(new Label(0, i, data.getVfdept_code()));
ws.addCell(new Label(1, i, data.getVfdept_name()));
}
//写入Excel工作表
wwb.write();
//关闭Excel工作薄对象
wwb.close();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//System.out.println(getVfdept_code());
}
}

//最好写一个这样的main方法来测试一下你的这个class是否写好了。
public static void main(String[] args)throws Exception{

File f=new File("kk.xls");
f.createNewFile();
//OutputStream os = new FileOutputStream(fileWrite);;
writeExcel(new FileOutputStream(f));
}
}

Datathplyyvch.java
package com.jagie.test;


public class Datathplyyvch {

private String vfdept_code;
private String vfdept_name;

public String getVfdept_code() {
return vfdept_code;
}
public void setVfdept_code(String vfdept_code) {
// TODO 自动生成方法存根
this.vfdept_code=vfdept_code;
}
public String getVfdept_name() {
return vfdept_name;
}
public void setVfdept_name(String vfdept_name) {
// TODO 自动生成方法存根
this.vfdept_name=vfdept_name;
}
}

jsp:
<%@page import="com.jagie.test.dfd" %>

<%

response.reset();

response.setContentType("application/vnd.ms-excel");

dfd.writeExcel(response.getOutputStream());

%>
...全文
评论 22
收藏 已收藏
分享
返回顶部
ivyaiivy
Web 开发 81,108 成员 341,721 内容