zoukankan      html  css  js  c++  java
  • C#将内容导出到Word到指定模板

    昨天做了下导入导出Excel文件,今天研究了下导出Word文件。 从网上找了半天才找到了一个能导出到指定模板的,在这里总结下。

    导出模板原理就是利用的替换占位符。

    我这里先建立好了一个模板,

    接下来写代码进行导出,

    前端就一段AJAX调用,这里我就不写了,直接上后端代码,看下面:

         /// <summary>
            /// 导出Word文件
            /// </summary>
            /// <returns></returns>
            [HttpPost]
            public ActionResult LeadWord()
            {
                #region 动态创建DataTable数据
                DataTable tblDatas = new DataTable("Datas");
                DataColumn dc = null;
                //赋值给dc,是便于对每一个datacolumn的操作
                dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));
                dc.AutoIncrement = true;//自动增加
                dc.AutoIncrementSeed = 1;//起始为1
                dc.AutoIncrementStep = 1;//步长为1
                dc.AllowDBNull = false;//
                dc = tblDatas.Columns.Add("name", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("sex", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("age", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("str1", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("str2", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("str3", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("str4", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("str5", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("str6", Type.GetType("System.String"));
                dc = tblDatas.Columns.Add("remark", Type.GetType("System.String"));
                DataRow newRow;
                newRow = tblDatas.NewRow();
                newRow["name"] = "张三";
                newRow["sex"] = "";
                newRow["age"] = "11";
                newRow["str1"] = "字符串1";
                newRow["str2"] = "字符串2";
                newRow["str3"] = "字符串3";
                newRow["str4"] = "字符串4";
                newRow["str5"] = "字符串5";
                newRow["str6"] = "字符串6";
                newRow["remark"] = "备注一下";
                tblDatas.Rows.Add(newRow);
                #endregion
                #region word要替换的表达式和表格字段的对应关系
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("$name$", "name");
                dic.Add("$sex$", "sex");
                dic.Add("$age$", "age");
                dic.Add("$str1$", "str1");
                dic.Add("$str2$", "str2");
                dic.Add("$str3$", "str3");
                dic.Add("$str4$", "str4");
                dic.Add("$str5$", "str5");
                dic.Add("$str6$", "str6");
                dic.Add("$remark$", "remark");
                #endregion
                string tempFile = "~/Content/Word/temp.doc";
                string saveFile = "~/Content/Word/1.doc";
                WordUtility w = new WordUtility(tempFile, saveFile);
                w.GenerateWord(tblDatas, dic, null);
                return Content("ok");
            }

    Helper Class(WordUtility.cs)

    using System;
    using System.Collections.Generic;
    using System.Data;
    using Word = Microsoft.Office.Interop.Word;
    using System.IO;
    using System.Windows.Forms;
    using System.Runtime.Remoting.Contexts;
    
    
    namespace Headfree.DefUI
    {
        /// <summary>
        /// 使用替换模板进行到处word文件
        /// </summary>
        public class WordUtility
        {
            private object tempFile = null;
            private object saveFile = null;
            private static Word._Document wDoc = null; //word文档
            private static Word._Application wApp = null; //word进程
            private object missing = System.Reflection.Missing.Value;
    
            public WordUtility(string tempFile, string saveFile)
            {
                tempFile=System.Web.HttpContext.Current.Server.MapPath(tempFile);
                saveFile = System.Web.HttpContext.Current.Server.MapPath(saveFile);           
                this.tempFile = Path.Combine(Application.StartupPath, @tempFile);
                this.saveFile = Path.Combine(Application.StartupPath, @saveFile);
            }
    
            /// <summary>
            /// 模版包含头部信息和表格,表格重复使用
            /// </summary>
            /// <param name="dt">重复表格的数据</param>
            /// <param name="expPairColumn">word中要替换的表达式和表格字段的对应关系</param>
            /// <param name="simpleExpPairValue">简单的非重复型数据</param>
            public bool GenerateWord(DataTable dt, Dictionary<string, string> expPairColumn, Dictionary<string, string> simpleExpPairValue)
            {
                if (!File.Exists(tempFile.ToString()))
                {
                   
                    return false;
                }
                try
                {
                    wApp = new Word.Application();
    
                    wApp.Visible = false;
    
                    wDoc = wApp.Documents.Add(ref tempFile, ref missing, ref missing, ref missing);
    
                    wDoc.Activate();// 当前文档置前
    
                    bool isGenerate = false;
    
                    if (simpleExpPairValue != null && simpleExpPairValue.Count > 0)
                        isGenerate = ReplaceAllRang(simpleExpPairValue);
    
                    // 表格有重复
                    if (dt != null && dt.Rows.Count > 0 && expPairColumn != null && expPairColumn.Count > 0)
                        isGenerate = GenerateTable(dt, expPairColumn);
    
                    if (isGenerate)
                        wDoc.SaveAs(ref saveFile, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
    
                    DisposeWord();
    
                    return true;
                }
                catch (Exception ex)
                {               
                    return false;
                }
            }
    
            /// <summary>
            /// 单个替换 模版没有重复使用的表格
            /// </summary>
            /// <param name="dc">要替换的</param>
            public bool GenerateWord(Dictionary<string, string> dc)
            {
                return GenerateWord(null, null, dc);
            }
    
    
            private bool GenerateTable(DataTable dt, Dictionary<string, string> expPairColumn)
            {
                try
                {
                    int tableNums = dt.Rows.Count;
    
                    Word.Table tb = wDoc.Tables[1];
    
                    tb.Range.Copy();
    
                    Dictionary<string, object> dc = new Dictionary<string, object>();
                    for (int i = 0; i < tableNums; i++)
                    {
                        dc.Clear();
    
                        if (i == 0)
                        {
                            foreach (string key in expPairColumn.Keys)
                            {
                                string column = expPairColumn[key];
                                object value = null;
                                value = dt.Rows[i][column];
                                dc.Add(key, value);
                            }
    
                            ReplaceTableRang(wDoc.Tables[1], dc);
                            continue;
                        }
    
                        wDoc.Paragraphs.Last.Range.Paste();
    
                        foreach (string key in expPairColumn.Keys)
                        {
                            string column = expPairColumn[key];
                            object value = null;
                            value = dt.Rows[i][column];
                            dc.Add(key, value);
                        }
    
                        ReplaceTableRang(wDoc.Tables[1], dc);
                    }
    
    
                    return true;
                }
                catch (Exception ex)
                {
                    DisposeWord();              
                    return false;
                }
            }
    
            private bool ReplaceTableRang(Word.Table table, Dictionary<string, object> dc)
            {
                try
                {
                    object replaceArea = Word.WdReplace.wdReplaceAll;
    
                    foreach (string item in dc.Keys)
                    {
                        object replaceKey = item;
                        object replaceValue = dc[item];
                        table.Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing,
                          ref  missing, ref missing, ref missing, ref missing, ref missing,
                          ref  replaceValue, ref replaceArea, ref missing, ref missing, ref missing,
                          ref  missing);
                    }
                    return true;
                }
                catch (Exception ex)
                {
                    DisposeWord();
                  
                    return false;
                }
            }
    
            private bool ReplaceAllRang(Dictionary<string, string> dc)
            {
                try
                {
                    object replaceArea = Word.WdReplace.wdReplaceAll;
    
                    foreach (string item in dc.Keys)
                    {
                        object replaceKey = item;
                        object replaceValue = dc[item];
                        wApp.Selection.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing,
                          ref  missing, ref missing, ref missing, ref missing, ref missing,
                          ref  replaceValue, ref replaceArea, ref missing, ref missing, ref missing,
                          ref  missing);
                    }
                    return true;
                }
                catch (Exception ex)
                {              
                    return false;
                }
            }
    
            private void DisposeWord()
            {
                object saveOption = Word.WdSaveOptions.wdSaveChanges;
    
                wDoc.Close(ref saveOption, ref missing, ref missing);
    
                saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
    
                wApp.Quit(ref saveOption, ref missing, ref missing); //关闭Word进程
            }
        }
    }

    好了,代码就这么多,来看下导出效果吧:

    ZJ。。。

  • 相关阅读:
    git pull遇到错误:error: Your local changes to the following files would be overwritten by merge:
    angular 过滤器(日期转换,时间转换,数据转换等)
    js 毫秒转天时分秒
    使用Vue-Router 2实现路由功能
    vue-cli中安装方法
    Vue 2.5 发布了:15篇前端热文回看
    es6 语法 (模块化)
    es6 语法 (Decorator)
    es6 语法 (Generator)
    js 判断当前是什么浏览器
  • 原文地址:https://www.cnblogs.com/shuai7boy/p/6984941.html
Copyright © 2011-2022 走看看