tnblog
首页
登录

根据客户端勾选信息,根据模板批量导出word

332人阅读 2019/7/3 9:44 总访问:2876 评论:0 手机 收藏
分类: .net

                 

 /// <summary>
        /// 导出
        /// <param name="param">前端值数组</param>
        /// <returns></returns>

        public  ActionResult Word(string param)
        {
            try
            {


                ResultInfo result = new ResultInfo();
                JavaScriptSerializer jss = new JavaScriptSerializer();
                //将字符串转换成json对象          
                List<InfoHelperClass> cp = jss.Deserialize<List<InfoHelperClass>>(param);
                //string logpath = Server.MapPath("..") + "/Exportfile/log.txt";
                //string datastring = "";
                //int k = 0;
                string filename = "";
                //声明书签数组  
                object[] oBookMark = new object[9];
                 //赋值书签名     ,也是模板书签名 
                oBookMark[0] = "Id";  //第几期   
                oBookMark[1] = "Department";  //单位  
                oBookMark[2] = "Sender";  //拟稿     
                oBookMark[3] = "hegao";  //核稿 
                oBookMark[4] = "issecrecy";//是否泄密     
                oBookMark[5] = "createtime";//上报时间
                oBookMark[6] = "Seconduser";//签发
                oBookMark[7] = "title";  //标题       
                oBookMark[8] = "content";//正文

                foreach( var item in cp )
                {
                     //创建文档
                    object oMissing = System.Reflection.Missing.Value;
                    //创建一个Word应用程序实例            
                    Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application();
                    //设置为不可见            
                    oWord.Visible = false;
                    //模板文件地址            
                    object oTemplate = Server.MapPath("..") + "/Template/InformationTemplate.docx";
                    //一模板为基础生成文档
                    Microsoft.Office.Interop.Word._Document oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);
                    
                    //赋值数据到书签的位置  
                    oDoc.Bookmarks.get_Item(ref oBookMark[0]).Range.Text = item.Id.ToString();
                    oDoc.Bookmarks.get_Item(ref oBookMark[1]).Range.Text = item.Department;
                    oDoc.Bookmarks.get_Item(ref oBookMark[2]).Range.Text = item.Sender;
                    oDoc.Bookmarks.get_Item(ref oBookMark[3]).Range.Text = item.Firstuser;
                    if (item.issecrecy == 0)
                    {
                        oDoc.Bookmarks.get_Item(ref oBookMark[4]).Range.Text = item.issecrecy.ToString("是");
                    }
                    else {
                        oDoc.Bookmarks.get_Item(ref oBookMark[4]).Range.Text = item.issecrecy.ToString("否");
                    }
                   
                    // oDoc.Bookmarks.get_Item(ref oBookMark[5]).Range.Text = item.createtime.ToString();
                    oDoc.Bookmarks.get_Item(ref oBookMark[5]).Range.Text = item.createtime.ToLongDateString().ToString();
                    oDoc.Bookmarks.get_Item(ref oBookMark[6]).Range.Text = item.Seconduser;
                    oDoc.Bookmarks.get_Item(ref oBookMark[7]).Range.Text = item.title;
                    oDoc.Bookmarks.get_Item(ref oBookMark[8]).Range.Text = item.content;


                    //k++;
                    ////输出日志
                    //datastring = item.Id.ToString() + " |  " + item.Department.ToString() + " |  " + item.Sender.ToString() + " |  " + item.Firstuser.ToString() + " |  " + item.issecrecy.ToString() + " |  " + item.createtime.ToString() + " |  " + item.Seconduser.ToString() + " |  " + item.title.ToString() + " | " + item.content.ToString();
                    //StreamWriter sw = new StreamWriter(logpath, true);
                    //sw.WriteLine(datastring);
                    //sw.WriteLine(k);
                    //sw.Close();
                      //导出信息名称
                    filename = item.title + "-上报信息-" + item.Id + ".doc";


                    //生成word ,并保存在文件夹中             
                    object filepath = Server.MapPath("..") + "/Exportfile/" + filename;
                    oDoc.SaveAs(ref filepath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                    oDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                    //关闭word                 
                    oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
                    oDoc = null;
                    oWord = null;

                }
               
                return Json(result);

            }
            catch (Exception e)
            {
                //ExceptionContext mExceptionContext = new ExceptionContext(new ControllerContext(), e);
                //OnException(mExceptionContext);
                string aa = e.Message;
                return null;
            }
        }


评价
老子许灵灵,写字第一名
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2020TNBLOG.NET