使用POI上传+读取jsp中的excel文件

Nih*_* KH 8 excel file-upload fileinputstream apache-poi apache-commons-fileupload

我想在JSP中读取一个excel文件,为此我首先使用Web应用程序项目将文件上传到名为uploads的:D分区的文件夹中,我尝试用另一个java projet 读取excel上传文件.这两个代码都在工作.这是通过Web应用程序项目(JSP和SERVLET)在特定文件夹中上传的代码:

图书馆

  1. 公地文件上传-1.2.2.jar
  2. 公地IO-2.1.jar

的index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Upload File</title>
</head>
<body>
<form action="UploadFile" method="post" enctype="multipart/form-data">
Select File : <input type="file" name="filetoupload">
<br/>
<input type="submit" value="Upload File">
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

UploadServlet.java(Servlet的)

import java.io.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;

public class UploadFile extends HttpServlet{

String saveFile="D:/upload/"; 

protected void processRequest(...)throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

try {
boolean ismultipart=ServletFileUpload.isMultipartContent(request);
if(!ismultipart){

}else{
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;

try{

items = upload.parseRequest(request);
}catch(Exception e){
}
Iterator itr = items.iterator();
while(itr.hasNext()){
FileItem item = (FileItem)itr.next();
if(item.isFormField()){

}else{
String itemname = item.getName();
if((itemname==null || itemname.equals(""))){
continue;
}
String filename = FilenameUtils.getName(itemname);
File f = checkExist(filename);
item.write(f);
}
}
}

}catch(Exception e){

}
finally {
out.close();
}
}

private File checkExist(String fileName) {
File f = new File(saveFile+"/"+fileName);

if(f.exists()){
StringBuffer sb = new StringBuffer(fileName);
sb.insert(sb.lastIndexOf("."),"-"+new Date().getTime());
f = new File(saveFile+"/"+sb.toString());
}
return f;
}

@Override
protected void doGet(...)throws ServletException, IOException {
processRequest(request, response);

}

@Override
protected void doPost(...)throws ServletException, IOException {
processRequest(request, response);
}

@Override
public String getServletInfo() {
return "Short description";
}

}
Run Code Online (Sandbox Code Playgroud)

然后我创建一个新的JAVA PROJECT(SWING)并尝试通过POI读取EXCEL文件的代码,它也可以,这里是代码:

图书馆

  1. dom4j的-1.6.1.jar
  2. POI-3.10-FINAL-20140208.jar
  3. POI-OOXML-3.9-20121203.jar
  4. POI-OOXML-架构 - 3.9-20121203.jar
  5. XMLBeans的-2.3.0.jar

JavaApplication.java

import java.io.*;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class JavaApplication{
public static void main(String[] args){
try{
FileInputStream file;
file = new FileInputStream(new File("D:\\upload\\total.xlsx"));

//Create Workbook instance holding reference to .xlsx file
XSSFWorkbook workbook = new XSSFWorkbook(file);

//Get first/desired sheet from the workbook
XSSFSheet sheet = workbook.getSheetAt(0);

//Iterate through each rows one by one
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()){
Row row = rowIterator.next();
//For each row, iterate through all the columns
Iterator<Cell> cellIterator = row.cellIterator();

while (cellIterator.hasNext()){
Cell cell = cellIterator.next();

//Check the cell type and format accordingly
switch (cell.getCellType()){
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "t");
break;
}
}
System.out.println("");
}
file.close();
} 
catch (Exception e) 
{
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)

问题是如何将这两个代码混合到UPLOAD文件然后将数据从EXCEL打印到JSP中的表???? 帮助我,我在这个程序上被困了一个多月

Cha*_*ddy 4

item.write(f);添加此后

InputStream inputStream= new ByteArrayInputStream(IOUtils.toByteArray(new FileInputStream(f)));

Workbook wb = WorkbookFactory.create(inputStream);
Sheet mySheet = wb.getSheetAt(0);
Iterator<Row> rowIter = mySheet.rowIterator();
rowIter.next();
Run Code Online (Sandbox Code Playgroud)

从这里继续您的代码。