1

私の目的は、ユーザーが入力したユーザー コメントを Excel シートに読み込むことです。そのために、次のように JSP コードを使用しています。

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.*" %>
<%@page import="java.util.*" %>
<%@page import="org.apache.poi.hssf.usermodel.*" %>
<%@page import="org.apache.xerces.parsers.*" %>
<%@page import="org.w3c.dom.*"%>
<%@page import="javax.xml.parsers.*"%>
<%@page import="javax.xml.parsers.DocumentBuilder"%>
<%@page import="javax.xml.parsers.DocumentBuilderFactory"%>
<%@page import="javax.xml.parsers.ParserConfigurationException"%>
<%@page import="org.xml.sax.*"%>
<%@page import="java.sql.*" %>
<%!
    private String getTextContent(Node node) {
        Node child;
        String sContent = node.getNodeValue() != null ? node.getNodeValue() : "";

        NodeList nodes = node.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            child = nodes.item(i);
            sContent += child.getNodeValue() != null ? child.getNodeValue() : "";
            if (nodes.item(i).getChildNodes().getLength() > 0) {
                sContent += getTextContent(nodes.item(i));
            }
        }
        return sContent;
    }
//Function to build Excel file

    public HSSFWorkbook genExcel(String xmlString) {
        try {
//Objects for Excel generation
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFSheet spreadSheet = wb.createSheet("spreadSheet");
            HSSFRow row;
            HSSFCell cell;
HSSFSheet spreadSheet2 = wb.createSheet("About_ClearPeaks_Export2Excel");
            HSSFRow row2;
            HSSFCell cell2;
row2 = spreadSheet2.createRow(1);
 cell2 = row2.createCell((short)1);
 cell2.setCellValue("The ClearPeaks Export2Excel solution allows users to export data to Excel by using Tomcat server.");
row2 = spreadSheet2.createRow(2);
 cell2 = row2.createCell((short)1);
 cell2.setCellValue("Copyright (C) 2009  ClearPeaks");

row2 = spreadSheet2.createRow(4);
 cell2 = row2.createCell((short)1);
 cell2.setCellValue("Please credit ClearPeaks when you publish, use or distribute this solution.");
row2 = spreadSheet2.createRow(6);
 cell2 = row2.createCell((short)1);
 cell2.setCellValue("This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation,"); 
row2 = spreadSheet2.createRow(7);
 cell2 = row2.createCell((short)1);
 cell2.setCellValue("either version 3 of the License, or (at your option) any later version.");
row2 = spreadSheet2.createRow(9);
 cell2 = row2.createCell((short)1);
 cell2.setCellValue("This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;"); 

row2 = spreadSheet2.createRow(10);
 cell2 = row2.createCell((short)1);
 cell2.setCellValue("without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details."); 


row2 = spreadSheet2.createRow(12);
 cell2 = row2.createCell((short)1);
 cell2.setCellValue("For more information please contact ClearPeaks info@clearpeaks.com or call +34 93 272 1546."); 












// Creating a environment for XML parsing and populating the data arrays

            DOMParser parser = new DOMParser();
            parser.parse(new InputSource(new java.io.StringReader(xmlString)));
            Document doc = parser.getDocument();

            NodeList nodeList = doc.getElementsByTagName("row");
            NodeList nodeListColumn = doc.getElementsByTagName("column");

// Loop to fill in data into workbook objects
            int numcol = 0;
            for (int i = 0; i < nodeList.getLength(); i++) {
                row = spreadSheet.createRow(i);
                for (int j = 0; j < nodeListColumn.getLength() / nodeList.getLength(); j++) {
                    cell = row.createCell((short) j);
                    cell.setCellValue(getTextContent(nodeListColumn.item(numcol)));
                    numcol += 1;
                }
            }

            Calendar calendar = Calendar.getInstance();
            return wb;
        } catch (java.lang.Exception e) {
            System.out.println("Exception: " + e.getMessage());
            return null;
        }
    }

%>
<%
// Get data from URL and create new file to hold data
                request.setCharacterEncoding("UTF-8");
        BufferedReader urlLine = request.getReader();
        String exported = (String) urlLine.readLine();
    while (urlLine.ready()) {
    exported += (String) urlLine.readLine();
}

        response.setContentType("text/xml");
        response.setHeader("Content-Disposition", "attachment;filename=unknown.xls");
        PrintWriter rsp = response.getWriter();

// Call method to read xml and load data into excel file
        HSSFWorkbook wb = genExcel(exported);
// Write tempo xls file
        String realPath = request.getRealPath("/");
        FileOutputStream fileOut = new FileOutputStream(realPath + "temp.xls");
        wb.write(fileOut);
        fileOut.close();

// Building xml to return url to file
        String serverName = request.getHeader("host");
        String sXML = "<data>";
        sXML = sXML + "<variable name=\"URLDummy\">";
        sXML = sXML + "<row>";
        sXML = sXML + "<column>http://" + serverName + "/ExportToExcelv3/temp.xls</column>";
        sXML = sXML + "</row>";
        sXML = sXML + "</variable>";
        sXML = sXML + "</data>";
        rsp.write(sXML);

%>

しかし、この JSP を実行すると、wb.write(fileOut); 行でエラーが発生します。どんな助けでも大歓迎です。

4

1 に答える 1