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 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();
 
 
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值