java+jxl+jsp导出excel问题

ivyaiivy 2014-11-21 12:21:15
能导出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());

%>
...全文
565 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
GB_18030 2014-12-01
  • 打赏
  • 举报
回复
debug模式断点跟踪,观察数据的传递情况 这种错误往往是不细心导致的
longzl123 2014-12-01
  • 打赏
  • 举报
回复
导出Excel的返回页面 exportOrdersExcel.jsp <%@ page language="java" import= "java.util.*" pageEncoding ="utf-8"%> <%@ page contentType="text/html; charset=GBK" %> <%@taglib uri= "/struts-tags" prefix ="s"%> <%@page import= "java.util.*"%> <%@page import= "java.text.SimpleDateFormat" %> <% response.setContentType("application/vnd.ms-excel;charset=GBK" ); %> <HTML> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <head>< title>Test</title ></head> <body> <% request.setCharacterEncoding( "GBK"); SimpleDateFormat sf= new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss" ); response.setHeader( "Content-Disposition", "attachment;filename=order_" +sf.format(new Date())+ ".xls"); %> <center> 订单列表</center > <table border= "1" align ="center" width="60%"> <tr align="center"> <td >序号 </td> <td >订单号 </td> <td >单价(元) </td> <td >数量 </td> <td> 总金额(元)</td > <td >优惠价格(元) </td> <td >实际需付金额(元) </td> <td >下单时间 </td> <td >订单完成时间 </td> <td >客户账号 </td> <td >主机名称 </td> <td >订单状态 </td> </tr > <s:iterator value="#request.orderList" var= "order" status ="st"> <tr align="center"> <td style=" mso-number-format:'\@' ;"> ${st.count }</td > <td style="mso-number-format: '\@';">  <s:property value="#order.orderId" /> </td> <td style=" mso-number-format:'\@' ;">  <s:property value="#order.price" /> </ td> <td style=" mso-number-format:'\@' ;">  <s:property value="#order.buyCount" /> </ td> <td style=" mso-number-format:'\@' ;">  <s:property value="%{@com.cloudsoar3c.tools.Util@getTotalPriceByOrder(#order.totalPrice)}" /> </ td> <td style=" mso-number-format:'\@' ;">  <s:property value="#order.preferentialPrice" /></td> <td style=" mso-number-format:'\@' ;">  <s:property value="%{@com.cloudsoar3c.tools.Util@getActualPayByOrder(#order.totalPrice,#order.preferentialPrice)}" /> </ td> <td style=" mso-number-format:'\@' ;">  <s:date name="#order.orderTime" format="yyyy-MM-dd HH:mm:dd"/>  </td > <td style=" mso-number-format:'\@' ;">  <s:date name="#order.completeTime" format="yyyy-MM-dd HH:mm:dd"/>  </td > <td style=" mso-number-format:'\@' ;">  <s:property value="#order.TClient.email" /> </ td> <td style=" mso-number-format:'\@' ;">  <s:property value="#order.TServerHostLicense.hostName" /> </ td> <td style=" mso-number-format:'\@' ;">  <s:property value="%{#order.TStatusTypeContent.statusTypeContent}" /> </ td> </tr > </s:iterator > </table> </body> </HTML> ------------------------------------------------------------------- 导出word的返回页面 exportOrdersWord.jsp <%@ page language="java" import= "java.util.*" pageEncoding ="utf-8"%> <%@ page contentType="text/html; charset=GBK"%> <%@taglib uri= "/struts-tags" prefix ="s"%> <%@page import= "java.util.*"%> <%@page import= "java.text.SimpleDateFormat" %> <% response.setContentType( "application/vnd.ms-word;charset=GBK" ); %> <HTML> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <head> <title> Test</ title> </head> <body> <% request.setCharacterEncoding( "GBK"); SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss" ); response.setHeader( "Content-Disposition", "attachment;filename=order_" + sf.format(new Date()) + ".doc"); %> <center >订单列表</ center> <table border="1" align= "center" width ="2000px"> <tr align= "center"> <td> 序号</td > <td> 订单号</td > <td> 单价(元)</td > <td> 数量</td > <td> 总金额(元)</td > <td> 优惠价格(元) </td> <td> 实际需付金额(元) </td> <td> 下单时间</td > <td> 订单完成时间</td > <td> 客户账号</td > <td> 主机名称</td > <td> 订单状态</td > </tr> <s:iterator value= "#request.orderList" var ="order" status="st"> <tr align= "center"> <td style=" mso-number-format:'\@' ;"> ${st.count }</td > <td style=" mso-number-format:'\@' ;">  <s:property value= "#order.orderId" />   </td> <td style=" mso-number-format:'\@' ;">  <s:property value= "#order.price" />   </td> <td style=" mso-number-format:'\@' ;">  <s:property value= "#order.buyCount" />   </td> <td style=" mso-number-format:'\@' ;">  <s:property value= "%{@com.cloudsoar3c.tools.Util@getTotalPriceByOrder(#order.totalPrice)}" />  </td> <td style=" mso-number-format:'\@' ;">  <s:property value= "#order.preferentialPrice" /></td > <td style=" mso-number-format:'\@' ;">  <s:property value= "%{@com.cloudsoar3c.tools.Util@getActualPayByOrder(#order.totalPrice,#order.preferentialPrice)}" />  </td> <td style=" mso-number-format:'\@' ;">  <s:date name= "#order.orderTime" format ="yyyy-MM-dd HH:mm:dd" />  </td> <td style=" mso-number-format:'\@' ;">  <s:date name= "#order.completeTime" format ="yyyy-MM-dd HH:mm:dd" />  </td> <td style=" mso-number-format:'\@' ;">   < s:property value="#order.TClient.email" />  </td> <td style=" mso-number-format:'\@' ;">  <s:property value= "#order.TServerHostLicense.hostName" />  </td> <td style=" mso-number-format:'\@' ;">  <s:property value= "%{#order.TStatusTypeContent.statusTypeContent}" />  </td> </tr> </s:iterator> </table > </body> </HTML>
longzl123 2014-12-01
  • 打赏
  • 举报
回复
用这个吧 将数据保存在list集合 然后跳到页面输出list集合 就会自动导出到excel 说明:实现的功能是 checkBox选择多个订单对象,然后由 word或excel 导出 Jsp页面 <a href= "javascript:exportOrders('excel')" >批量导出订单(excel) </a> <a href= "javascript:exportOrders('word')" >批量导出订单(word) </a> -------------------------------------------------------------- Js文件 /* * 批量导出订单 *将要导出数据 id进行拼接 */ function exportOrders(model) { //messageId是 checkbox的 id var messageIds = document.getElementsByName( "messageId"); var orderIds = ""; var checkedCount = 0; for ( var i = 0; i < messageIds.length; i++) { if (messageIds[i].checked) { checkedCount++; } } if (checkedCount <= 0) { alert( "请至少选择一条数据!" ); return; } else { for ( var i = 0; i < messageIds.length; i = i + 1) { if (messageIds[i].checked) { orderIds += ( "'"+messageIds[i].value + "';" ); } } } document.getElementById( "orderIds").value = orderIds; document.getElementById( "model").value = model; var action = document.myForm.action; // 记录之前的Action,用完之后,恢复原状 document.myForm.action = "order/exportOrders/"; document.myForm.submit(); document.myForm.action = action; } -------------------------------------------------------------- Struts.xml文件 <!-- 批量导出订单信息(excel/word)--> <actionname ="exportOrders"method ="exportOrders" class="com.cloudsoar3c.action.OrderAction" > <result name= "excel">/WEB-INF/order/exportOrdersExcel.jsp </result> <result name= "word">/WEB-INF/order/exportOrdersWord.jsp </result> <interceptor-ref name= "securityStack"></interceptor-ref > </action> -------------------------------------------------------------- Action文件 /** * 批量导出订单信息(excel/word) * * @return */ public String exportOrders(){ String stringOrderIds = this.request .getParameter("orderIds"); String model = this.request .getParameter("model"); String orderIds = Util. charReplacement(stringOrderIds, ";", ","); Util. log(orderIds); List<TOrder> orderList = this.orderBiz .getOrderListByIds(orderIds); Util. log("导出的订单集合大小:" +orderList.size()); super.request .setAttribute("orderList", orderList); return "excel" .equals(model.trim()) ? "excel" : "word"; } -------------------------------------------------------------- /** * 拆分字符串(将"A#B#C#D#E#F#"格式的字符串转换为"A,B,C,D,E,F"),并输出 */ public static String charReplacement(String sourceStr, String sourceChar, String targetChar) { String result = ""; // 将需要处理的字符串中含有的sourceChar,全部替换为targetChar String temp = sourceStr. replaceAll(sourceChar, targetChar); // 截取字符串中最后一个字符; int len = temp.length(); result = temp.substring(0, len - 1); return result; } -------------------------------------------------------------------
shixitong 2014-11-24
  • 打赏
  • 举报
回复
引用
已经发私信给你了
ivyaiivy 2014-11-24
  • 打赏
  • 举报
回复
这个有语法错误了,这个list从哪边得到的? dfd dObject=new dfd(); List list = dObject.getData(); 这样才是取list [/quote] 还是不明白,能不能帮我调试成功呀,我有点晕了。
shixitong 2014-11-24
  • 打赏
  • 举报
回复
引用
一样的提示list cannot be resolved to a variable
这个有语法错误了,这个list从哪边得到的? dfd dObject=new dfd(); List list = dObject.getData(); 这样才是取list
ivyaiivy 2014-11-24
  • 打赏
  • 举报
回复
引用 15 楼 shixitong 的回复:
引用
dfd.writeExcel(response.getOutputStream(),list);我用你的方法,就得在JSP里面加上list,但是就会提示list cannot be resolved to a variable,又该怎么解决呢,不好意思,问题可能有点笨,海涵。
jsp里要导入你相应的java类
<%@ page import="java.util.List" %>
其它没有的类依次也导入下
<%@page import="com.jagie.test.dfd" %>
<%@ page import="java.util.List"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.OutputStream"%>
<%@ page import="jxl.*"%>
<%@ page import="java.io.File"%>

<%

response.reset();

response.setContentType("application/vnd.ms-excel");
//dfd.writeExcel(response.getOutputStream(), null);
dfd.writeExcel(response.getOutputStream(),list);

%>
一样的提示list cannot be resolved to a variable
shixitong 2014-11-24
  • 打赏
  • 举报
回复
引用
dfd.writeExcel(response.getOutputStream(),list);我用你的方法,就得在JSP里面加上list,但是就会提示list cannot be resolved to a variable,又该怎么解决呢,不好意思,问题可能有点笨,海涵。
jsp里要导入你相应的java类
<%@ page import="java.util.List" %>
其它没有的类依次也导入下
ivyaiivy 2014-11-24
  • 打赏
  • 举报
回复
引用 13 楼 shixitong 的回复:
//在调用这个方法之前先获得list(通过getData方法),然后传到下面的方法里
 dfd dObject=new dfd();
 List list = dObject.getData();
 File f=new File("kk.xls");
 f.createNewFile();
 writeExcel(new FileOutputStream(f),list);
public static void writeExcel(OutputStream os,List list) 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);//设置列宽度
 
        
        //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());
    } 
}
dfd.writeExcel(response.getOutputStream(),list);我用你的方法,就得在JSP里面加上list,但是就会提示list cannot be resolved to a variable,又该怎么解决呢,不好意思,问题可能有点笨,海涵。
shixitong 2014-11-24
  • 打赏
  • 举报
回复
//在调用这个方法之前先获得list(通过getData方法),然后传到下面的方法里
 dfd dObject=new dfd();
 List list = dObject.getData();
 File f=new File("kk.xls");
 f.createNewFile();
 writeExcel(new FileOutputStream(f),list);
public static void writeExcel(OutputStream os,List list) 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);//设置列宽度
 
        
        //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());
    } 
}
ivyaiivy 2014-11-24
  • 打赏
  • 举报
回复
引用 10 楼 shixitong 的回复:
不一定要把所有都改为静态的,可以把list作为参数传到writeExcel方法中 这样你在main方法中先获得这个list然后把list传到writeExcel中就行了
list我是在LIST getData()方法中获得的,我要如何才能把它做为参数传入静态的writeExcel方法中去呢?
Defonds 2014-11-21
  • 打赏
  • 举报
回复
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()));
}

List list = new ArrayList();
你 new 了一个空链,却没有放任何数据进去,指定是没有数据啊。
for 循环根本就进不去。
  • 打赏
  • 举报
回复
建议使用POI,JXL很久没更新了。
  • 打赏
  • 举报
回复
打断点跟踪看看;看list中有没值。
Giberson1 2014-11-21
  • 打赏
  • 举报
回复
看看例子吧。 jxl是java用来操纵excel表格的一个包。里面提供了各种操作excel的类和方法。类似的还有PIO也可以实现这些功能 下面是实现读取excel的一个简单示例,里面含有注释。
package com.foolfish.jxl;

import java.io.File;
import java.io.IOException;

import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class JxlWrite {
public static void main(String[] args){
WritableWorkbook workbook;
try {
//创建工作簿
workbook = Workbook.createWorkbook(new File("E:/eclipse project/Jxl/bin/test.xls"));
//创建sheet  
       WritableSheet sheet1 = workbook.createSheet("测试用excel", 0);  
       WritableSheet sheet2 = workbook.createSheet("测试excel", 1);  
         
       //开始创建cell  
        
       //WritableCell cell  
       for(int i = 0 ; i < 10 ;i++){  
        //向sheet中写入数据
               sheet1.addCell(new jxl.write.Label(0, i, "书目ID"));  
                 
       }  
       workbook.write();  
       workbook.close();  
        
       //sheet.addCell(new jxl.write.Label(0, 1, "书目ID"));  
} 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();
}  
        
}
}
下面是个读取excel的代码
package com.foolfish.jxl;

import java.io.File;
import java.io.IOException;

import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class JxlWrite {
public static void main(String[] args){
WritableWorkbook workbook;
try {
//创建工作簿
workbook = Workbook.createWorkbook(new File("E:/eclipse project/Jxl/bin/test.xls"));
//创建sheet  
       WritableSheet sheet1 = workbook.createSheet("测试用excel", 0);  
       WritableSheet sheet2 = workbook.createSheet("测试excel", 1);  
         
       //开始创建cell  
        
       //WritableCell cell  
       for(int i = 0 ; i < 10 ;i++){  
        //向sheet中写入数据
               sheet1.addCell(new jxl.write.Label(0, i, "书目ID"));  
                 
       }  
       workbook.write();  
       workbook.close();  
        
       //sheet.addCell(new jxl.write.Label(0, 1, "书目ID"));  
} 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();
}  
        
}
}
shixitong 2014-11-21
  • 打赏
  • 举报
回复
不一定要把所有都改为静态的,可以把list作为参数传到writeExcel方法中 这样你在main方法中先获得这个list然后把list传到writeExcel中就行了
ivyaiivy 2014-11-21
  • 打赏
  • 举报
回复
引用 8 楼 shixitong 的回复:
List list = new ArrayList();改为 List list=getData();试试
因为public static void writeExcel(OutputStream os) throws Exception 是静态的,没法用getData(),我把所有的都改成静态的,也会报别的错
shixitong 2014-11-21
  • 打赏
  • 举报
回复
List list = new ArrayList();改为 List list=getData();试试
sweet____ 2014-11-21
  • 打赏
  • 举报
回复
apache poi导入导出是不错的,这个好用点吧
cn_yaojin 2014-11-21
  • 打赏
  • 举报
回复
导出excel、word了什么的,你用freemarker非常简单,速度快,出错很容易找到,就跟写html样的。
加载更多回复(2)

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧