星空网 > 软件开发 > ASP.net

Csharp: Create Excel Workbook or word from a Template File using aspose.Word 14.5 and aspose.Cell 8.1

winform:

/// <summary>    ///     /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    private void btnGenWord_Click(object sender, EventArgs e)    {      Dictionary<string, string> dictSource = new Dictionary<string, string>();      dictSource.Add("NO", "T0001");      dictSource.Add("INDUSTRY", "捷为工作室");      dictSource.Add("NAME", "塗聚文");      string templateFile =("Templates/Templates.doc");      Aspose.Words.Document doc = new Aspose.Words.Document(templateFile);      //使用文本方式替换      foreach (string name in dictSource.Keys)      {        doc.Range.Replace(name, dictSource[name], true, true);      }      #region 使用书签替换模式      Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks["SEX"];      if (bookmark != null)      {        bookmark.Text = "男";      }      bookmark = doc.Range.Bookmarks["TEL"];      if (bookmark != null)      {        bookmark.Text = "13824350518*";      }      #endregion            doc.Save("testAdvice"+DateTime.Now.ToString("yyyyMMddHHmmssfff")+".docx",Aspose.Words.SaveFormat.Docx);    }    /// <summary>    ///     /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    private void btnGenExcel_Click(object sender, EventArgs e)    {      Dictionary<string, string> dictSource = new Dictionary<string, string>();      dictSource.Add("NO", "T0001");      dictSource.Add("INDUSTRY", "捷为工作室");      dictSource.Add("NAME", "塗聚文");      string templateFile = ("Templates/Templates.xls");      WorkbookDesigner designer = new WorkbookDesigner();      //designer.Workbook.FileName=templateFile;      Aspose.Cells.Workbook work = new Workbook(templateFile);      designer.Workbook.Copy(work);        Aspose.Cells.Worksheet worksheet = designer.Workbook.Worksheets[0];      worksheet.Name = "geovindu";      //使用文本替换      foreach (string name in dictSource.Keys)      {        worksheet.Replace(name, dictSource[name]);      }      //使用绑定数据方式替换      designer.SetDataSource("SEX", "男");      designer.SetDataSource("TEL", "13824350518*");      designer.Process();         designer.Workbook.Save("testAdvice.xlsx",Aspose.Cells.SaveFormat.Xlsx);    }

  

webform:

/// <summary>    /// https://github.com/aspose-words/Aspose.Words-for-.NET    /// https://asposewords.codeplex.com/    /// https://asposednn.codeplex.com/    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    protected void btnGenWord_Click(object sender, EventArgs e)    {      Dictionary<string, string> dictSource = new Dictionary<string, string>();      dictSource.Add("NO", "T0001");      dictSource.Add("INDUSTRY", "捷為工作室");      dictSource.Add("NAME", "涂聚文");      string templateFile = Server.MapPath("./Templates/Templates.doc");      Aspose.Words.Document doc = new Aspose.Words.Document(templateFile); //veb: 14.5      //使用文本方式替换      foreach (string name in dictSource.Keys)      {        doc.Range.Replace(name, dictSource[name], true, true);      }      #region 使用书签替换模式      Aspose.Words.Bookmark bookmark = doc.Range.Bookmarks["SEX"];      if (bookmark != null)      {        bookmark.Text = "男";      }      //书签方式      bookmark = doc.Range.Bookmarks["TEL"];      if (bookmark != null)      {        bookmark.Text = "13824350518*";      }      #endregion      string savefile = Server.MapPath("./DuFile/geovindu.docx");      doc.Save(savefile, Aspose.Words.SaveFormat.Docx);      Response.Clear();      Response.Buffer = true;      //以字符流的形式下载文件         string fileName = "geovindu.docx"; //下載文件名稱      FileStream fs = new FileStream(savefile, FileMode.Open);      byte[] bytes = new byte[(int)fs.Length];      fs.Read(bytes, 0, bytes.Length);      fs.Close();      Response.ContentEncoding = System.Text.Encoding.UTF8;      Response.HeaderEncoding = System.Text.Encoding.UTF8;           Response.ContentType = "application/octet-stream";      //通知浏览器下载文件而不是打开         Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));      //Response.AddHeader("Content-Length", fs.Length.ToString());      Response.BinaryWrite(bytes);      Response.Flush();      Response.End();    }    /// <summary>    /// http://aspose.github.io/    /// https://github.com/asposemarketplace/Aspose_for_Open

  




原标题:Csharp: Create Excel Workbook or word from a Template File using aspose.Word 14.5 and aspose.Cell 8.1

关键词:ASP

ASP
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Wish物流跟踪信息修改时间延长至46天(附疫情期支持政策汇总):https://www.ikjzd.com/articles/116355
观察|疫情影响下日韩等国这些变化尤其值得注意:https://www.ikjzd.com/articles/116356
干货|螺旋式爆款打造法就是用逆袭思维做运营:https://www.ikjzd.com/articles/116357
海外红人KOL,原来靠这些社媒赚钱!:https://www.ikjzd.com/articles/116358
重要!MasterCard发布2020拒付新标准!:https://www.ikjzd.com/articles/116359
如何极致地书写及优化亚马逊listing标题?:https://www.ikjzd.com/articles/11636
去日本入住酒店,东西随意用却有一个特殊“要:https://www.vstour.cn/a/411241.html
中国有哪些著名的酒店品牌。:https://www.vstour.cn/a/411242.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流