我已經編寫了一個代碼來導入一個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>
如何將數據傳遞給jsp – Abubakkar
我的問題是如何將向量值傳遞給jsp。 – JavaGuy
我的意思是我們不能在正常的java中使用request.setAttribute函數 – JavaGuy