0

我编写了一个代码来导入一个获取数据的 excel 文件,并且我想在我的 JSP 文件中插入数据。我可以通过使用普通代码而不是使用 servlet 来做到这一点吗?我上传excel文件的代码是

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Vector;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.RestoreAction;

import org.apache.poi.hssf.record.formula.functions.Goto;
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 uploadexcel {
public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
      System.out.println("Please enter a filename with extention to upload");

    String fileName = "C:\\"+scanner.nextLine();
    Vector dataHolder = ReadCSV(fileName);
    printCellDataToConsole(dataHolder);
} 

private static void printCellDataToConsole(Vector dataHolder) {
    // TODO Auto-generated method stub
    for(int i=0;i<dataHolder.size();i++)
    {
        Vector column=(Vector)dataHolder.elementAt(i);
        for (int j = 0; j < column.size(); j++) {
            HSSFCell myCell = (HSSFCell) column.elementAt(j);
            String stringCellValue = myCell.toString();

            System.out.print(stringCellValue + "\t");
    }
    System.out.println();
    }
}

private static Vector ReadCSV(String fileName) {
    // TODO Auto-generated method stub
     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 column = new Vector();
                 while (cellIter.hasNext()) {
                         HSSFCell myCell = (HSSFCell) cellIter.next();
                         column.addElement(myCell);
                 }
                 cellVectorHolder.addElement(column);
         }
 } catch(IOException ie)
 {
     System.err.println("Please enter a valid input");

 }
     catch (Exception e) {
         e.printStackTrace();
 }
 return cellVectorHolder;
}

}

我的jsp文件是

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>NAMES ON THE LIST:</title>
</head>
<body>
names: <input type="text" name="firstname"><br>
</body>
</html>
4

2 回答 2

1

您确实意识到在服务器上评估 JSP,然后将 HTML 结果传递给浏览器?

本质上,您可以在 JSP 页面中包含以下片段:

<%  printCellDataToConsole( ReadCSV( "file.csv" ) ) %>

这将在网页中包含您的 csv,模数标记...

干杯,

于 2012-10-11T10:48:19.207 回答
0

我建议你不要这样做。调用Servlet 是完成此任务的正确方法。

但是,你可以这样做。

<%@page import="java.util.Vector"%>

<%
 Vector oRetVal = new Vector();
 try{
       oRetVal = uploadexcel.ReadCSV("C:\\test.csv"); // Just an example
    }catch(Exception e){

        e.printStackTrace();
    }
%>

在 JSP 中迭代 oRetVal。

注意:与问题无关:阅读Java Naming Conventions
它应该是“上传Excel”。

于 2012-10-12T07:10:41.523 回答