-3

我有以下代码用于将数据从 MySQL 数据库写入 excel 文件。

为此,我下载了 org.apache.poi 包并将其放在 Tomcat ROOT 目录中的 classes 文件夹中

<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.util.Hashtable"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<%@ page import="java.lang.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.ResultSet"%>
<%@ page import="java.sql.ResultSetMetaData"%>
<jsp:directive.import></jsp:directive.import>
<jsp:directive.import></jsp:directive.import>
<jsp:directive.import></jsp:directive.import>
<jsp:directive.import></jsp:directive.import>
<jsp:directive.import></jsp:directive.import>
<%
    try{
    String filename="D:/data.xls" ;
    HSSFWorkbook hwb=new HSSFWorkbook();
    HSSFSheet sheet =  hwb.createSheet("new sheet");

    HSSFRow rowhead=   sheet.createRow((short)0);
    rowhead.createCell((short) 0).setCellValue("BID");
    rowhead.createCell((short) 1).setCellValue("BELONGING_TYPE");
    rowhead.createCell((short) 2).setCellValue("BELONGING_TEXT");

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yatin", "root", "root");
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery("Select * from tm_belonging");
    int i=1;
    while(rs.next()){
    HSSFRow row=   sheet.createRow((short)i);
    row.createCell((short) 0).setCellValue(rs.getInt("BID"));
    row.createCell((short) 1).setCellValue(rs.getString("BELONGING_TYPE"));
    row.createCell((short) 2).setCellValue(rs.getString("BELONGING_TEXT"));
    i++;
    }
    FileOutputStream fileOut =  new FileOutputStream(filename);
    hwb.write(fileOut);
    fileOut.close();
    out.println("Your excel file has been generated!");

    } catch ( Exception ex ) {
        System.out.println(ex);
        ex.printStackTrace();

    }
%>

但我收到以下错误

告诉我那里出了什么问题

类型异常报告消息:

描述 服务器遇到一个内部错误 () 阻止它完成这个请求。

例外:

org.apache.jasper.JasperException: /ExcelReport.jsp(11,4) Invalid directive
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:88)
    org.apache.jasper.compiler.Parser.parseDirective(Parser.java:472)
    org.apache.jasper.compiler.Parser.parseFileDirectives(Parser.java:1748)
    org.apache.jasper.compiler.Parser.parse(Parser.java:127)
    org.apache.jasper.compiler.ParserController.doParse(ParserController.java:255)
    org.apache.jasper.compiler.ParserController.parseDirectives(ParserController.java:120)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:180)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:347)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
4

1 回答 1

0

我可以检测到的第一个失败是文件名必须是“D:\data.xls”。Windows 环境中的路径应使用反斜杠(而在 Linux 中不应使用 D:...)

当我将“D:/data.xls”之类的文件名放入我的(正常工作的)代码中时,我在发生的异常报告中得到了相同的描述——我猜,这可能是你的解决方案吗?

于 2012-08-04T16:18:25.487 回答