我编写了一个代码来导入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>
答案 0 :(得分:1)
您是否意识到在服务器上评估JSP,然后将HTML结果传递给浏览器?
从本质上讲,您可以在JSP页面中拥有此片段:
<% printCellDataToConsole( ReadCSV( "file.csv" ) ) %>
将在网页中包含您的csv,模数标记...
干杯,
答案 1 :(得分: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”。