Poi 读写Excel 合并ExcelSheet Struts2实现

文章来源:孤松 时间:2025-03-28

网上有很多人正在找如许的例子,有多个Excel,要把他们开并到1个Excel内部,那里触及不过是Excel的读与战Sheet的开并。尔干了如许1个兑现,能够参照,固然更盼望辅导。应用Struts完毕他的上传功用,正在把多个Excel上传到Action后,停止开并,而后曲交施行停载。也便是道,尔们1个Action里要动用Struts2的上传战停载二个效用。实行的步调:1.拷贝Struts的包到工程(预计城市吧,Ctrl+C 添 Ctrl + V)2.正在Web.xml里建设Struts2,也没有易

<?xmlversion="1.0"encoding="UTF-8"?><web-appversion="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>

3.界说Struts2的摆设文献

<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEstrutsPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><!--少许根本设备,本身瞧着办--><constantname="struts.devMode"value="false"></constant><constantname="struts.i18n.reload"value="true"></constant><constantname="struts.i18n.encoding"value="UTF-8"></constant><constantname="struts.multipart.saveDir"value="C:\\"></constant><constantname="struts.multipart.maxSize"value="20971520"></constant><packagename="default"namespace="/"extends="struts-default"><actionname="doUpload"class="com.golden.action.StrutsFileUpload"><resultname="success"type="stream"><paramname="contentType">application/vnd.ms-excel</param><paramname="contentDisposition">attachment;filename="TEST.xls"</param><paramname="inputName">downLoadStream</param><paramname="bufferSize">4096</param></result></action></package></struts>

注重:contentType:要扶植为停载典型为Excel,固然那些能够正在Actoin里动静界说,念兑现的详细再道。contentDisposition:内部切切没有要记了attachment;否则大概会出少许题目,固然大概有的人没有写。前面是停载的文献实,也能够正在Action里界说。inputName:实正施行停载的办法bufferSize:慢冲区年夜小

4.写1个上传页里

<%@pagelanguage="java"pageEncoding="UTF-8"%><%@taglibprefix="s"uri="/struts-tags"%><%Stringpath=request.getContextPath();StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"><html><head><basehref="<%=basePath%>"><title>多文献Excel理会</title><metahttp-equiv="pragma"content="no-cache"><metahttp-equiv="cache-control"content="no-cache"><metahttp-equiv="expires"content="0"><metahttp-equiv="keywords"content="keyword1,keyword2,keyword3"><metahttp-equiv="description"content="Thisismypage"></head><body><center><s:formaction="doUpload"method="POST"enctype="multipart/form-data"><s:filename="upload"label="上传的文献1"/><s:filename="upload"label="上传的文献2"/><s:filename="upload"label="上传的文献3"/><s:filename="upload"label="上传的文献4"/><s:filename="upload"label="上传的文献5"/><s:filename="upload"label="上传的文献6"/><s:submitvalue="上传"/></s:form></center></body></html>

注重:内中应用了Struts2的标签,也能够曲交应用Html标签5.最关头的局限,写Action的类

packagecom.golden.action;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.InputStream;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFCellStyle;importorg.apache.poi.hssf.usermodel.HSSFDateUtil;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;importcom.opensymphony.xwork2.ActionSupport;importjava.math.*;importjava.text.NumberFormat;@SuppressWarnings("serial")publicclassStrutsFileUploadextendsActionSupport{privateFile[]upload;//本质上传文献privateString[]uploadContentType;//文献的内乱容典型privateString[]uploadFileName;//上传文献实/***哀告的Action*/@OverridepublicStringexecute()throwsException{return"success";}/***实正的停载办法*/@SuppressWarnings("deprecation")publicInputStreamgetDownLoadStream()throwsException{HSSFWorkbookwb=newHSSFWorkbook();//创立1个靠左排搁样子,假若须要其余形状自能够再界说少许HSSFCellStylestyle=wb.createCellStyle();//style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);style.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//正在单位格中左排搁try{for(inti=0;i<upload.length;i++){Filef=upload[i];//与得1个文献FileInputStreamis=newFileInputStream(f);HSSFWorkbookwbs=newHSSFWorkbook(is);//凭据读出的Excel,创造SheetHSSFSheetsheet=wb.createSheet(uploadFileName[i]);//无间与的是第1个Sheet,必定要注重,倘使您要读与全部的Sheet,轮回读与便可HSSFSheetchildSheet=wbs.getSheetAt(0);//轮回读与Excel的止for(intj=0;j<childSheet.getLastRowNum();j++){//凭据读与的止,创办要开并Sheet的止HSSFRowr=sheet.createRow(j);HSSFRowrow=childSheet.getRow(j);//判定能否为空,由于大概呈现空止的环境if(null!=row){//轮回读与列for(intk=0;k<row.getLastCellNum();k++){//凭据读与的列,创办列HSSFCellc=r.createCell(k);HSSFCellcell=row.getCell(k);//将值战款式一齐赋值给单位格Stringvalue="";if(null!=cell){switch(cell.getCellType()){caseHSSFCell.CELL_TYPE_NUMERIC://数值型if(HSSFDateUtil.isCellDateFormatted(cell)){//即使是Date典型则,获得该Cell的Date值value=HSSFDateUtil.getJavaDate(cell.getNumericCellValue()).toString();}else{//杂数字,那里要判定能否为少量的环境,原因整数正在写时兴会被加入少量面Stringt=cell.getNumericCellValue()+"";BigDecimaln=newBigDecimal(cell.getNumericCellValue());//判定能否有少量面if(t.indexOf(".")<0){value=n.intValue()+"";}else{//数字花样化对于象NumberFormatnf=NumberFormat.getInstance();//少量面最年夜二位nf.setMaximumFractionDigits(2);//施行花样化value=nf.format(n.doubleValue());}}break;caseHSSFCell.CELL_TYPE_STRING://字符串型value=cell.getRichStringCellValue().toString();break;caseHSSFCell.CELL_TYPE_FORMULA://公式型//读公式企图值value=String.valueOf(cell.getNumericCellValue());break;caseHSSFCell.CELL_TYPE_BOOLEAN://布我value=""+cell.getBooleanCellValue();break;/*此止表白该单位格值为空*/caseHSSFCell.CELL_TYPE_BLANK://空值value="";break;caseHSSFCell.CELL_TYPE_ERROR://毛病value="";break;default:value=cell.getRichStringCellValue().toString();}}else{value="";}c.setCellValue(value);c.setCellStyle(style);}}else{HSSFCellc=r.createCell(0);c.setCellValue("");}}}}catch(Exceptione){e.printStackTrace();}//这类写法没有会出现姑且文献,由于那里应用字节数组手脚介量ByteArrayOutputStreamos=newByteArrayOutputStream();wb.write(os);byte[]content=os.toByteArray();InputStreamis=newByteArrayInputStream(content);returnis;}publicFile[]getUpload(){returnupload;}publicvoidsetUpload(File[]upload){this.upload=upload;}publicString[]getUploadContentType(){returnuploadContentType;}publicvoidsetUploadContentType(String[]uploadContentType){this.uploadContentType=uploadContentType;}publicString[]getUploadFileName(){returnuploadFileName;}publicvoidsetUploadFileName(String[]uploadFileName){this.uploadFileName=uploadFileName;}}

那里要存眷的中央太多,详细的代码里疏解写的很分明。必定要注重的是要判定单位格的典型,出格是数字典范时,尔凭据本身的需要必定了极少处置。

推举您浏览更多相关于“ excelstrutsaction开并sheet ”的著作