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

来源:互联网 发布:朔州seo快速优化软件 编辑:程序博客网 时间:2024/06/15 09:16

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
原创粉丝点击