kenshinsee 2009-06-13 19:51
浏览 215
已采纳

SERVLET中调EXCEL导出函数报错

我调试时候在MAIN中是可以正常到处数据的
但我加到SERVLET中调用这个函数就报NoClassDefFoundError,请问下是什么原因
代码如下(部分)
[code="java"]
public class CommonTookits extends HttpServlet
{
/**

  • 用于导出数据到指定地址,指定名字的EXCEL */ private int checkInitial = 0; //判断初始化,如果非零就不再初始化变量,不然前面的行数会刷新到0 private HSSFWorkbook workbook = null; private HSSFSheet sheet = null; private HSSFRow row = null; private String outputFile = null; private FileOutputStream fOut = null; private int rowIndex = 0; //row的索引,应该是递增的,每个数组元素占一行

public void doExportToExcel(String fileName, String sheetName, String [] dataArray)
{
if (checkInitial ==0)
{
workbook = new HSSFWorkbook(); /********************************/
sheet = workbook.createSheet();
row = sheet.createRow((short)0);
outputFile = fileName;
checkInitial ++;
} //确保初始化一次

try 
{ 
    row = sheet.createRow((short)rowIndex); 
    int cellIndex =0;  //cell的索引,每次传入一个数组进来,cell肯定都要归0

    for(int i =0; i<dataArray.length; i++)
    {
        HSSFCell cell = row.createCell((short) cellIndex); 
        cell.setCellType(HSSFCell.CELL_TYPE_STRING); 
        cell.setCellValue(dataArray[i]); 
        fOut = new FileOutputStream(outputFile); 
        workbook.write(fOut); 
        fOut.flush(); 
        cellIndex ++;
    }

    rowIndex ++;
    fOut.close(); 
}catch(Exception e) { 
    e.printStackTrace();
}

}
}
[/code]

MAIN函数中我就这么调,可以正常运行
[code="java"]
CommonTookits ct = new CommonTookits(); // 函数所在类
Dbconnect dbc = new Dbconnect("ORCL","aaa","aaa"); //自己包装的数据库连接类
ResultSet rs = dbc.doQuery("select * from user_info");
while(rs.next())
{
String [] s = {rs.getString(1),rs.getString(2),rs.getString(3)};
ct.doExportToExcel("C:/Documents and Settings/ibm/桌面/aaa.xls", "hello", s);
}
[/code]

SERVLET中(部分)
[code="java"]
public class UserInfo extends HttpServlet
{
Dbconnect dbconn;
CommonTookits ct;

public UserInfo() throws ClassNotFoundException, SQLException, IOException
{
    dbconn = new Dbconnect("ORCL","aaa","aaa");
    ct = new CommonTookits();
}

public void doExportToExcel()
{
    try
    {
        ResultSet rs = dbconn.doQuery("select * from user_info");
        while(rs.next())
        {
            String [] s = {rs.getString(1),rs.getString(2),rs.getString(3)};
            ct.doExportToExcel("C:/Documents and Settings/ibm/桌面/aaa.xls", "hello", s);
            response.sendRedirect("../mymain.jsp");
        }

    } catch (ClassNotFoundException e)
    {
        e.printStackTrace();
    } catch (SQLException e)
    {
        e.printStackTrace();
    } catch (IOException e)
    {
        e.printStackTrace();
    }
}

[/code]

报错
[quote]
严重: Servlet.service() for servlet UserInfo threw exception
java.lang.NoClassDefFoundError: org/apache/poi/hssf/usermodel/HSSFWorkbook
at demo.tools.CommonTookits.doExportToExcel(CommonTookits.java:61)
at demo.servlet.UserInfo.doExportToExcel(UserInfo.java:204)
at demo.servlet.UserInfo.doBusiness(UserInfo.java:51)
at demo.servlet.UserInfo.doGet(UserInfo.java:224)
[/quote]

61行指向的是workbook = new HSSFWorkbook(),星号注释的那一行

麻烦大家帮我看看啊,谢谢啦
[b]问题补充:[/b]
我POI的包是加了的啊

[img]/upload/attachment/114319/f8bdf431-c1be-3550-99fb-4feb83b14afa.jpg[/img]

[b]问题补充:[/b]
这里所有类都是在一个工程里的,如果没import包就报错了

  • 写回答

2条回答 默认 最新

  • wanghaolovezlq 2009-06-13 21:09
    关注

    晕啊,
    你没把那个包放到webroot/WEB-INF/lib里面啊,
    这样就没更新过去 啊

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)