关于 jsp servlet 将Excel表格内容写进数据库,将数据库导出到Excel中。(一)

这篇博客展示了如何使用jsp servlet实现Excel表格内容的上传并写入数据库,以及从数据库导出数据到Excel文件。通过文件流处理,将客户端上传的Excel临时文件读取并保存,然后进行数据操作。
摘要由CSDN通过智能技术生成
EXCEL到数据库,引入poi.jar
jsp如下

点击(此处)折叠或打开

  1. <form enctype="multipart/form-data" name=testform method=post action=Testaction>
  2.   <table>
  3.     <tr>
  4.        <td><font size=2>批量上传:</font><input type="file" name="test" size="10"><br></td>
  5.       <td><input type="submit" name="批量上传" size="10"value="批量上传"><br></td></tr></table><br>
  6.       </form>
Servlet如下

点击(此处)折叠或打开

  1. package control;

  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.PrintWriter;
  7. import java.io.RandomAccessFile;

  8. import javax.servlet.ServletException;
  9. import javax.servlet.http.HttpServlet;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;

  12. public class Testaction extends HttpServlet {

  13.     /**
  14.      *
  15.      */
  16.     private static final long serialVersionUID = 1L;

  17.     public void doGet(HttpServletRequest request, HttpServletResponse response)
  18.             throws ServletException, IOException {


  19.     }

  20.     /**
  21.      * The doPost method of the servlet. <br>
  22.      *
  23.      * This method is called when a form has its tag value method equals to post.
  24.      *
  25.      * @param request the request send by the client to the server
  26.      * @param response the response send by the server to the client
  27.      * @throws ServletException if an error occurred
  28.      * @throws IOException if an error occurred
  29.      */
  30.     public void doPost(HttpServletRequest request, HttpServletResponse response)
  31.     throws ServletException, IOException {
  32.         //接收上传文件内容中临时文件的文件名
  33.         String tempFileName = new String("tempFileName");
  34.         //tempfile 对象指向临时文件
  35.         File tempFile = new File("D:/"+tempFileName);
  36.         //outputfile 文件输出流指向这个临时文件
  37.         FileOutputStream outputStream = new FileOutputStream(tempFile);
  38.         //得到客服端提交的所有数据
  39.         InputStream fileSourcel = request.getInputStream();
  40.         //将得到的客服端数据写入临时文件
  41.         byte b[] = new byte[1000];
  42.         int n ;
  43.         while ((n=fileSourcel.read(b))!=
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值