如何读取csv数据并插入数据库

时间:2018-06-11 11:04:07

标签: oracle jsp

我试图在jsp中读取和插入CSV数据,但我无法读取,对于xls,.xlsx格式我可以读取和插入数据我谷歌它,我发现下面的代码被修改

Reader in1=new FileReader(file1);

Iterable<CSVRecord> records=CSVFormat.RFC4180.withFirstRecordAsHeader().parse(in1);
for(CSVRecord record:records)
{
   String policy_no=record.get("POLICY_NO");
   String ENDORSEMENT_NO=record.get("ENDORSEMENT_NO");
   String GP_EBAO=record.get("GP_EBAO");
   String TOL_PRM=record.get("TOL_PRM");
   String PRODUCT_NAME=record.get("PRODUCT_NAME");
   String POLICY_NUMBER=record.get("POLICY_NUMBER");
   String GP_OF=record.get("GP_OF");
   String DIFF=record.get("DIFF");

}

但它仍然给我 org.apache.poi.POIXMLException:org.apache.poi.openxml4j.exceptions.InvalidFormatException:包应包含内容类型部分[M1.13] 异常< / p>

这是我的阅读excel的代码,任何人都可以帮助我如何阅读和插入csv数据

    <%@page import="org.jfree.data.io.CSV"%>
<%@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"%>
   <%@page import="org.apache.commons.csv.CSVFormat" %>
   <%@page import="org.apache.commons.csv.CSVRecord" %>




     <!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;
    }



        %>   
  <%


 ArrayList CellArrayListHolder=new ArrayList();
 Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@172.18.114.213:1821:ed","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();




%>

<%




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;



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);
   //sb.append(cell.getStringCellValue()+"Null");

 }


    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>

1 个答案:

答案 0 :(得分:0)

我使用opencsv-2.2 jar解决了这个问题 这是我的工作代码,其中详细信息正确插入表

<%@page import="org.jfree.data.io.CSV"%>
<%@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"%>
   <%@page import="au.com.bytecode.opencsv.CSVReader"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
         <%


//  try
//  {
   //try
    //{
 ArrayList CellArrayListHolder=new ArrayList();
 Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@172.18.119.213:1821: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);




fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();




%>
<%
    File file1 = new File(saveFile);
//FileInputStream file_inut=new FileInputStream(file1);

CSVReader reader = new CSVReader(new FileReader(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
    {
        int search_id=0; 
       String[] nextline;
       int i=0;
        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()));
        while((nextline=reader.readNext())!=null)
        {

                    ps.setString(4, nextline[0]);
                    ps.setString(5, nextline[1]);
                    ps.setString(6, nextline[2]);
                    ps.setString(7, nextline[3]);
                    ps.setString(8, nextline[4]);
                    ps.setString(9, nextline[5]);
                    ps.setString(10, nextline[6]);
                    ps.setString(11, nextline[7]);

                    i=ps.executeUpdate();


        }
        if(i>0)
        {
             out.println("<script type=\"text/javascript\">");
   out.println("alert('File uploaded successfully');");
   out.println("</script>");    
        }

    }
}

    %>
    </body>
</html>