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

简介: winform: /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private v

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_OpenXML
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> dictSource = new Dictionary<string, string>();
            dictSource.Add("NO", "T0002");
            dictSource.Add("INDUSTRY", "捷為工作室");
            dictSource.Add("NAME", "涂聚文");
 
            string templateFile = Server.MapPath("./Templates/Templates.xls");
            WorkbookDesigner designer = new WorkbookDesigner();  //Veb:8.1
            Aspose.Cells.Workbook work = new Workbook(templateFile);
            designer.Workbook.Copy(work);  
            //designer.Open(templateFile);
 
            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();
            string savefile = Server.MapPath("./DuFile/geovindu.xlsx");
            designer.Workbook.Save(savefile, Aspose.Cells.SaveFormat.Xlsx);
            string fileName = "geovindu.xlsx"; //下載文件名稱
            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();
 
 
        }


 

目录
相关文章
iframe 在线预览pdf、word、excel、ppt、txt、图片、视频
iframe 在线预览pdf、word、excel、ppt、txt、图片、视频
|
2月前
|
小程序
公众号如何添加附传Word、Excel、Pdf、PPT文档
公众号里添加一些文档给公众号粉丝下载,比如课件PPT、申请表Word文档、岗位需求Excel表、大赛入围/获奖名单等。公众号本身是不支持直接上传文件的,但我们可以通过附件小程序“间接”上传文件。
499 0
|
6月前
|
人工智能 算法 安全
使用CodeBuddy实现批量转换PPT、Excel、Word为PDF文件工具
通过 CodeBuddy 实现本地批量转换工具,让复杂的文档处理需求转化为 “需求描述→代码生成→一键运行” 的极简流程,真正实现 “技术为效率服务” 的目标。感兴趣的快来体验下把
266 10
|
Java API Apache
Java编程如何读取Word文档里的Excel表格,并在保存文本内容时保留表格的样式?
【10月更文挑战第29天】Java编程如何读取Word文档里的Excel表格,并在保存文本内容时保留表格的样式?
832 5
|
存储 Java Apache
Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览
Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览
955 0
|
11月前
|
人工智能 自然语言处理 JavaScript
Univer:开源全栈 AI 办公工具,支持 Word、Excel、PPT 等文档处理和多人实时协作
Univer 是一款开源的 AI 办公工具,支持 Word、Excel 等文档处理的全栈解决方案。它具有强大的功能、高度的可扩展性和跨平台兼容性,适用于个人和企业用户,能够显著提高工作效率。
1748 9
Univer:开源全栈 AI 办公工具,支持 Word、Excel、PPT 等文档处理和多人实时协作
|
Python
办公自动化-Python如何提取Word标题并保存到Excel中?
办公自动化-Python如何提取Word标题并保存到Excel中?
268 2
|
Web App开发 前端开发 安全
2024年新一代WebOffice内嵌网页组件,Web网页在线编辑Word/Excel/PPT
WebOffice控件面临兼容性、用户体验和维护难题。随着浏览器更新,依赖插件的技术不再适用,如Chrome不再支持NPAPI和PPAPI。产品普遍不支持多版本Office并存,定制能力弱,升级复杂。猿大师办公助手提供了解决方案,它兼容多种浏览器,包括最新版和国产浏览器,不依赖插件,支持文档对比,具有丰富的功能和接口,兼容多种Office版本,允许源码级定制,提供终身技术支持,并实现静默在线升级。适用于多种行业和操作系统。
949 114
aspose实现word,excel等文件预览
aspose实现word,excel等文件预览

热门文章

最新文章