Apache FileUpload 上传以及 JExcelApi 解析
2008-09-27 18:48
507 查看
最近遇到点读取 Excel 数据的问题,于是花了点时间找开源工具。
要解析 Excel,首当其冲的是上传文件,以前在项目里我们用 SmartUpload 进行上传,不过这个项目似乎已经停止开发了,于是在这里我使用 Apache Commons FileUpload,可以在 http://jakarta.apache.org/commons/fileupload 找到。目前该项目的最新版本是 1.1.1,网上有大量的范例程序,不过后来用的时候发现大部分方法在新版本中都不推荐使用了,于是好好读了一回 API 和官方范例。
先来看看如何上传文件,Servlet 很简单,在这里我限制了最大上传量为 1M,且直接读进内存中,不进行磁盘临时文件缓存。
表单上一定要加ENCTYPE="multipart/form-data"
<form id="frm_reqNew" name="frm_reqNew" ENCTYPE="multipart/form-data" action="$!actionPath/deliver/deliverReqCreate.shtml" method="post" >
-------------------------------------------------------------------------------------------------------------------
上面的程序示范了如何上传文件到服务器,本文的主要目的不光是上传,还要进行 Excel 解析,抽取有用的内容。开源的 Excel 解析器很多,在此我选择了 JExcelApi,可以在 http://jexcelapi.sourceforge.net 找到,据说是韩国人开发的,最新版本是 2.6.2。为什么没有选 POI,原因也是因为它 N 久没有更新了。我总是喜欢最新的东东,比如 Adobe 的 PDF Reader,硬是下载了 8.0,结果感觉还没有 6.0 好用。:(
以下程序修改直上传,做了部分调整,取消了文件储存,直接通过读取输入流进行解析,并假设约定的 Excel 文件有五列 N 行,第一行为标题信息。
JExcelApi 用起来很简单,而且还可以根据 Excel 中数据类型转换成 Java 数据类型,比如 int、double,具体信息可以参考它的开发指南。当然,本范例还提供现构造 Excel 然后下载的方法,如果以后遇到,一定继续完善。
------------------------------------------------------------------------------------------------------------------
生成 excel 和下载
代码如下,放在 servlet 中,io 异常我没捕获,直接由 get or post 方法抛出,当然,如果更严谨点可以放在 finally 里关闭。
要解析 Excel,首当其冲的是上传文件,以前在项目里我们用 SmartUpload 进行上传,不过这个项目似乎已经停止开发了,于是在这里我使用 Apache Commons FileUpload,可以在 http://jakarta.apache.org/commons/fileupload 找到。目前该项目的最新版本是 1.1.1,网上有大量的范例程序,不过后来用的时候发现大部分方法在新版本中都不推荐使用了,于是好好读了一回 API 和官方范例。
先来看看如何上传文件,Servlet 很简单,在这里我限制了最大上传量为 1M,且直接读进内存中,不进行磁盘临时文件缓存。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | java.io.IOException; java.io.PrintWriter; java.io.File; java.net.URI; java.net.URL; javax.servlet.ServletException; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; java.util.List; org.apache.commons.fileupload.RequestContext; org.apache.commons.fileupload.servlet.ServletRequestContext; org.apache.commons.fileupload.servlet.ServletFileUpload; org.apache.commons.fileupload.disk.DiskFileItemFactory; org.apache.commons.fileupload.FileItem; UploadServlet HttpServlet UploadServlet() (); destroy() .destroy(); doGet(HttpServletRequest request, HttpServletResponse response) ServletException, IOException doPost(HttpServletRequest request, HttpServletResponse response) ServletException, IOException response.setContentType( ); response.setCharacterEncoding( ); PrintWriter out = response.getWriter(); out.println( ); out.println( ); out.println( ); URL url = getServletContext().getResource( ); RequestContext requestContext = ServletRequestContext(request); (ServletFileUpload.isMultipartContent(requestContext)) DiskFileItemFactory factory = DiskFileItemFactory(); factory.setSizeThreshold( 1024 * 1024 ); ServletFileUpload upload = ServletFileUpload(factory); upload.setSizeMax( 1024 * 1024 ); List items = ; items = upload.parseRequest(requestContext); ( i = 0 ; i < items.size(); i ++ ) FileItem fi = (FileItem) items.get(i); ( ! fi.isFormField()) String sourcePath = fi.getName(); String[] sourcePaths = sourcePath.split( ); String fileName = sourcePaths[sourcePaths.length - 1 ]; File uploadedFile = File( URI(url.toString() + fileName)); fi.write(uploadedFile); out.println(fileName + ); (Exception e) out.println( ); out.println( + e.toString()); e.printStackTrace(); out.println( ); out.println( ); out.flush(); out.close(); init() ServletException |
<form id="frm_reqNew" name="frm_reqNew" ENCTYPE="multipart/form-data" action="$!actionPath/deliver/deliverReqCreate.shtml" method="post" >
-------------------------------------------------------------------------------------------------------------------
上面的程序示范了如何上传文件到服务器,本文的主要目的不光是上传,还要进行 Excel 解析,抽取有用的内容。开源的 Excel 解析器很多,在此我选择了 JExcelApi,可以在 http://jexcelapi.sourceforge.net 找到,据说是韩国人开发的,最新版本是 2.6.2。为什么没有选 POI,原因也是因为它 N 久没有更新了。我总是喜欢最新的东东,比如 Adobe 的 PDF Reader,硬是下载了 8.0,结果感觉还没有 6.0 好用。:(
以下程序修改直上传,做了部分调整,取消了文件储存,直接通过读取输入流进行解析,并假设约定的 Excel 文件有五列 N 行,第一行为标题信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110111 | java.io.IOException; java.io.PrintWriter; javax.servlet.ServletException; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; java.util.List; org.apache.commons.fileupload.RequestContext; org.apache.commons.fileupload.servlet.ServletRequestContext; org.apache.commons.fileupload.servlet.ServletFileUpload; org.apache.commons.fileupload.disk.DiskFileItemFactory; org.apache.commons.fileupload.FileItem; jxl.Workbook; jxl.Sheet; jxl.Cell; UploadServlet HttpServlet UploadServlet() (); destroy() .destroy(); doGet(HttpServletRequest request, HttpServletResponse response) ServletException, IOException doPost(HttpServletRequest request, HttpServletResponse response) ServletException, IOException response.setContentType( ); response.setCharacterEncoding( ); PrintWriter out = response.getWriter(); out.println( ); out.println( ); out.println( ); FileItem fileItem = ; RequestContext requestContext = ServletRequestContext(request); (ServletFileUpload.isMultipartContent(requestContext)) DiskFileItemFactory factory = DiskFileItemFactory(); factory.setSizeThreshold( 1024 * 1024 ); ServletFileUpload upload = ServletFileUpload(factory); upload.setSizeMax( 1024 * 1024 ); List items = ; items = upload.parseRequest(requestContext); ( i = 0 ; i < items.size(); i ++ ) FileItem fi = (FileItem) items.get(i); ( ! fi.isFormField()) fileItem = fi; ; out.println(parseExcel(fileItem)); (Exception e) out.println( ); out.println( + e.toString()); e.printStackTrace(); out.println( ); out.println( ); out.flush(); out.close(); String parseExcel(FileItem fi) Exception Workbook workbook = ; workbook = Workbook.getWorkbook(fi.getInputStream()); Sheet sheet = workbook.getSheet( 0 ); count = sheet.getRows(); String a1 = sheet.getCell( 0 , 0 ).getContents(); String a2 = sheet.getCell( 1 , 0 ).getContents(); String a3 = sheet.getCell( 2 , 0 ).getContents(); String a4 = sheet.getCell( 3 , 0 ).getContents(); String a5 = sheet.getCell( 4 , 0 ).getContents(); ( i = 1 ;i < count;i ++ ) Cell[] cells = sheet.getRow(i); System.out.println(cells[ 0 ].getContents() + cells[ 1 ].getContents() + cells[ 2 ].getContents() + cells[ 3 ].getContents() + cells[ 4 ].getContents()); ; (Exception e) e; (workbook != ) workbook.close(); init() ServletException |
------------------------------------------------------------------------------------------------------------------
生成 excel 和下载
代码如下,放在 servlet 中,io 异常我没捕获,直接由 get or post 方法抛出,当然,如果更严谨点可以放在 finally 里关闭。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | response.setContentType(); String filename = String(.getBytes(),); response.setHeader(,+filename); String materialName = ; String size = ; String unit = ; String qty = ; String band = ; String company = ; String memo = ; String price = ; String repDate = ; List<String[]> list = ArrayList<String[]>(); ( i = 10; i > 0; i--) String[] outPut = materialName,size,unit,qty+i,band,company,memo,price,repDate; list.add(outPut); ByteArrayOutputStream baos = ByteArrayOutputStream(); WritableWorkbook workbook = Workbook.createWorkbook(baos); WritableSheet sheet = workbook.createSheet(, 0); WritableFont blodFont = WritableFont(WritableFont.TAHOMA,10,WritableFont.BOLD, ); WritableCellFormat blodFormat = WritableCellFormat (blodFont); Label label = ; label = Label(0, 0, , blodFormat); sheet.addCell(label); label = Label(1, 0, , blodFormat); sheet.addCell(label); label = Label(2, 0, , blodFormat); sheet.addCell(label); label = Label(3, 0, , blodFormat); sheet.addCell(label); label = Label(4, 0, , blodFormat); sheet.addCell(label); label = Label(5, 0, , blodFormat); sheet.addCell(label); label = Label(6, 0, , blodFormat); sheet.addCell(label); label = Label(7, 0, , blodFormat); sheet.addCell(label); label = Label(8, 0, , blodFormat); sheet.addCell(label); ( i = 1; i <= list.size(); i++) String[] outPut = list.get(i-1); label = Label(0, i, outPut[0]); sheet.addCell(label); label = Label(1, i, outPut[1]); sheet.addCell(label); label = Label(2, i, outPut[2]); sheet.addCell(label); label = Label(3, i, outPut[3]); sheet.addCell(label); label = Label(4, i, outPut[4]); sheet.addCell(label); label = Label(5, i, outPut[5]); sheet.addCell(label); label = Label(6, i, outPut[6]); sheet.addCell(label); label = Label(7, i, outPut[7]); sheet.addCell(label); label = Label(8, i, repDate); sheet.addCell(label); workbook.write(); workbook.close(); OutputStream os = response.getOutputStream(); os.write(baos.toByteArray()); os.flush(); os.close(); baos.close(); (RowsExceededException e) e.printStackTrace(); (WriteException e) e.printStackTrace(); |
相关文章推荐
- 使用Apache FileUpload 上传以及 JExcelApi 生成Excel文件
- Apache FileUpload上传组件API解析(待续)
- FileUpload文件上传源码解析
- Jsp apache-common-fileupload 文件上传 乱码
- Apache FileUpload文件上传组件API解析
- commons-fileupload上传附件api使用
- Apache FileUpload文件上传组件API解析
- Apache FileUpload实现文件上传
- 利用apache-commons-fileupload写jsp上传文件
- Apache的FileUpload组件实现文件上传
- apache的commons-fileupload实现文件上传,下载
- 使用apache的fileupload实现文件的上传
- struts2 文件上传和下载,以及部分源码解析
- PHP 上传apk, 解析apk ,以及写入文件到apk包
- 谷歌地图 API 开发之添加标记(解析以及补充)
- Apache的fileupload上传文件
- IPFS + 区块链 系列】 入门篇 - IPFS + Ethereum (中篇)-js-ipfs-api - 图片上传到IPFS以及下载
- commons-fileupload(apache开源文件上传组件)使用方式
- Apache FileUpload的两种上传方式介绍及应用
- 利用apache的Commons-fileupload和Ajax实现文件上传进度条