2012-10-11 55 views
0

我已經編寫了一個代碼來導入一個excel文件,我在這裏獲取數據,並且希望在我的JSP文件中插入數據。我可以通過使用普通代碼而不是使用servlet來實現嗎? 我的代碼上傳Excel文件將正常的java文件值傳遞給jsp文件

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> 
+1

如何將數據傳遞給jsp – Abubakkar

+0

我的問題是如何將向量值傳遞給jsp。 – JavaGuy

+0

我的意思是我們不能在正常的java中使用request.setAttribute函數 – JavaGuy

回答

0

我會建議你不是這樣做的。 調用Servle t是完成此操作的正確方法。

但是,你可以這樣做。

<%@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
它應該是「UploadExcel」。

1

你知道那JSP是在服務器上進行評估,然後將HTML結果傳遞給瀏覽器?

在本質上,你可以有這樣的片段在JSP頁面:

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

,其中將包括在網頁中,模數標記你的csv ...

乾杯,

+1

問題中的代碼,現在看起來,會將csv打印到服務器的控制檯,而不是打印到jsp頁面。使printCellData返回一個字符串(或更好),打印到從jsp傳遞過來的OutputStream。 –

+0

當然,好點的阿森。 –