码迷,mamicode.com
首页 > 编程语言 > 详细

去哪网实习总结:java读写excel表格(JavaWeb)

时间:2015-08-16 23:15:38      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:java   java web   读写excel表格   

本来是以做数据挖掘的目的进去哪网的,结构却成了系统开发。。。

不过还是比较认真的做了三个月,老师很认同我的工作态度和成果。。。

实习马上就要结束了,总结一下几点之前没有注意过的变成习惯和问题,分享给大家。


同时打个广告:去哪网内审部招JavaWeb开发实习生,时间非常自由,每周一天、周六周日甚至都可以,时间充裕的小伙伴给我留言啊,挣个零花钱,还能长点经验。。。。(保研的、想工作的大四狗最合适不过了。。。)




需哟的包(java操作excel包 jxl.jar):http://download.csdn.net/detail/mmc2015/9009859

还是直接上代码,官方文档参考http://www.andykhan.com/jexcelapi/:

//(NOTE: when creating a spreadsheet from a ServletInputStream you must remove the HTTP header information before creating the Workbook object.) <html>
	@SuppressWarnings("rawtypes")
public static void createExcel(TreeMap MonitorPointInstanceDetailsMap) throws WriteException, IOException{
	String monitorPointName = MonitorPointInstanceDetailsMap.get("monitorPointName").toString();			
		
	//输出EXCEL
	String nowDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	String serverRealRootPath =<strong> GetServerRealPathUtil.getRootPath()</strong>;//参考前几篇博客
	//System.out.println(serverRealRootPath);
	String fileName = serverRealRootPath+File.separator+nowDate+".xlsx";
	File file = new File(fileName);
	OutputStream os = new FileOutputStream(file);
        
	//创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(file);
        //创建新的一页
        WritableSheet sheet = workbook.createSheet("First Sheet", 0);
        //创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容
        //The other point to note is that the cell's location is specified as (column, row). Both are zero indexed integer values - A1 being represented by (0,0), B1 by (1,0), A2 by (0,1) and so on.
        
        /*构造合并的表头*/
        //添加合并单元格,第一个参数是起始列,第二个参数是起始行,第三个参数是终止列,第四个参数是终止行
        sheet.mergeCells(0, 0, 6, 0);
        //设置字体种类和黑体显示,字体为Arial,字号大小为15,采用黑体显示
        WritableFont mergeTitleBold = new WritableFont(WritableFont.ARIAL, 15, WritableFont.BOLD);
        //生成一个单元格样式控制对象
        WritableCellFormat mergeTitleFormate = new WritableCellFormat(mergeTitleBold);
        //单元格中的内容水平方向居中
        mergeTitleFormate.setAlignment(jxl.format.Alignment.CENTRE);
        //单元格的内容垂直方向居中
        mergeTitleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
        Label mergeTitle = new Label(0, 0, "表头表头。。。", mergeTitleFormate);
        sheet.setRowView(0, 800, false);//设置行的高度
        sheet.addCell(mergeTitle);
        
        int currentRow = 1;
        Iterator it=MonitorPointInstanceDetailsMap.entrySet().iterator();
	while(it.hasNext()){
            Map.Entry entry = (Map.Entry)it.next();  
            String processID = entry.getKey().toString();
            if(processID.equals("monitorPointName")){
            	continue;
            }
            String value = entry.getValue().toString();
            //System.out.println(processID+"---"+value);
            String processName = value.split(";")[0];
            if((value.split(";")).length!=5){
            	//该流程【在统计周期内】还没有对应的流程实例信息
       		continue;
            }
		    String[] processInstance_Name = (value.split(";")[1]).split(",");
		    String[] processInstance_SenderName = (value.split(";")[2]).split(",");
		    String[] processInstance_CreateDate = (value.split(";")[3]).split(",");
		    String[] processInstance_StopNodeName = (value.split(";")[4]).split(",");
		    for(int i=0;i<processInstance_Name.length&&!processInstance_Name[i].equals("");i++){
		    	if(i==0){//增加标题行
		    		//设置字体种类和黑体显示,字体为Arial,字号大小为12,采用黑体显示
		            WritableFont titleBold = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD);
		            //生成一个单元格样式控制对象
		            WritableCellFormat titleFormate = new WritableCellFormat(titleBold);
		            //单元格中的内容水平方向居中
		            titleFormate.setAlignment(jxl.format.Alignment.CENTRE);
		            //单元格的内容垂直方向居中
		            titleFormate.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
		            sheet.setRowView(0, 600, false);//设置行的高度
		            Label title0 = new Label(0, currentRow, "ID", titleFormate);
		            Label title1 = new Label(1, currentRow, "流程名称", titleFormate);
		            Label title2 = new Label(2, currentRow, "流程实例名称", titleFormate);
		            Label title3 = new Label(3, currentRow, "实例发起人", titleFormate);
		            Label title4 = new Label(4, currentRow, "实例发起时间", titleFormate);
		            Label title5 = new Label(5, currentRow, "实例是否结束", titleFormate);
		            Label title6 = new Label(6, currentRow, "待处理的节点名称", titleFormate);
		            sheet.addCell(title0);
		            sheet.addCell(title1);
		            sheet.addCell(title2);
		            sheet.addCell(title3);
		            sheet.addCell(title4);
		            sheet.addCell(title5);
		            sheet.addCell(title6);
		            currentRow++;
		    	}
		    	//增加实例内容行
		    	sheet.addCell(new Label(0, currentRow, Integer.toString(i+1)));
		    	sheet.addCell(new Label(1, currentRow, processName));
		    	sheet.addCell(new Label(2, currentRow, processInstance_Name[i]));
		    	sheet.addCell(new Label(3, currentRow, processInstance_SenderName[i]));
		    	sheet.addCell(new Label(4, currentRow, processInstance_CreateDate[i]));
		    	sheet.addCell(new Label(5, currentRow, "未结束"));
		    	sheet.addCell(new Label(6, currentRow, processInstance_StopNodeName[i]));
	            <span style="white-space:pre">	</span>currentRow++;
		    }
		}
		
		
        //把创建的内容写入到输出流中,并关闭输出流
        workbook.write();
        workbook.close();
        
        os.close();
    }


版权声明:本文为博主原创文章,未经博主允许不得转载。

去哪网实习总结:java读写excel表格(JavaWeb)

标签:java   java web   读写excel表格   

原文地址:http://blog.csdn.net/mmc2015/article/details/47685763

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!