问题描述
我想在 JSP 中读取一个 excel 文件,为此我首先使用 Web 应用程序项目将文件上传到名为 uploads 的 :D 分区中的文件夹中,然后我尝试使用另一个 java 项目读取 excel 上传的文件.两个代码都在工作.这是通过 Web 应用程序项目(JSP 和 SERVLET)上传到特定文件夹的代码:
I want to read an excel file in JSP,for this I first uploaded the file in a folder in the :D partition named uploads using a web application project,and I tried to read the excel uploaded file with an another java projet.The both codes are working.Here it is the code of uploading in a specific folder via web application project(JSP and SERVLET):
图书馆
- commons-fileupload-1.2.2.jar
- commons-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>
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";
}
}
然后我创建了一个新的 JAVA PROJECT (SWING) 并尝试了通过 POI 读取 EXCEL 文件的代码,它也工作得很好,这是代码:
Then I create a new JAVA PROJECT (SWING) and try the code of reading an EXCEL File via POI,It worked as well,here its is the code:
图书馆
- dom4j-1.6.1.jar
- poi-3.10-FINAL-20140208.jar
- poi-ooxml-3.9-20121203.jar
- poi-ooxml-schemas-3.9-20121203.jar
- 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();
}
}
}
问题是如何将这两个代码混合以上传文件,然后将EXCEL中的数据打印到JSP中的表格中????帮帮我,我在这个程序上被困了一个多月
The question is how to mix these two codes to can UPLOAD the file then print the data from EXCEL to a table in JSP ???? help me i'm stuck for over a month on this procedure
推荐答案
after item.write(f);
添加这个
after item.write(f);
add this
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();
从这里继续您的代码.
这篇关于上传+使用POI读取jsp中的excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!