Уважаемый @ashleedawg. Здесь я нашел свое решение, которое, как я надеюсь, будет полезным для кого-то.
Обработка нулевого значения
<%@page import="org.apache.commons.fileupload.FileItemStream"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.omg.PortableServer.Servant"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="java.nio.file.Paths"%>
<%@ 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.hssf.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="com.oreilly.servlet.MultipartRequest" %>
<%@page import="org.apache.poi.xssf.usermodel.*"%>
<%@page import="org.apache.poi.xssf.usermodel.XSSFWorkbook"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<style>
.blueText {
color: blue;
}
</style>
</head>
<body>
<%!
public static boolean isRowEmpty(Row row){
int firstCol = row.getFirstCellNum();
for(int cnt = 0; cnt<4 ; cnt++){
Cell cell = row.getCell(firstCol+cnt);
if(cell!=null && cell.getCellType()!=Cell.CELL_TYPE_BLANK){
return false;
}
}
return true;
}
%>
<%
// try
// {
//try
//{
ArrayList CellArrayListHolder=new ArrayList();
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@172.18.114.213:1721:godb","xe","se");
String contentType = request.getContentType();
if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
{
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
String saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
//out.println(saveFile);
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
FileOutputStream fileOut = new FileOutputStream(saveFile);
//out.println("<script type=\"text/javascript\">");
// out.println("alert('You are successfully Login at:" + saveFile + "')");
// out.println("location='EmployeeLogin.html';");
// out.println("</script>");
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%>
<%
//PreparedStatement psmnt = null;
//FileInputStream fis;
//try {
//Class.forName("com.mysql.jdbc.Driver").newInstance();
//connection = DriverManager.getConnection(connectionURL, "root", "root");
File file1 = new File(saveFile);
FileInputStream file_inut=new FileInputStream(file1);
if(saveFile.indexOf(".")>0)
{
saveFile=saveFile.substring(0,saveFile.lastIndexOf("."));
}
String query="Select File_name,to_char(FILE_UPLOAD_DATE,'DD-MON-YYYY') from DYNAMIC_INSERT where File_name='"+saveFile+"'";
Statement st1=con.createStatement();
ResultSet rs=st1.executeQuery(query);
if(rs.next())
{
String file_name=rs.getString(1);
String FILE_UPLOAD_DATE=rs.getString(2);
out.println("<script type=\"text/javascript\">");
out.println("alert('File name " + file_name + " already exists File upload Date is "+FILE_UPLOAD_DATE+"')");
out.println("location='xlsUpload.html';");
out.println("</script>");
}
else
{
XSSFWorkbook workbook=new XSSFWorkbook(file_inut);
XSSFSheet firstsheet=workbook.getSheetAt(0);
Iterator<Row> iterator=firstsheet.rowIterator();
int count=0;
int search_id=0;
XSSFRow nextrow;
Row row;
//nextrow=firstsheet.createRow((short)1);
//nextrow.createCell(0).setCellValue("Null");
// nextrow.createCell(1);
StringBuffer sb=new StringBuffer();
while(iterator.hasNext())
{
row =(XSSFRow)iterator.next();
**for(int col=0;col<row.getLastCellNum();col++)
{
Cell cell=row.getCell(col, Row.CREATE_NULL_AS_BLANK);
}**// add this line
ArrayList rowarraylist=new ArrayList();
Iterator<Cell> cellIterator=row.cellIterator();
while(cellIterator.hasNext())
{
XSSFCell cell=(XSSFCell)cellIterator.next();
rowarraylist.add(cell);
}
CellArrayListHolder.add(rowarraylist);
}
//out.println(CellArrayListHolder);
ArrayList rowarraylist=null;
//PreparedStatement st=con.prepareStatement("insert into DYNAMIC_INSERT values(?,?,?,?,?,?,?,?,?,?,?)");
PreparedStatement ps=con.prepareStatement("insert into DYNAMIC_INSERT values(?,?,?,?,?,?,?,?,?,?,?)");
ps.setString(1, saveFile);
ps.setInt(2, search_id);
ps.setDate(3,new java.sql.Date(System.currentTimeMillis()));
for(int i=1;i<CellArrayListHolder.size();i++)
{
rowarraylist=(ArrayList)CellArrayListHolder.get(i);
//st.setString(1, file_name);
ps.setString(4, (rowarraylist.get(0)).toString());
ps.setString(5, (rowarraylist.get(1)).toString());
ps.setString(6, (rowarraylist.get(2)).toString());
ps.setString(7, (rowarraylist.get(3)).toString());
ps.setString(8, (rowarraylist.get(4)).toString());
ps.setString(9, (rowarraylist.get(5)).toString());
ps.setString(10, (rowarraylist.get(6)).toString());
ps.setString(11, (rowarraylist.get(7)).toString());
//ps.setString(10, (rowarraylist.get(6)).toString());
/*st.setString(6, rowarraylist.get(2).toString());
st.setString(7, rowarraylist.get(3).toString());
st.setString(8, rowarraylist.get(4).toString());
st.setString(9, rowarraylist.get(5).toString());
st.setString(10, rowarraylist.get(6).toString());
st.setString(11, rowarraylist.get(7).toString());*/
//st.executeUpdate();
count=ps.executeUpdate();
}
if(count>0)
{
out.println("<script type=\"text/javascript\">");
out.println("alert('File uploaded successfully');");
out.println("location='xlsUpload.html';");
out.println("</script>");
}
}
}
%>
</body>
</html>
Поиск файла Excel: .html
<form action="Retrieve_data.jsp" method="post">
<center>
<table border="2" >
<tr><center><td colspan="2"><p align="center"><B>DOWNLOAD THE FILE</B><center></td></tr>
<tr><td><b>Enter File Name</b>
</td>
<td><input type="text" name="txtfilename"><br></td></tr>
<tr><td colspan="2">
<p align="center"><br><input type="submit" value="Download Excel" name="btnsubmit"></p></td></tr>
</table>
</form>
Retrieve.jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form method="post" action="Download_excel.jsp">
<!-- <p align="right"><br><input type="submit" value="Download Excel" name="btnsubmit"></p></td></tr>-->
<table border="1">
<tr>
<td>FILE_NAME</td>
<td>FILE_UPLOAD_DATE</td>
<td>POLICY_NO</td>
<td> ENDORSEMENT_NO</td>
<td> GP_EBAO</td>
<td> TOL_PRM</td>
<td> PRODUCT_NAME</td>
<td> POLICY_NUMBER</td>
<td> GP_OF</td>
<td>DIFF</td>
</tr>
<%
String txtfilename=request.getParameter("txtfilename");
String btnsubmit=request.getParameter("btnsubmit");
if(txtfilename==null ||txtfilename.trim().isEmpty())
{
out.println("<html><body><script type=\"text/javascript\">");
out.println("confirm('first name field cannot be blank');");
out.println("location='xlsUpload.html';");
out.println("</script></body></html>");
}
else
if(btnsubmit!=null)
{
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename="+txtfilename+".xls");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@172.18.114.213:1721:godb","xe","se");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from DYNAMIC_INSERT where file_name='"+txtfilename+"'");
while(rs.next())
{
%>
<tr>
<td><%=rs.getString("FILE_NAME")%></td>
<td><%=rs.getString("FILE_UPLOAD_DATE")%></td>
<td><%=rs.getString("POLICY_NO")%></td>
<td><%=rs.getString("ENDORSEMENT_NO")%></td>
<td><%=rs.getString("GP_EBAO")%></td>
<td><%=rs.getString("TOL_PRM")%></td>
<td><%=rs.getString("PRODUCT_NAME")%></td>
<td><%=rs.getString("POLICY_NUMBER")%></td>
<td><%=rs.getString("GP_OF")%></td>
<td><%=rs.getString("DIFF")%></td>
</tr>
<%
}
}
%>
</table>
</form>
</body>
</html>
Download.jsp:
<%@page import="org.apache.commons.fileupload.FileItemStream"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.omg.PortableServer.Servant"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="java.nio.file.Paths"%>
<%@ 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.hssf.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="com.oreilly.servlet.MultipartRequest" %>
<%@page import="org.apache.poi.xssf.usermodel.*"%>
<%@page import="org.apache.poi.xssf.usermodel.XSSFWorkbook"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String btnsubmit=request.getParameter("btnsubmit");
if(btnsubmit!=null)
{
String FILE_NAME[]=request.getParameterValues("FILE_NAME");
String FILE_UPLOAD_DATE []=request.getParameterValues("FILE_UPLOAD_DATE");
String POLICY_NO []=request.getParameterValues("POLICY_NO");
String ENDORSEMENT_NO[]=request.getParameterValues("ENDORSEMENT_NO");
String GP_EBAO []=request.getParameterValues("GP_EBAO");
String TOL_PRM[]=request.getParameterValues("TOL_PRM");
String PRODUCT_NAME []=request.getParameterValues("PRODUCT_NAME");
String POLICY_NUMBER[]=request.getParameterValues("POLICY_NUMBER");
String GP_OF []=request.getParameterValues("GP_OF");
String DIFF[]=request.getParameterValues("DIFF");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@172.18.114.213:1821:godb","sbigi_gtrg","Spacess");
//String query="Select File_name from DYNAMIC_INSERT where File_name='"+FILE_NAME+"'";
// Statement st1=con.createStatement();
// ResultSet rs=st1.executeQuery(query);
// if(rs.next())
// {
//String file_name1=rs.getString(1);
//String file_store="D:\\file_download\\"+FILE_NAME+".xls";
String file_store="D:\\GODBFILES\\File_upload\\File.xls";
XSSFWorkbook xwb=new XSSFWorkbook();
XSSFSheet sheet=xwb.createSheet(file_store+".xls");
// String file_store=sheet;
XSSFRow rowhead=sheet.createRow((short)0);
rowhead.createCell((short)0).setCellValue("FILE_NAME");
rowhead.createCell((short)1).setCellValue("FILE_UPLOAD_DATE");
rowhead.createCell((short)2).setCellValue("POLICY_NO");
rowhead.createCell((short)3).setCellValue("ENDORSEMENT_NO");
rowhead.createCell((short)4).setCellValue("GP_EBAO");
rowhead.createCell((short)5).setCellValue("TOL_PRM");
rowhead.createCell((short)6).setCellValue("PRODUCT_NAME");
rowhead.createCell((short)7).setCellValue("POLICY_NUMBER");
rowhead.createCell((short)8).setCellValue("GP_OF");
rowhead.createCell((short)9).setCellValue("DIFF");
for(int i=0;i<FILE_NAME.length;i++)
{
int j=i+1;
XSSFRow row=sheet.createRow((short)j);
row.createCell((short) 0).setCellValue(FILE_NAME[i]);
row.createCell((short) 1).setCellValue(FILE_UPLOAD_DATE[i]);
row.createCell((short) 2).setCellValue(POLICY_NO[i]);
row.createCell((short) 3).setCellValue(ENDORSEMENT_NO[i]);
row.createCell((short) 4).setCellValue(GP_EBAO[i]);
row.createCell((short) 5).setCellValue(TOL_PRM[i]);
row.createCell((short) 6).setCellValue(PRODUCT_NAME[i]);
row.createCell((short) 7).setCellValue(POLICY_NUMBER[i]);
row.createCell((short) 8).setCellValue(GP_OF[i]);
row.createCell((short) 9).setCellValue(DIFF[i]);
}
FileOutputStream fileout=new FileOutputStream(file_store);
xwb.write(fileout);
out.println("<script type=\"text/javascript\">");
out.println("alert('File download successfully');");
out.println("location='xlsUpload.html';");
out.println("</script>");
// }
}
%>
</body>
</html>