首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

求解决办法,生成一份word需要30秒,太慢了要提速,同样的小弟我用NPOI控件生成Excel表10份简历才需要1-2秒

2013-11-20 
求解决方法,生成一份word需要30秒,太慢了要提速,同样的我用NPOI控件生成Excel表10份简历才需要1-2秒using

求解决方法,生成一份word需要30秒,太慢了要提速,同样的我用NPOI控件生成Excel表10份简历才需要1-2秒


using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Microsoft.Win32;
using Microsoft.Office.Interop.Word;
using System.Net;
using System.Web;
using System.Data;
using System.Text.RegularExpressions;

namespace OfficeOperate
{
    public class WordOperate
    {
        bool leicai = true;

        #region 新建Word文档
        /// <summary>
        /// 动态生成Word文档并填充内容 
        /// </summary>
        /// <param name="dir">文档目录</param>
        /// <param name="fileName">文档名</param>
        /// <returns>返回自定义信息</returns>
        public static bool CreateWordFile(string dir, string fileName)
        {
            Object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
            Object oMissing = System.Reflection.Missing.Value;

            if (!Directory.Exists(dir))
            {
                //创建文件所在目录
                Directory.CreateDirectory(dir);
            }
            //创建Word文档(Microsoft.Office.Interop.Word)
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
            WordApp.Visible = true;
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Add(
                ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            //保存
            object FileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;//
            object filename = dir + fileName;
            WordDoc.SaveAs(ref filename, ref FileFormat, 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);
            WordDoc.Close(ref saveOption, ref oMissing, ref oMissing);
            WordApp.Quit(ref saveOption, ref oMissing, ref oMissing);
            return true;
            try
            { }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return false;
            }
        }
 #endregion 新建Word文档

        #region 给word文档添加页眉页脚
        /// <summary>
        /// 给word文档添加页眉
        /// </summary>
        /// <param name="filePath">文件名</param>
        /// <returns></returns>
        public static bool AddPageHeaderFooter(string filePath)


        {

            Object saveOption = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;

            Object oMissing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word._Application WordApp = new Application();
            WordApp.Visible = true;
            object filename = filePath;
            Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, 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);

            ////添加页眉方法一:
            WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
            WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("http://www.xxx.com ");//页眉内容

            /*
            ////添加页眉方法二:
            if (WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView ||
                WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView)
            {
                WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;
            }
            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
            WordApp.Selection.HeaderFooter.LinkToPrevious = false;
            WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
            WordApp.Selection.HeaderFooter.Range.Text = "页眉内容";
            */
            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;
            WordApp.Selection.HeaderFooter.LinkToPrevious = false;
            WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
            WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("http://www.xxx.com ");
            
            //跳出页眉页脚设置
            WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;

            //保存
            WordDoc.Save();
            WordDoc.Close(ref saveOption, ref oMissing, ref oMissing);
            WordApp.Quit(ref saveOption, ref oMissing, ref oMissing);
            return true;
            try
            { }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return false;
            }
        }





谁能给个例子或者代码,这样生成实在是太慢了,我用NPOI控件生成Excel表10份简历才需要1-2秒,
而生成word文档需要30秒,

求解决方法~~~~ 直接把预览简历页面的内容去掉头与尾,然后作为Word保存的...
不知道与你是否适用.
[解决办法]
NPOI 2.0操作word
[解决办法]
OpenXML

老虎传送门
[解决办法]

忘说了,样式也要包含在对应的web控件里..
我这里是直接将一段div的内容放进去了的...

谢了

http://tonyqus.sinaapp.com/archives/590
[解决办法]
为什么不用模板去做
[解决办法]
引用:
Quote: 引用:

为什么不用模板去做


有没有例子,发一个我看看啊


http://www.cnblogs.com/kesalin/archive/2012/04/18/open_xml_word.html

热点排行