多线程导出word

来源:互联网 发布:上瘾网络剧全集在线看 编辑:程序博客网 时间:2024/05/16 23:44
 

以下示例将完成批量导出word的功能,由于涉及到批量,为了页面不‘假死’,用到了多线程。至于线程最多能有多少个,网上说没有限制,当时也懒得考虑了,直接多少份就用多少线程。

       导出借用的是水晶报表(Crystal Reports)的现有资源,主要就是稳定,比前一篇导出word的不确定因素相比少了很多。最重要的是,这个页面很复杂,使用了三个子报表,水晶报表的强大之处完全体现出来了。

     如果仔细看,你会发现,不只是导出word,最下面的方法是选择导出的格式,有5种,我所知道常用的,除了Cvs格式都有了,能够满足你的要求了吧。。

  

/// <summary>

    /// 多线程操作    /// </summary>    class threadbody    {        Bill_Word billWord; //        int threadId;//线程编号        string thCustId;   //客户编号        string path;         public threadbody(Bill_Word _billWord, int _threadId, string _thCustId, string _path)        {                  billWord = _billWord;            threadId = _threadId;            thCustId = _thCustId;            path = _path;        }         /// <summary>        /// 线程批量生成        /// </summary>        public void ThreadCreate()        {                ReportDocument myReport = new ReportDocument();                string reportPath = System.Threading.Thread.GetDomain().BaseDirectory + "Reports\\rpt\\rp_word.rpt";                 myReport.Load(reportPath);                //数据填充                DataSet fillDS = billWord.GetFill(thCustId);                myReport.SetDataSource(fillDS);                string fileName = fillDS.Tables[3].Rows[0][7].ToString() + DateTime.Now.ToString("yyyyMMddhhssmm");                if (billWord.Export(path + fileName, "doc", myReport, thCustId)) //如果导出成功,保存记录                {                    clsReports.fExportFilesAction(1,"",fileName + ".doc",fillDS.Tables[3].Rows[0][6].ToString(),thCustId,DateTime.Now,false,Convert.ToDateTime(fillDS.Tables[3].Rows[0][0]).Month + "");                    billWord.BArr_threadw[threadId] = true;//表明此线程结束。                }         }    } /// <summary>    /// 页面类(经过精简,只要相关内容)    /// </summary>     public partial class Bill_Word : System.Web.UI.Page    {         public bool[] BArr_threadw; //每个线程结束标志        public bool BFinish;        public string[] custIds;        public string mes = "现在没信息啦";        private StringBuilder sb_suc = new StringBuilder("");         protected void Page_Load(object sender, EventArgs e)        {            if (Request["action"] == "1")   //批量生成            {                //Thread thCreate = new Thread(new ThreadStart(ThreadCreate));                //thCreate.Start();                //ThreadCreate();                custIds = Request.Cookies["Bill_custs"].Value.Split(',');                //定义线程数组,启动接收线程                Thread[] threadSend = new Thread[custIds.Length];   //多线程                threadbody[] thBody = new threadbody[custIds.Length]; //                for (int j = 0; j < custIds.Length; j++)                {                    thBody[j] = new threadbody(this, j, custIds[j],Server.MapPath("..\\Bill\\Send\\"));                    threadSend[j] = new Thread(new ThreadStart(thBody[j].ThreadCreate));                    threadSend[j].Start();                }                BArr_threadw = new bool[custIds.Length];                MesShow();            }    }         /// <summary>        /// 所有线程结束提示        /// </summary>        public void MesShow()        {            while (true)//等待            {                BFinish = true;                for (int i = 0; i <custIds.Length; i++)                {                    if (BArr_threadw[i] == false)//有未结束线程,等待                    {                        BFinish = false;                        Thread.Sleep(100);                        break;                    }                }                if (BFinish == true)//所有线程均已结束,停止等待,                {                    break;                }            }            if (BFinish == true)            {                 //操作提示                int total = custIds.Length;                int suc = sb_suc.ToString().Split(',').Length;                int fail = total - suc;                //btnSend.AddScript(clsSystem.fGetExtMsgAlert("系统提示", string.Format("总共发送邮件{0}封,成功发送邮件{1}封,失败{2}封", total, suc, fail)));                //GridFiles.Reload();                mes = string.Format("总共生成对账单{0}份,成功生成{1}份,失败{2}份", total, suc, fail);                Response.Write("<script>alert('"+mes+"');</script>");            }         }         /// <summary>        /// 导出到服务器端        /// </summary>        /// <param name="FileName">文件保存路径</param>        /// <param name="Ext">扩展名(doc.pdf.rtf.xls.html)</param>        /// <param name="_report"></param>         public bool Export(string FileName, string Ext,ReportDocument _report,string Id)         {             try             {                 ExportOptions exportOptions = new ExportOptions();                 DiskFileDestinationOptions diskOptions = ExportOptions.CreateDiskFileDestinationOptions();                 exportOptions.ExportFormatType = GetExportFormatType(Ext);                 exportOptions.ExportDestinationType = ExportDestinationType.DiskFile;                 diskOptions.DiskFileName = FileName + "." + Ext;                 exportOptions.ExportDestinationOptions = diskOptions;                 _report.Export(exportOptions);                 if (sb_suc.ToString() == "")                     sb_suc.Append(Id);                 else                     sb_suc.Append("," + Id);                 return true;             }             catch             {                 return false;             }         }         /// <summary>        /// 导出格式        /// </summary>        /// <param name="ext"></param>        /// <returns></returns>         private ExportFormatType GetExportFormatType(string ext)         {             switch (ext)             {                 case "pdf":                      return ExportFormatType.PortableDocFormat;                  case "rtf":                      return ExportFormatType.RichText;                  case "doc":                      return ExportFormatType.WordForWindows;                  case "xls":                      return ExportFormatType.Excel;                  case "html":                      return ExportFormatType.HTML32;                                  default:                     return ExportFormatType.NoFormat;             }         }}
原创粉丝点击