About the JSP servlet writes the Excel table contents into the database and exports the database to Excel. A

Source: Internet
Author: User
Tags readline

Turn from: http://blog.chinaunix.net/uid-28794959-id-3773177.html

From Excel to database, introduce Poi.jar
JSP is as follows

Click (here) to collapse or open <form enctype= "Multipart/form-data" Name=testform method=post action=testaction>
<table>
<tr>
<td><font size=2> Bulk Upload: </font><input type= "file" name= "test" size= "ten" ><br></td >
<td><input type= "Submit" name= "Batch upload" size= "" value= "Batch upload" ><br></td></tr></table ><br>
</form> servlet is as follows

Click (here) to collapse or open package control;

Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.PrintWriter;
Import Java.io.RandomAccessFile;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Testaction extends HttpServlet {

/**
*
*/
Private static final long serialversionuid = 1L;

public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {


}

/**
* The DoPost method of the servlet. <br>
*
* This is called when a form, has its tag value, equals to post.
*
* @param request the request send by the client to the server
* @param response The response send by the server to the client
* @throws servletexception If an error occurred
* @throws IOException If an error occurred
*/
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Receive the file name of the temporary file in the uploaded file content
String tempfilename = new String ("TempFileName");
The Tempfile object points to a temporary file
File Tempfile = new file ("d:/" +tempfilename);
The OutputFile file output stream points to this temporary file
FileOutputStream outputstream = new FileOutputStream (tempfile);
Get all the data submitted by customer service
InputStream filesourcel = Request.getinputstream ();
Write the resulting client data to a temporary file
byte b[] = new byte[1000];
int n;
while ((N=filesourcel.read (b))!=-1) {
Outputstream.write (B,0,n);
}

Turn off the output stream and the input stream
Outputstream.close ();
Filesourcel.close ();

The Randomfile object points to a temporary file
Randomaccessfile randomfile = new Randomaccessfile (tempfile, "R");
Read the first row of data from a temporary file
Randomfile.readline ();
Reads the second row of data in the temporary file, which contains the path and file name of the file
String FilePath = Randomfile.readline ();
System.out.println (FilePath);
Get file name
int position = Filepath.lastindexof (' \ n ');
codetostring codetostring = new codetostring ();
String filename = codetostring.codestring (filepath.substring (Position,filepath.length ()-1));
Relocate read file pointer to file header
Randomfile.seek (0);
Gets the position of the four-line carriage return, which is the starting position of the uploaded file data
Long forthenterposition = 0;
int forth = 1;
while ((N=randomfile.readbyte ())!=-1&& (forth<=4)) {
if (n== ' \ n ') {
Forthenterposition = Randomfile.getfilepointer ();
forth++;
}
}

Create a directory for uploaded files
File FileUpload = new file ("F:/myeclipse/manager/webroot/file", "upLoad");
Fileupload.mkdir ();
The SaveFile object points to the file to save
File SaveFile = new file ("F:/myeclipse/manager/webroot/file/upload", filename);
Randomaccessfile randomaccessfile = new Randomaccessfile (SaveFile, "RW");
Find the ending position of the uploaded file data, that is, line fourth
Randomfile.seek (Randomfile.length ());
Long endposition = Randomfile.getfilepointer ();
int j = 1;
while ((endposition>=0) && (j<=4)) {
endposition--;
Randomfile.seek (endposition);
if (randomfile.readbyte () = = ' \ n ') {
j + +;
}
}

Writes data to the file to be saved from the beginning of the uploaded file data to the end position
Randomfile.seek (forthenterposition);
Long startpoint = Randomfile.getfilepointer ();
while (startpoint<endposition) {
Randomaccessfile.write (Randomfile.readbyte ());
StartPoint = Randomfile.getfilepointer ();
}
Randomaccessfile.close ();
Randomfile.close ();
Tempfile.delete ();

Testexcel t=new testexcel ();
T.add ();


}




Real core code, analyzing Excel

Click (here) to collapse or open package control;

Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.