protected void btnGenerateFile_Click(object sender, EventArgs e) { string tempalte = "Templates/template.htm";//相对目录 TestInfo info = new TestInfo(); info.Title = "测试标题"; info.Content = "测试内容,这是测试内容"; info.Datetime = DateTime.Now; NVelocityHelper adapter = new NVelocityHelper(tempalte); adapter.AddKeyValue("title", "This is a title") .AddKeyValue("content", "This is a Content") .AddKeyValue("datetime", System.DateTime.Now) .AddKeyValue("TestInfo", info); adapter.FileNameOfOutput = "testTemplate"; string filePath = adapter.ExecuteFile(); if (!string.IsNullOrEmpty(filePath)) { this.txtCode.InnerHtml = FileUtil.FileToString(filePath, System.Text.Encoding.UTF8); } }
/// <summary> /// 生成图标文件 /// </summary> /// <param name="iconSize">图表尺寸,可选16,32等</param> /// <returns></returns> public ActionResult GenerateIconCSS(int iconSize = 16) { CommonResult result = new CommonResult(); string realPath = Server.MapPath("~/Content/icons-customed/" + iconSize); if (Directory.Exists(realPath)) { List <CListItem> list = GetImageToList(realPath, iconSize); try { //使用相对路径进行构造处理 string template = string.Format("Content/icons-customed/{0}/icon.css.vm", iconSize); NVelocityHelper helper = new NVelocityHelper(template); helper.AddKeyValue("FileNameList", list); helper.FileNameOfOutput = "icon"; helper.FileExtension = ".css"; helper.DirectoryOfOutput = realPath;//指定实际路径 string outputFilePath = helper.ExecuteFile(); if (!string.IsNullOrEmpty(outputFilePath)) { result.Success = true; //写入数据库 bool write = BLLFactory <Icon> .Instance.BatchAddIcon(list, iconSize); } } catch (Exception ex) { LogHelper.WriteLog(LogLevel.LOG_LEVEL_CRIT, ex, typeof(IconController)); result.ErrorMessage = ex.Message; } } else { result.ErrorMessage = "没有找到图片文件"; } return(ToJsonContent(result)); }
protected void btnGenerateString_Click(object sender, EventArgs e) { string tempalte = "Templates/template.htm";//相对目录 TestInfo info = new TestInfo(); info.Title = "测试标题"; info.Content = "测试内容,这是测试内容"; info.Datetime = DateTime.Now; NVelocityHelper adapter = new NVelocityHelper(tempalte); adapter.AddKeyValue("title", "This is a title") .AddKeyValue("content", "This is a Content") .AddKeyValue("datetime", System.DateTime.Now) .AddKeyValue("TestInfo", info); this.txtCode.InnerHtml = adapter.ExecuteString(); }