阅读背景:

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);
        }
/// <summary>
        /// 
     



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: