jQuery中的ajax方法,我们有自己封装的工具库utils.js,包含以上多数常用的方法。
----------------------------------------------------------------------------------
关于遮罩层及其简单实现,遮罩层使用在表单提交的时候为了防止二次提交
---------------------------------------------------------------------------------
导入导出excel文件:
导入:表单的方式导入文件(与上传附件类似,基于strust2)
html:
<form action="kktzdzlmx!sknrUpload.action" method="post" enctype='multipart/form-data' id="xs_upload">
<input type="hidden" name="kcrwdm" value="${param.kcrwdm}">
<input type="hidden" name="xnxqdm" value="${param.xnxqdm}">
<input type="hidden" name="jxbdm" value="${param.jxbdm}">
<input type="hidden" name="kkbmdm" value="${param.kkbmdm}">
<input type="hidden" name="kkjysdm" value="${param.kkjysdm}">
<input type="hidden" name="pkrs" value="${param.pkrs}">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td height="18" class='ctdheader2' align="left"> <font
color="#FFFFFF">导入授课内容</font></td>
</tr>
</table>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr style="height:30px">
<td width='130' align="right" height='25px'></td>
<td align="left">
<a href='${basePath}mains/templates/downloadtemplate/sknr.xls' class="easyui-linkbutton" data-options="iconCls:'icon-down'" target=_blank>点击下载模板</a>
<br>
</td>
</tr>
<tr style="height:30px">
<td align="right">选择文件:</td>
<td align="left" ><input type="file" name="file" id="file" data-options='required:true' class='easyui-validatebox'></td>
</tr>
<tr style="height:30px" id="tjTr">
<td> </td>
<td align="center"><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save'" οnclick="tj(this)">上传</a></td>
</tr>
</table>
<p id="tips" ></p>
</form>
action:
// struts2 上传
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
/**
* 导入xxx
* @author XQ
* @date 2018/01/11
*/
public String sknrUpload(){
StringBuffer msg = new StringBuffer();
boolean pass = true;
List<Map<String, String>> jxdgList = new ArrayList<Map<String,String>>();
try {
jxdgList = ReadExcelUtils.getListData2Header(file, 0);//这里你的excel是在第一行开始就是0,第二行1,依次类推
} catch (Exception e) {
logger.error("xxx", e);
getRequest().setAttribute("msg","excel打开出错");
return "jxdgdrjg";
}
//开始处理excel表,如果行为空则跳过
for(int i=0;i<jxdgList.size();i++){
Map<String,String> tempMap = jxdgList.get(i);
System.out.println("---"+tempMap.size());
System.out.println("jinlaile");
if(Utils.nullOrEmpty(tempMap.get("xxx")))continue;
Iterator iter = tempMap.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
String key = (String)entry.getKey();
String val = (String)entry.getValue();
if(val != null){
val = val.trim();
}
tempMap.put(key, val);
}
//xxx如果不是数字,utils是工具类
if(!Utils.isNumeric(tempMap.get("xxx"))){
msg.append("xxxx,");
pass = false;
}
//xxx如果长度超过自定义的size则不予许导入,pass=false
if(!Utils.nullOrEmpty(tempMap.get("xxx")) && tempMap.get("xxx").length()>150){
msg.append("xxx");
pass = false;
}
}
msg.append("导入不成功 ,请修改完重新导入。");
file.deleteOnExit();
if(pass){//如果数据没问题可以执行相关的sql操作
DefaultTransactionDefinition def =new DefaultTransactionDefinition();
TransactionStatus ts = dao.getTran().getTransaction(def);
/*
* getSafePara 获取request参数,这些参数在skbrImport.jsp表单绑定提交过来
*/
String kcrwdm = getSafePara("kcrwdm", false);
String xnxqdm = getSafePara("xnxqdm", false);
try{
for(int i=0;i<jxdgList.size();i++){
Map<String,String> tempMap = jxdgList.get(i);
//判断该行是否为空,如果是则跳过
if(Utils.nullOrEmpty(tempMap.get("xx")))
continue;
String sql;
sql = "update k_0002 set sknrjj="+Utils.getSafeValue(tempMap.get("xxx"),false)+"where kxh="+tempMap.get("xxx")+"and kcrwdm="+kcrwdm+"and xnxqdm="+xnxqdm;
int ret = dao.execSqlUpdateOrDelToTran(sql);
System.out.println(ret);
if (ret >= 0) { saveLog("xxx",sql,"xxx",""); }
msg.setLength(0);
msg.append("导入成功");
}
dao.getTran().commit(ts);
}catch (Exception e) {
dao.getTran().rollback(ts);
logger.error("导入xxx", e);
getRequest().setAttribute("msg","操作失败");
return "jxdgdrjg";
}
};
getRequest().setAttribute("msg",msg.toString());
return "jxdgdrjg";
}
/*------------------------------------------------------------------------------------------------------*/
导出excel:
@Result(name = "downloadexcel",type = "stream", params = { "contentType", "application/vnd.ms-excel",
"inputName", "excelStream", "contentDisposition", "attachment;filename=\"${fileName}\"", "bufferSize",
"4096" })
//需要先导入相关的类,file,FileInputStream,excel读取工具类,格式转换工具类XLSTransformer...
private String fileName;
private InputStream excelStream;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
try {
this.fileName = new String(fileName.getBytes("gb2312"), "iso8859-1");
}catch (UnsupportedEncodingException e) {
this.fileName = fileName;
}
}
public InputStream getExcelStream() {
return excelStream;
}
public void setExcelStream(InputStream excelStream) {
this.excelStream = excelStream;
}
public String export() throws ParsePropertyException, InvalidFormatException, IOException {
Map<String, Object> beans = new HashMap<String, Object>();
beans.put("jxrls", jxrls);
beans.put("info", info);
XLSTransformer transformer = new XLSTransformer();
ByteArrayOutputStream os = new ByteArrayOutputStream();
InputStream xlsTemplateIO = new BufferedInputStream(
new FileInputStream(ServletActionContext.getServletContext().getRealPath("/")
+File.separator+"mains"+File.separator+"templates"+File.separator+"printtemplate"+File.separator+"print_jxrl.xls"));
org.apache.poi.ss.usermodel.Workbook wb = transformer.transformXLS(xlsTemplateIO, beans);
// if("ZYYXY_".equalsIgnoreCase((String) ConfigService.getContextProperty("ntss.schoolKey"))){
// HSSFSheet sheet = (HSSFSheet) wb.getSheetAt(0);
// sheet.getRow(0).setZeroHeight(true);
// sheet.protectSheet((String) ConfigService.getContextProperty("ntss.excelPassword"));
// }
wb.write(os);
setExcelStream(new ByteArrayInputStream(os.toByteArray()));
setFileName(info.get("kcmc")+"-"+info.get("jxbmc")+"xxx.xls");
return "downloadexcel";
}