public void Process(String templateFile) { // Velocity 初始化. // 从配置文件读取配置信息. Velocity.Init("nvelocity.properties"); // 创建 Velocity Context VelocityContext context = new VelocityContext(); // 将列表的数据, 以 list 作为名称,放入 context. context.Put("list", Names); // 模版. Template template = null; // 尝试加载模版文件. try { template = Velocity.GetTemplate(templateFile); } catch (ResourceNotFoundException) { System.Console.Out.WriteLine("Example1 : error : cannot find template " + templateFile); } catch (ParseErrorException pee) { System.Console.Out.WriteLine("Example1 : Syntax error in template " + templateFile + ":" + pee); } // 处理模版信息. if (template != null) { template.Merge(context, System.Console.Out); } }
private void BuildOneFile(string className) { Type modelType = allTypeList.FirstOrDefault(p => p.FullName == className); if (modelType == null) { return; } //// 取得对象的所有属性. //PropertyInfo[] propArray = modelType.GetProperties(); //foreach (PropertyInfo prop in propArray) //{ // Console.WriteLine("属性名{0}, CanRead={1}, CanWrite={2}, 数据类型={3}", // prop.Name, prop.CanRead, prop.CanWrite, prop.PropertyType.Name); // Console.WriteLine("是否是抽象类{0}, 是否是类{1}", prop.PropertyType.IsAbstract, prop.PropertyType.IsClass); // Console.WriteLine("是否是数组{0}, 是否是枚举{1}", prop.PropertyType.IsArray, prop.PropertyType.IsEnum); // foreach (var attr in prop.GetCustomAttributes(true)) // { // Console.WriteLine("定义在属性上面的自定义特性: {0}", attr); // if (attr is System.ComponentModel.DataAnnotations.DisplayAttribute) // { // System.ComponentModel.DataAnnotations.DisplayAttribute displayAttr = attr as System.ComponentModel.DataAnnotations.DisplayAttribute; // Console.WriteLine("属性显示的标题:{0}", displayAttr.Name); // } // } // Console.WriteLine(); //} //var keyInfo = modelType.GetKeyInfo(); //Console.WriteLine("keyInfo = {0}", keyInfo); // Velocity 初始化. // 从配置文件读取配置信息. Velocity.Init("nvelocity.properties"); // 创建 Velocity Context VelocityContext context = new VelocityContext(); // 将数据, 以 Model 作为名称,放入 context. context.Put("Model", modelType); // 模版. Template template = null; string templateFile = this.cboTemplate.Text; // 尝试加载模版文件. try { template = Velocity.GetTemplate(templateFile, "utf-8"); } catch (ResourceNotFoundException) { MyMessage.Fail(String.Format("未能找到模板文件:{0}", templateFile)); } catch (ParseErrorException pee) { MyMessage.Fail(String.Format("解析模板文件 {0} 发生了异常!\n{1}", templateFile, pee)); } // 输出文件名, 去掉最后的 .vm string outputFileName = templateFile.Substring(0, templateFile.Length - 3); // 移除 template 目录. outputFileName = outputFileName.Replace("template\\", ""); // # 替换为类名. outputFileName = outputFileName.Replace("#", modelType.Name); try { // 处理模版信息. if (template != null) { string fileName = String.Format("{0}\\{1}", this.txtOutputPath.Text, outputFileName); using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.UTF8)) { template.Merge(context, sw); } } } catch (Exception ex) { MyMessage.Fail(String.Format("根据模板文件 {0} 生成文档的过程中,发生了异常!\n{1}", templateFile, ex)); } }
private void BuildAllFile(List <Chapter> cList) { List <Chapter> chapterList = new List <Chapter>(); foreach (var c in cList) { Chapter myChapter = new Chapter(); myChapter.BookCode = c.BookCode; myChapter.ChapterCode = c.ChapterCode; myChapter.ChapterName = c.ChapterName; // 取得行列表. myChapter.Lines = lineService.GetChapterLineList(c.ChapterCode); chapterList.Add(myChapter); } // Velocity 初始化. // 从配置文件读取配置信息. Velocity.Init("nvelocity.properties"); // 创建 Velocity Context VelocityContext context = new VelocityContext(); // 将列表的数据, 以 ChapterList 作为名称,放入 context. context.Put("ChapterList", chapterList); // 模版. Template template = null; string templateFile = this.cboTemplate.Text; // 尝试加载模版文件. try { template = Velocity.GetTemplate(templateFile); } catch (ResourceNotFoundException) { MyMessage.Fail(String.Format("未能找到模板文件:{0}", templateFile)); } catch (ParseErrorException pee) { MyMessage.Fail(String.Format("解析模板文件 {0} 发生了异常!\n{1}", templateFile, pee)); } try { // 处理模版信息. if (template != null) { string fileName = String.Format("{0}\\{1}.{2}", this.txtOutputPath.Text, this.cboBooks.SelectedValue, this.cboTemplate.SelectedFileType); using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.UTF8)) { template.Merge(context, sw); } } } catch (Exception ex) { MyMessage.Fail(String.Format("根据模板文件 {0} 生成文档的过程中,发生了异常!\n{1}", templateFile, ex)); } }