上传,解析Excel文件并保存数据到数据库

web框架:spring+hibernate+jQuery

HTML端代码:










 请采用excel文件格式。 value="0" /> name="resultfileurls" value="${report.resultinfoListString}" />










                       
${item.localurl } 

                                                        x

 

                添加附件

 


对应的js代码部分:


title="  上传" iconCls="icon-save"
style="width:500px;height:240px;padding:5px;background-color:#fafafa;">

style="padding:10px;background:#fff;border:1px solid #ccc;">


浏览:
解释:



style="text-align:right;height:30px;line-height:30px;">
href="javascript:void(0)"
onClick="$('#uploadify').uploadifyUpload();">上传
class="easyui-linkbutton" iconCls="icon-cancel"
href="javascript:void(0)" onClick="$('#fileText').window('close');">取消






后端controller程序处理代码:

protected void formSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, Errors errors,
PmUserSession userSession, PageSession pageSession)
throws Exception {
String attid=request.getParameter("attid"); //attid为对应Excel文件的UUID号
if(WebUtils.isNotNull(attid)){
List rinfoList=dao.find(" from AAA obj where obj.attid='"+attid+"' and del=0"); //AAA为数据库中存文件路径的表名用hibernate映射之后的名称
if(rinfoList.size()>0){
File f=new File(rinfoList.get(0).getLocalurl());
ImportTerm i=new ImportTerm();
i.importTerm(f, dao,attid);
response.sendRedirect(request.getContextPath() + "/embed/datasetElement.htm?id="+request.getParameter("dataset"));
}
}


}

调用ImportTerm类的方法:

package cn.nsl.data;


import java.io.File;
import java.util.List;


import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import cn.nsl.domain.glob.DataSet;
import cn.nsl.domain.glob.DataSetElement;
import cn.nsl.domain.glob.Elements;
import cn.nsl.web.Application;


import com.jbetter.spring.orm.ICommonDao;




public class ImportTerm extends ImportExcel {

public void importTerm(File f,ICommonDao dao,String attid){
try {
Workbook wb;
wb = WorkbookFactory.create(f);
Sheet sheet = wb.getSheet("Sheet1");
importTermSheet(sheet,dao,dataset,attid);

} catch (Exception e) {
// TODO Auto-generated catch block
log.error(e);
e.printStackTrace();
}

}

void importTermSheet(Sheet sheet,ICommonDao dao,DataSet dataset,String attid) throws Exception{
int rowEnd = Math.max(100, sheet.getLastRowNum());
for(int i=1;i<=rowEnd;i++){
importTermProd(sheet.getRow(i),dao,dataset,attid);
}
}
void importTermProd(Row r,ICommonDao dao,DataSet dataset,String attid) throws Exception{
String code=getString(r, 0);
String cnname = getString(r, 1);
String name = getString(r, 2);
String frequency = getString(r, 3);
String limiting = getString(r, 4);
String multiJATS = getString(r, 5);
String remarks = getString(r, 6);

               biaoming c = new biaoming();
c.setFrequency(frequency);
c.setLimiting(limiting);
c.setMultiJATS(multiJATS);
c.setRemarks(remarks);
c.setAttid(attid);

dao.save(c);
}

public static void main(String []args){
System.setProperty("file.encoding", "UTF-8");
ApplicationContext apx = new ClassPathXmlApplicationContext("applicationContext-hibernate.xml");
dao = (ICommonDao) apx.getBean("commonDao");
ImportTerm i = new ImportTerm();
i.importTerm(new File("D://term.xlsx"),dao,null,"dd");
}


}

到此Excel上传解析入库完成。方法多种多样,熟练一种就可以了。


你可能感兴趣的:(javaweb)