0

我正在开发一个 .xls 阅读器,它获取 .xls 文件并读取它,并将输出作为纯文本提供,但我在将文件路径放入 servlet 时遇到了一些问题......我正在使用 poi.jar 来阅读.xls 文件 ..

.jsp

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Upload Form</title>

    </head>


    <body bgcolor="lightblue">
 <table id='table1' align="center" >
                <tr>

                </tr></table><br/><br/><br/>  
            <br/>

            <table border="1" width="50%" bgcolor="#C2DFFF">
                <tr>
                    <td width="100%">
                        <form action="writeExcel" method="post" enctype="multipart/form-data">

                            <h2 align="center">Welcome</h2>
  <center>

      <input type="file" name="photo"/>
                                <input type="file" name="hem"/>
                                <br/><br/>
                                <input type="submit" value="Save"/>
                                &nbsp;
                                <input type="button" value="Logout" name="logout" onclick="goToURL();" />
                                <br/>
                                <br/>

                            </center>     
                        </form>
                    </td>
                </tr>
</table>

            <br/><br/>


    </body>
</html>

.java

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class writeExcel extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String fileName = request.getParameter("hem");



       Vector dataHolder = ReadCSV(fileName);
        printCellDataToConsole(dataHolder);
    }

   public static Vector ReadCSV(String fileName) {
        Vector cellVectorHolder = new Vector();

        try {
            FileInputStream myInput = new FileInputStream(fileName);

            POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

            HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

            HSSFSheet mySheet = myWorkBook.getSheetAt(0);

            Iterator rowIter = mySheet.rowIterator();

            while (rowIter.hasNext()) {
                HSSFRow myRow = (HSSFRow) rowIter.next();
                Iterator cellIter = myRow.cellIterator();
                Vector cellStoreVector = new Vector();
                while (cellIter.hasNext()) {
                    HSSFCell myCell = (HSSFCell) cellIter.next();
                    cellStoreVector.addElement(myCell);
                }
                cellVectorHolder.addElement(cellStoreVector);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cellVectorHolder;
    }

    private static void printCellDataToConsole(Vector dataHolder) {

        for (int i = 0; i < dataHolder.size(); i++) {
            Vector cellStoreVector = (Vector) dataHolder.elementAt(i);
            for (int j = 0; j < cellStoreVector.size(); j++) {
                HSSFCell myCell = (HSSFCell) cellStoreVector.elementAt(j);
                String stringCellValue = myCell.toString();
                System.out.print(stringCellValue + "\t");
            }
            System.out.println();
        }
    }
}
4

1 回答 1

0

@AVD:fileName 是否代表文件/文档的绝对路径?如果没有,则设置/获取真实(绝对)路径。

@Nitin Sharma:谢谢AVD ..当我使用finename =“C:\ a.xls”时工作正常但我试图通过文件从.jsp获取路径,它只给出文件名而不是完整路径

@AVD:您必须将上传的文件保存在您的网络应用根目录(或子文件夹)下,或者使用上传文件的 InputStream(Servlet 3.0 部分或 Apache fileupload api)对象而不是文件名。

@Nitin Sharma:我不想上传文件...我只想获取该文件的完整路径..

@AVD:没什么好说的!没有选择。(不要保存上传的文件,而是使用上传文件的流)。

@Nitin Sharma:我使用了它,但是 poi 使​​用 File 类对象来读取文件..但是 Apache fileupload api 使用 FileItem 类对象来读取或获取 .xls 文件的路径...而且我无法将 FileItem 类型转换为文件


看一下POIFSFileSystemclass,它带InputStream参数,你可以通过FileItem.getInputStream()(Commons upload)获取。

POIFSFileSystem myFileSystem = new POIFSFileSystem(fileItem.getInputStream());
于 2012-06-26T07:34:01.323 回答