我有以下代码将数据从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();
}
%>
但是我收到以下错误
告诉我那里有什么问题
输入异常报告消息:
description服务器遇到内部错误(),导致无法完成此请求。
例外:
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)
答案 0 :(得分:0)
我可以检测到的第一个失败是文件名必须是“D:\ data.xls”。 Windows环境中的路径应该使用反斜杠(在Linux中它不应该使用D:...)
当我将“D:/data.xls”这样的文件名放到我的(正确工作的)代码中时,我在发生的异常报告中得到相同的描述 - 我想,这可能是你的解决方案吗?