java+jxl+jsp导出excel问题
能导出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
Datathplyyvch.java
jsp:
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());
%>
...全文
相关推荐
jxl.jar + commons-fileupload.jar.rar
Java处理Excel使用的jxl.jar包,Java处理文件上传需要的Apache commons-fileupload.jar包,做JSP开发的朋友,支持下
读取excel表数据代码!
这个代码详细,只需要修改与你代码读取文件相同就OK了,基本不需要修改什么,有任何问题请M我!
java读取Excel数据.docx
...
java实现JSP表格数据导出到EXCEL文件
java实现JSP表格数据导出到EXCEL文件
将jsp页面中的table中的数据导出到excel文件中,并可以存储在本地
这是一个简单的javaweb项目例子,使用的是struts2技术,在后台的action中写好一个list,然后给这个list填充好内容,之后在jsp前台显示,并可以通过导出到excel文件,存储在本地指定的位置。


Web 开发
