public class FopServlet extends HttpServlet { public static final String FO_REQUEST_PARAM = "fo"; public static final String XML_REQUEST_PARAM = "xml"; public static final String XSL_REQUEST_PARAM = "xsl"; Logger log = null;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException { if (log == null) { log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN); MessageHandler.setScreenLogger(log); } try { String foParam = request.getParameter(FO_REQUEST_PARAM); String xslParam="/Fop/glossary.xsl"; String xmlParam="/Fop/glossary.xml"; if (foParam != null) { File fofile = new File(foParam); FileInputStream file = new FileInputStream(fofile); renderFO(new InputSource(file), response); } else if ((xmlParam != null) && (xslParam != null)) { XSLTInputHandler input = new XSLTInputHandler(new File(xmlParam), new File(xslParam)); renderXML(input, response); } else { PrintWriter out = response.getWriter(); out.println("<html><head><title>Error</title></head>\n"+ "<body><h1>FopServlet Error</h1><h3>No 'fo' "+ "request param given.</body></html>"); } } catch (ServletException ex) { throw ex; } catch (Exception ex) { throw new ServletException(ex); } }
public void renderFO(InputSource foFile, HttpServletResponse response) throws ServletException { try { ByteArrayOutputStream out = new ByteArrayOutputStream();
response.setContentType("application/pdf");
Driver driver = new Driver(foFile, out); driver.setLogger(log); driver.setRenderer(Driver.RENDER_PDF); driver.run();
再通过Java产生pdf格式的文件jasperreports比较简单,
后面Java产生pdf文件可以参考
http://blog.csdn.net/guoquanyou/archive/2008/12/08/3474795.aspx
中的MyCompile.java文件
import javax.servlet.*;
import javax.servlet.http.*;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.apache.fop.apps.Driver;
import org.apache.fop.apps.Version;
import org.apache.fop.apps.XSLTInputHandler;
import org.apache.fop.messaging.MessageHandler;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
public class FopServlet extends HttpServlet {
public static final String FO_REQUEST_PARAM = "fo";
public static final String XML_REQUEST_PARAM = "xml";
public static final String XSL_REQUEST_PARAM = "xsl";
Logger log = null;
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException {
if (log == null) {
log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
MessageHandler.setScreenLogger(log);
}
try {
String foParam = request.getParameter(FO_REQUEST_PARAM);
String xslParam="/Fop/glossary.xsl";
String xmlParam="/Fop/glossary.xml";
if (foParam != null) {
File fofile = new File(foParam);
FileInputStream file = new FileInputStream(fofile);
renderFO(new InputSource(file), response);
} else if ((xmlParam != null) && (xslParam != null)) {
XSLTInputHandler input =
new XSLTInputHandler(new File(xmlParam),
new File(xslParam));
renderXML(input, response);
} else {
PrintWriter out = response.getWriter();
out.println("<html><head><title>Error</title></head>\n"+
"<body><h1>FopServlet Error</h1><h3>No 'fo' "+
"request param given.</body></html>");
}
} catch (ServletException ex) {
throw ex;
}
catch (Exception ex) {
throw new ServletException(ex);
}
}
public void renderFO(InputSource foFile,
HttpServletResponse response) throws ServletException {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.setContentType("application/pdf");
Driver driver = new Driver(foFile, out);
driver.setLogger(log);
driver.setRenderer(Driver.RENDER_PDF);
driver.run();
byte[] content = out.toByteArray();
response.setContentLength(content.length);
response.getOutputStream().write(content);
response.getOutputStream().flush();
} catch (Exception ex) {
throw new ServletException(ex);
}
}
public void renderXML(XSLTInputHandler input,
HttpServletResponse response) throws ServletException {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.setContentType("application/pdf");
Driver driver = new Driver();
driver.setLogger(log);
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(out);
driver.render(input.getParser(), input.getInputSource());
byte[] content = out.toByteArray();
response.setContentLength(content.length);
response.getOutputStream().write(content);
response.getOutputStream().flush();
} catch (Exception ex) {
throw new ServletException(ex);
}
}
} 代码可以参考下...
其他找找POI的api吧
原来是xls,要是你把xls存为CSV格式。
用jasperreports也是可以作为数据源的。
再通过Java产生pdf格式的文件
xls---->xml---->fo---->pdf现在遇到2个问题:
1.xml---->fo的xsl问题,有没有现成的。
2.fo---->pdf的fop的包问题,我在apache下载了一个fop0.95的包,但里面没有org.apache.fop.apps.driver这个类。谁能提供我个链接。
b.xml--->fo用xsl
c.fo--->pdf用fop
fo-->fop问题也不大,就是还没知道jar包。Driver这个类没找到。挺奇怪,0.95难道变了。xml-->fo经过xsl转换fo问题较大,要一种完全与xls显示格式是一样的xsl。
a.xls--->xml用jxl
b.xml--->fo用xsl
c.fo--->pdf用fop
。