• 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();
                    }
                }
            }

  • 相关阅读:
    poj3180 The Cow Prom
    洛谷P1434 滑雪
    洛谷P1199 三国游戏
    洛谷P1230 智力大冲浪
    洛谷P1012 拼数
    洛谷P1106 删数问题
    bzoj3538 [Usaco2014 Open]Dueling GPS
    Android(java)学习笔记134:Android数据存储5种方式总结
    Android(java)学习笔记133:Eclipse中的控制台不停报错Can't bind to local 8700 for debugger
    Android(java)学习笔记132:eclipse 导入项目是提示:某些项目因位于工作空间目录中而被隐藏。
  • 原文地址:https://www.cnblogs.com/wujialongcn/p/1930949.html
Copyright © 2020-2023  润新知