这是我的索引文件
<form action="DataImport" method="post>"
<input name="filename" type="file" size="20">
<input type="submit" value="Import">
DataImport.java(Servlet的)
public class DataImport extends HttpServlet
{
private static final long serialVersionUID = 1L;
public DataImport()
{
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
}
@SuppressWarnings("rawtypes")
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
try
{
String file = request.getParameter("file");
String fileName1=""+file+"";
ArrayList dataHolder = read.readExcelFile(fileName1);
Connection con = EmployeeDao1.con11();
System.out.println("Connection :"+con);
@SuppressWarnings("unused")
Statement sql_statement = con.createStatement();
String jdbc_insert_sql = "INSERT INTO emp4"
+ "(Id,name,salary) VALUES"
+ "(?,?,?)";
PreparedStatement ps = con.prepareStatement(jdbc_insert_sql);
int count=0;
ArrayList cellStoreArrayList=null;
for(int i=1;i<dataHolder.size();i++)
{
cellStoreArrayList=(ArrayList)dataHolder.get(i);
ps.setString(1,((HSSFCell)cellStoreArrayList.get(0)).toString());
ps.setString(2,((HSSFCell)cellStoreArrayList.get(1)).toString());
ps.setString(3,((HSSFCell)cellStoreArrayList.get(2)).toString());
count= ps.executeUpdate();
}
if(count>0)
{
for (int j=1;j < dataHolder.size(); j++)
{
cellStoreArrayList=(ArrayList)dataHolder.get(j);
System.out.println(((HSSFCell)cellStoreArrayList.get(0)).toString());
System.out.println(((HSSFCell)cellStoreArrayList.get(1)).toString());
System.out.println(((HSSFCell)cellStoreArrayList.get(2)).toString());
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
这是我的read.java类来读取excel文件
public class read {
@SuppressWarnings({"unchecked", "rawtypes"})
public static ArrayList readExcelFile(String fileName) {
ArrayList cellArrayLisstHolder = new ArrayList();
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("D:\\LOGA\\Book1.xlsx"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while (rows.hasNext()) {
HSSFRow row = (HSSFRow) rows.next();
Iterator cells = row.cellIterator();
ArrayList cellStoreArrayList = new ArrayList();
while (cells.hasNext()) {
HSSFCell cell = (HSSFCell) cells.next();
cellStoreArrayList.add(cell);
int cellType = cell.getCellType();
if (cellType == HSSFCell.CELL_TYPE_STRING) {
HSSFRichTextString strData = cell.getRichStringCellValue();
System.out.println("String data=" + strData.getString());
} else if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
double data = cell.getNumericCellValue();
System.out.println("Numeric data=" + data);
}
}
cellArrayLisstHolder.add(cellStoreArrayList);
}
} catch (Exception e) {
e.printStackTrace();
}
return cellArrayLisstHolder;
}
}
当我尝试执行servlet时,它显示以下错误
SEVERE: Servlet.service() for servlet DataImport threw exception
java.lang.ClassNotFoundException: org.apache.poi.poifs.filesystem.POIFSFileSystem
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1358)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1204)
at pack.DataImport.doPost(DataImport.java:47)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at java.lang.Thread.run(Unknown Source)
任何人都可以解决这个问题吗?
并且我已经包含了我的所有poi和sql jar文件,excel文件(Book1.xslx)仅在D:\\LOGA
...
答案 0 :(得分:1)
您似乎忘记在CLASSPATH中包含POI库(例如,您的Web应用程序未引用它)。您可以在WEB-INF / lib下(在WAR归档文件中或以展开的目录格式)中包含此lib,也可以将其放在app server / web server CLASSPATH中的其他位置
答案 1 :(得分:0)
<form action="DataImport" method="post>"
这里有一个小错误
引号超出表单标记
method="post>"
只需改变它
<form action="DataImport" method="post">
并且还改变了这个
的servlet条目String file = request.getParameter("file");
到这个
String file = request.getParameter("filename");
答案 2 :(得分:0)
这是我的解决方案......工作正常 file upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<jsp:useBean id="excel" class="com.ImportExcel"/>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="index.jsp" method="post" enctype="multipart/form-data">
<input type="file" name="filename">
<input type="submit" value="Submit">
</form>
</body>
</html>
的index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@ page import ="java.util.Date" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.io.FileNotFoundException" %>
<%@ page import ="java.io.IOException" %>
<%@ page import ="java.util.Iterator" %>
<%@ page import ="java.util.ArrayList" %>
<%@ page import="org.apache.poi.xssf.usermodel.*" %>
<%@ page import ="org.apache.poi.hssf.usermodel.HSSFCell" %>
<%@ page import ="org.apache.poi.hssf.usermodel.HSSFRow" %>
<%@ page import ="org.apache.poi.hssf.usermodel.HSSFSheet" %>
<%@ page import ="org.apache.poi.hssf.usermodel.HSSFWorkbook" %>
<%@ page import ="org.apache.poi.poifs.filesystem.POIFSFileSystem" %>
<%@ page import="org.apache.poi.ss.usermodel.Cell" %>
<%@ page import ="org.apache.poi.ss.usermodel.Row"%>
<%@ page import="org.apache.poi.ss.usermodel.Sheet" %>
<%@ page import="org.apache.poi.ss.usermodel.Workbook" %>
<%@ page import ="org.apache.poi.xssf.usermodel.XSSFWorkbook" %>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
ArrayList cellArrayListHolder = new ArrayList ();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test","root","root");
String Path="upload";
String absolutepath=getServletContext().getRealPath(Path);
MultipartRequest mr=new MultipartRequest(request,absolutepath);
String filename=mr.getOriginalFileName("filename");
FileInputStream myinput = new FileInputStream(new File(absolutepath+"/"+filename));
Workbook workbook = new XSSFWorkbook(myinput);
/*Create a workbook using myfilesystem */
/* Get the first sheet from workbook */
Sheet firstSheet = workbook.getSheetAt(0);
/*We need to something to iterate through the cell */
Iterator<Row> iterator = firstSheet.iterator();
while (iterator.hasNext())
{
XSSFRow nextRow =(XSSFRow)iterator.next();
ArrayList rowarrylist=new ArrayList();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
XSSFCell cell = (XSSFCell)cellIterator.next();
rowarrylist.add(cell);
}
cellArrayListHolder.add(rowarrylist);
}
out.println(cellArrayListHolder);
ArrayList rowarrylist=null;
PreparedStatement st=con.prepareStatement("INSERT INTO export VALUES (?,?,?,?)");
for(int i=1;i<cellArrayListHolder.size();i++)
{
rowarrylist=(ArrayList)cellArrayListHolder.get(i);
st.setString(1,rowarrylist.get(0)
.toString());
st.setString(2,rowarrylist.get(1).toString());
st.setString(3,rowarrylist.get(2).toString());
st.setString(4,rowarrylist.get(3).toString());
st.executeUpdate();
}
}
catch(Exception e)
{
e.getMessage();
out.println(e.getMessage());
}
%>
</body>
</html>