`
SavageGarden
  • 浏览: 216098 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JSP+VBScript+Word

    博客分类:
  • VB
阅读更多

真是难以想象,一天的时间,我也整出来了一个 VBScript 的应用,看来微软的这些东西还是有那么点可取之处的,别的不说,就说 word 里录制宏和 word 里带的 Vb 的帮助文档,就省去了不少麻烦。废话不多少,代码说话。

需求为:查询某种类型的记录,然后在表格中显示其部分属性,并按照月份分成几个小表格,打印到 word 中,并将此 word 内容作为另一种记录的一个附件。

写了个测试例子,贴代码

首先是个 Bean

 

package com.test;

public class Bean {

       public String title;
       public String date;
       public String status;

       public Bean(String title,String date,String status){

              this.title = title;
              this.date = date;
              this.status = status;

       }

       public String getDate() {
              return date;
       }

       public void setDate(String date) {
              this.date = date;
       }

       public String getStatus() {
              return status;
       }

       public void setStatus(String status) {
              this.status = status;
       }

       public String getTitle() {
              return title;
       }

       public void setTitle(String title) {
              this.title = title;
       }

}
 

 

JSP中按照月份分组部分

 

Bean bean1 = new Bean("测试一","2007-11-12","已办理");
Bean bean2 = new Bean("测试二","2007-12-12","已办理");
Bean bean3 = new Bean("测试三","2008-10-12","已办理");
Bean bean4 = new Bean("测试四","2008-11-12","已办理");
Bean bean5 = new Bean("测试五","2007-11-20","已办理");
Bean bean6 = new Bean("测试六","2008-11-12","已办理");

ArrayList list = new ArrayList();
list.add(bean1);
list.add(bean2);
list.add(bean3);
list.add(bean4);
list.add(bean5);
list.add(bean6);

String[] yearAndMonth = new String[48];//4*12=48,存储某年某月
for(int i = 0;i < yearAndMonth.length;i ++){//数组初始化
	yearAndMonth[i] = "";
}
HashMap hp = new HashMap();//存放某年某月的记录列表
int index = 0;//数组的索引
for(int i = 0;i < list.size();i ++){
	Bean bean = (Bean)list.get(i);
	String beginTime = bean.getDate();
	boolean needAdd = true;//标记是否需要添加
	for(int j = 0;j < index;j ++){
		if(beginTime.substring(0,7).equals(yearAndMonth[j])){//如果某年某月已经有记录
			needAdd = false;//不需要添加
			break;
		}
	}
	if(needAdd){//需要添加
		yearAndMonth[index] = beginTime.substring(0,7);//改写数组对应索引处的值
		//新建List保存记录并存储到Map
		ArrayList listByTime = new ArrayList();
		listByTime.add(bean);
		hp.put(beginTime.substring(0,7),listByTime);
		index ++;
	}else{//不需要添加
		//从Map中得到对应的List,并在此添加记录
		((ArrayList)hp.get(beginTime.substring(0,7))).add(bean);	
	}
}
for(int j = 0;j < index;j ++){
	out.print(yearAndMonth[j]);
	out.println("<br>");
	if(hp.get(yearAndMonth[j])!=null){
		ArrayList a = (ArrayList)hp.get(yearAndMonth[j]);
		out.print("有"+a.size()+"个:");
		for(int x = 0;x < a.size();x ++){
			Bean bean = (Bean)a.get(x);
			out.print(bean.getDate()+"   ");
		}
		out.println("<br>");
	}
}

 然后是写入到制定位置的word文件中

<script language="vbscript">
	function writeListToWord()
		Set fso = CreateObject("Scripting.FileSystemObject")
		Set MyFile = fso.GetFile("E://test.doc")
		MyFile.Copy ("E://test1.doc")
		
	  Set myDocApp = CreateObject("Word.Application")
	  myDocApp.Visible = True
	  myDocApp.Activate
	  set myDoc = myDocApp.Documents.Open("E://test1.doc")
	  'Set myDoc = myDocApp.Documents.Add()
	  
	  Set objSelection = myDocApp.Selection
		
		<%
		for(int j = 0;j < index;j ++){
			if(hp.get(yearAndMonth[j])!=null){
				ArrayList a = (ArrayList)hp.get(yearAndMonth[j]);
				%>
				Set table1 = objSelection.Tables.Add(objSelection.Range, <%=a.size()+1%>, 3)
				Set Table1 = myDoc.Tables(<%=j+1%>)
				Table1.Cell(1,1).Range.Text = "标题"
				Table1.Cell(1,2).Range.Text = "发布时间"
				Table1.Cell(1,3).Range.Text = "状态"
				<%
				for(int x = 0;x < a.size();x ++){
					Bean bean = (Bean)a.get(x);
					%>
					Table1.Cell(<%=x+2%>,1).Range.Text = "<%=bean.getTitle()%>"
					Table1.Cell(<%=x+2%>,2).Range.Text = "<%=bean.getDate()%>"
					Table1.Cell(<%=x+2%>,3).Range.Text = "<%=bean.getStatus()%>"
					<%
				}
				%>
				for i = 1 To Table1.Rows.Count
					objSelection.MoveDown
				next
				
				objSelection.TypeText "新的循环开始"&vbCrLf
				<%
			}
		}
		%>
	  myDoc.close()
	  myDocApp.quit()
	  
	  Set MyFile = fso.GetFile("E://test1.doc")
	  MyFile.Copy ("E://test.doc")
	  MyFile.delete()
	end function
</script>

 OK,代码就是这些了,但是测试的话要加入受信站点,既允许读写本地文件

 

          

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics