cloudcc导入excel文件

<cc:page type="normal" style="standard" showSidebar="false" showHeader="false" />
<cc>

CCService cs = new CCService(userInfo);


  	
org.apache.poi.hssf.usermodel.HSSFWorkbook workbook = new org.apache.poi.hssf.usermodel.HSSFWorkbook();
org.apache.poi.hssf.usermodel.HSSFSheet sheet = workbook.createSheet("sheet1");
//创建第一列表头
org.apache.poi.ss.usermodel.Row row0 = sheet.createRow(0);
row0.setHeight((short)500);//设置高度
//设置单元格宽度
sheet.setColumnWidth(0,  (short)5000);
sheet.setColumnWidth(1,  (short)4000);
sheet.setColumnWidth(2,  (short)5000);
sheet.setColumnWidth(3,  (short)((20 + 0.72) * 256));//照片单元格的宽度
sheet.setColumnWidth(4,  (short)3000);
sheet.setColumnWidth(5,  (short)1500);




row0.createCell(0).setCellValue("订单号");
row0.createCell(1).setCellValue("录入人员");
row0.createCell(2).setCellValue("部门");
row0.createCell(3).setCellValue("照片");
row0.createCell(4).setCellValue("姓名");
row0.createCell(5).setCellValue("性别");



List<CCObject> bjList = cs.cquery("**","id ='"+bjid+"'");//查询列表
CCObject bjjbxx = bjList.get(0);
List<CCObject> htList = cs.cquery("**,"bjmc__c ='"+bjid+"'");//查询列表
//查询出关联学员的相关信息
CCObject ht = null;
CCObject xyxx = null;
String htXyId="";
String xyID="";
String url1="";
org.apache.poi.hssf.usermodel.HSSFRow dataRow = null;

java.io.DataInputStream dataInputStream  = null;
java.io.FileOutputStream fileOutputStream = null;
java.io.ByteArrayOutputStream output = null;
if(htList.size()>0){
try{
	

	for(int i=0;i<htList.size();i++){
		ht = htList.get(i);
		dataRow = sheet.createRow(i+1);//数据行
	    dataRow.setHeight((short)3000);
		dataRow.createCell(0).setCellValue( ht.get("name") == null ? "" : ht.get("name")+"");
		dataRow.createCell(1).setCellValue(ht.get("lrry") == null ? "" : ht.get("lrry")+"");
		dataRow.createCell(2).setCellValue(ht.get("bm") == null ? "" : ht.get("bm")+"");
		java.io.InputStream inputStream = null;  	
		String zpView = ht.get("zpView") == null ? "" :ht.get("zpView")+"";	      
	    if(!"".equals(zpView)){
			url1="https://k8smt01.cloudcc.com/querysome.action?m=viewImg&fileid="+zpView+"&binding="+binding;
			java.net.URL url = null;
			url = new java.net.URL(url1);
			dataInputStream = new java.io.DataInputStream(url.openStream());
			String imageName =  "test.jpg";
			fileOutputStream = new java.io.FileOutputStream(new File(imageName));
			output = new java.io.ByteArrayOutputStream();

			byte[] buffer = new byte[1024];
			int length;
			while ((length = dataInputStream.read(buffer)) > 0) {
				output.write(buffer, 0, length);
			}
			byte[] context=output.toByteArray();
			org.apache.poi.hssf.usermodel.HSSFPatriarch  drawingPatriarch = sheet.createDrawingPatriarch();
			org.apache.poi.hssf.usermodel.HSSFClientAnchor anchor = new org.apache.poi.hssf.usermodel.HSSFClientAnchor(0, 0, 0, 255, (short)3, i+1, (short) 4,  i+1);
			drawingPatriarch.createPicture(anchor, workbook.addPicture(context, org.apache.poi.xssf.usermodel.XSSFWorkbook.PICTURE_TYPE_JPEG));
			dataRow.createCell(3).setCellValue("");
		}else{
			dataRow.createCell(3).setCellValue("");
		}

		dataRow.createCell(4).setCellValue(ht.get("xyxmview") == null ? "" : ht.get("xyxmview")+"");
		dataRow.createCell(5).setCellValue(ht.get("xb") == null ? "" : ht.get("xb")+"");
		dataRow.createCell(6).setCellValue(ht.get("szcs") == null ? "" : ht.get("szcs")+"");
		dataRow.createCell(7).setCellValue(ht.get("htsfzh") == null ? "" : "'"+ht.get("htsfzh")+"");
		dataRow.createCell(8).setCellValue(ht.get("dwmc") == null ? "" : ht.get("dwmc")+"");
		dataRow.createCell(9).setCellValue(ht.get("xl") == null ? "" : ht.get("xl")+"");
		dataRow.createCell(10).setCellValue(ht.get("zy") == null ? "" : ht.get("zy")+"");
		dataRow.createCell(11).setCellValue(ht.get("zw") == null ? "" : ht.get("zw")+"");
		dataRow.createCell(12).setCellValue(ht.get("lxfs") == null ? "" : ht.get("lxfs")+"");
		dataRow.createCell(13).setCellValue(ht.get("email") == null ? "" : ht.get("email")+"");
		dataRow.createCell(14).setCellValue(ht.get("dz") == null ? "" : ht.get("dz")+"");
		dataRow.createCell(15).setCellValue(ht.get("jkgs") == null ? "" : ht.get("jkgs")+"");
		dataRow.createCell(16).setCellValue(ht.get("jkzh") == null ? "" : ht.get("jkzh")+""	);
		dataRow.createCell(17).setCellValue(ht.get("hkr") == null ? "" : ht.get("hkr")+"");
		dataRow.createCell(18).setCellValue(ht.get("hxje") == null ? "" : ht.get("hxje")+"");
		dataRow.createCell(19).setCellValue( ht.get("zsbh") == null ? "" : "'"+ht.get("zsbh")+"");
	
		}
	
	    out.println(url1);
		
		}catch(Exception e){
            out.println(url1);
		}finally{
			if(dataInputStream!=null){
				dataInputStream.close();
			}
			if(fileOutputStream!=null){
				fileOutputStream.close();
			}
			if(output!=null){
				output.close();
			}
			
		}
				
		//以下代码为导出文件流操作
	            java.io.ByteArrayOutputStream fos = null;
            		byte[] retArr = null;
            		try {
            			fos = new java.io.ByteArrayOutputStream();
            			workbook.write(fos);
            			retArr = fos.toByteArray();
            		} catch (Exception e) {
            			// TODO Auto-generated catch block
            			e.printStackTrace();
            		} finally {
            			try {
            				fos.close();
            			} catch (IOException e) {
            				// TODO Auto-generated catch block
            				out.println(e);
            			}
            		}
            		java.io.OutputStream os = response.getOutputStream();
            		String excelName=bjjbxx.get("name")+"学员信息";
            		try{
            			response.reset();
            			String fileName = new String(excelName.getBytes(), "iso8859-1");
            			response.setHeader("Content-Disposition", "attachment; filename="+fileName+".xls");//要保存的文件名
            			response.setContentType("application/octet-stream; charset=utf-8");
            			os.write(retArr);
            			os.flush();
            		}finally{
            			if (os != null) {
            				os.close();
            			}
            		}	
	
	
}else{
	
	out.println("<script>alert('该班级暂时没有学员');</script>");
}


		
</cc>
 

cloudcc本身支持POI的导入和导出功能,但是缺点是只能写在jsp页面,无法写成工具类,使用紧急

上一篇:蚂蚁金服重磅发布SOFAStack双模微服务平台


下一篇:Excel 文件的上传