//js代码 jQuery.noConflict(); //jquery版本兼容 function ajaxFileUpload() { var fileVal = jQuery("#file").attr("value"); var fileValArr = fileVal.split("."); if (fileValArr[fileValArr.length - 1] != 'xls') { alert("请上传excel文件!"); return false; } jQuery("#loading").ajaxStart(function() { jQuery(this).show(); $("#note").html("正在上传..."); })// 开始上传文件时显示一个图片 .ajaxComplete(function() { jQuery(this).hide(); });// 文件上传完成将图片隐藏起来 jQuery.ajaxFileUpload({ url : '../../examTarget/uploadFile.action',// 用于文件上传的服务器端请求地址 secureuri : false,// 一般设置为false 这个为空ajaxfileupload中的iframe不显示 fileElementId : 'file',// 文件上传空间的id属性 <input type="file" id="file" name="file" /> data:{'examActivityRowid':examActivityRowid}, dataType : 'json',// 返回值类型 一般设置为json success : function(data) // 服务器成功响应处理函数 { // 这里放入返回成功后需要处理的响应data是返回的数据 if(data && data.fileName!=""){ $("#note").html("上传成功,正在解析..."); } }, error : function(data)// 服务器响应失败处理函数 { // 服务器响应失败的处理信息。 $("#note").html("上传成功失败!"); } }) return false; }
java struts2后台处理
public void uploadFile() throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("utf-8"); InputStream is = null; OutputStream os = null; try { checkUserId(); String path = Util.getBootProps().getProperty( PropertiesValue.MARKT_EXCEL_TEMPLATE_PATH); is = new FileInputStream(file);// 输入流 fileName = System.currentTimeMillis() + ".xls"; File toFile = new File(path, fileName); os = new FileOutputStream(toFile); // 设置缓存 byte[] buffer = new byte[1024]; int length = 0; // 读取myFile文件输出到toFile文件中 while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } } catch (Exception e) { fileName = ""; } finally { try { // 关闭输入流 if (null != is) { is.close(); } // 关闭输出流 if (null != os) { os.close(); } } catch (IOException e) { fileName = ""; } } ServletActionContext.getResponse().getWriter().write( "{fileName:\"" + fileName + "\"}"); ServletActionContext.getResponse().getWriter().flush(); ServletActionContext.getResponse().getWriter().close(); }
xml配置
<action name="uploadFile" class="examTaskKpiTagertAction" method="uploadFile"> </action>
=============================================上传功能==================
jxl的 excel生成 直接下载
import jxl.Workbook; import jxl.format.UnderlineStyle; import jxl.write.DateFormat; import jxl.write.DateTime; import jxl.write.Label; import jxl.write.NumberFormat; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook;
public void download() throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); response.reset(); //设置响应头为二进制流 response.setContentType( "application/x-msdownload;charset=utf-8"); response.setCharacterEncoding("utf-8"); downloadFileName = "目标值导入模板.xls"; ServletActionContext.getResponse().setHeader( "Content-Disposition", "attachment; filename=" + new String(downloadFileName.getBytes(),"ISO-8859-1")); WritableWorkbook wb = Workbook.createWorkbook(ServletActionContext.getResponse().getOutputStream()); // 创建Excel工作表 WritableSheet ws = wb.createSheet("sheet1", 0); // 添加Label对象 Label label1 = new Label(0, 0, "测试创建Excel"); ws.addCell(label1); // 添加带有字型Formatting的对象 WritableFont wf = new WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD, true); WritableCellFormat wcfF = new WritableCellFormat(wf); Label labelCF = new Label(1, 0, "This is a Label Cell", wcfF); ws.addCell(labelCF); // 添加带有字体颜色Formatting的对象 WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED); WritableCellFormat wcfFC = new WritableCellFormat(wfc); Label labelCFC = new Label(1, 0, "This is a Label Cell", wcfFC); ws.addCell(labelCFC); // 添加Number对象 jxl.write.Number labelN = new jxl.write.Number(0, 1, 3.1415926); ws.addCell(labelN); // 添加带有formatting的 Number对象 NumberFormat nf = new NumberFormat("#.##"); WritableCellFormat wcfN = new WritableCellFormat(nf); jxl.write.Number labelNF = new jxl.write.Number(1, 1, 3.1415926, wcfN); ws.addCell(labelNF); // 添加Boolean对象 jxl.write.Boolean labelB = new jxl.write.Boolean(0, 2, false); ws.addCell(labelB); // 添加DateTime对象 DateTime labelDT = new DateTime(0, 3, new java.util.Date()); ws.addCell(labelDT); // 添加带有formatting的DateFormat对象 DateFormat df = new DateFormat("dd MM yyyy hh:mm:ss"); WritableCellFormat wcfDF = new WritableCellFormat(df); DateTime labelDTF = new DateTime(1, 3, new java.util.Date(), wcfDF); ws.addCell(labelDTF); Label label31 = new Label(3, 1, "0011x"); Label label32 = new Label(3, 2, "0012x"); Label label33 = new Label(3, 3, "0013x"); ws.addCell(label31); ws.addCell(label32); ws.addCell(label33); ws.setColumnView(3, 0); wb.write(); // 写入Exel工作表 wb.close(); // 关闭Excel工作薄对象 }
<action name="download" class="examTaskKpiTagertAction" method="download"> <result name="success" type="stream"> </result> </action>