asp.net导出Excel文件_本机测试通过(服务器可能因权限或未安装office而不能运行)

        public void ProcessRequest(HttpContext context)
        {
            HttpResponse Response = context.Response;
            HttpServerUtility Server = context.Server;


            DateTime beforeTime = DateTime.Now;
            Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.ApplicationClass();

            try
            {
                Microsoft.Office.Interop.Excel.Workbook xBook = null;
                xApp.Visible = false;

                string filePath = Server.MapPath("后台新增导出PI模板.xls");

                xBook = xApp.Workbooks._Open(filePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                Microsoft.Office.Interop.Excel.Worksheet xSheet = (Microsoft.Office.Interop.Excel.Worksheet)xBook.Sheets[1];
                Microsoft.Office.Interop.Excel.Range rng = xSheet.get_Range("E2", Missing.Value);
                rng.Value2 = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "  吴家龙";
                // xApp.Save(@"c:\C:\Users\Administrator\Desktop\2010.xls");

                //保存修改
                xBook.Save();

                DateTime afterTime = DateTime.Now;

                //wb.Close(null, null, null);
                xApp.Workbooks.Close();
                xApp.Quit();
                KillExcelProcess(beforeTime, afterTime);

 

                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "utf-8";  //("GB2312");为简体中文
                //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
                //filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以
                //为:.doc    .xls    .txt   .htm  
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xls");
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); //GB2312 
                //Response.ContentType指定文件类型 可以为application/ms-excel ,application/ms-//word,application/ms-txt,application/ms-html 或其他浏览器可直接支持文档 
                Response.ContentType = "application/ms-excel";
                Response.WriteFile(filePath);
                Response.End();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 方法名称: KillExcelProcess
        /// 内容描述: 用Process方法结束Excel进程
        /// 实现流程:
        /// 作    者: 吴家龙
        /// 日    期: 2011-01-07 21:29
        /// </summary>
        public void KillExcelProcess(DateTime beforeTime, DateTime afterTime)
        {
            Process[] myProcesses;
            DateTime startTime;
            myProcesses = Process.GetProcessesByName("Excel");

            //得不到Excel进程ID,暂时只能判断进程启动时间
            foreach (Process myProcess in myProcesses)
            {
                startTime = myProcess.StartTime;

                if (startTime > beforeTime && startTime < afterTime)
                {
                    myProcess.Kill();
                }
            }
        }

posted @ 2011-01-08 22:18  Own_nothing  阅读(460)  评论(1编辑  收藏  举报
广州科讯信息科技有限公司