• asp.net读取execl模板并填充数据,关闭进程


    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
        <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
        <script type="text/javascript">
            function getExecl() {
                $.ajax({
                    type: 'POST',
                    url: 'ashx/execl.ashx',
                    success: function (data) {
                        var re = eval(data);
                        for (var i in re) {
                            if (re[i].num > 0)
                                alert(re[i].num);
                        }
                    },
                    error: function (data) { }
                });
    
            }
        
        
        </script>
    </head>
    前台代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Microsoft.Office.Interop.Excel;
    using System.Runtime.InteropServices;
    using System.Web.Script.Serialization;
    
    namespace WebApplication1.ashx
    {
        /// <summary>
        /// Summary description for execl
        /// </summary>
        public class execl : IHttpHandler
        {
            [DllImport("User32.dll", CharSet = CharSet.Auto)]
            public static extern int GetWindowThreadProcessId(IntPtr hwnd, out   int ID);
            JavaScriptSerializer json = new JavaScriptSerializer();
            public void ProcessRequest(HttpContext context)
            {
                 context.Response.ContentType = "text/plain";
                 int s= getExecl();
                 MessageInfo MessageRe = new MessageInfo(s, null);
                 string returnValue = json.Serialize(MessageRe);
                 returnValue = "[" + returnValue + "]";
                 context.Response.Write(returnValue);
            }
            private int getExecl()
            {
                int num = 0;
                try
                {
                    ApplicationClass excel = new ApplicationClass();
                    string time = DateTime.Now.ToString("yyyyMMddHHmmss");
                    string path = @"H:exel" + time + ".xlsx";
                    excel.Workbooks.Open(@"H:exel1.xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    excel.Visible = false; //设置可见性
                    Worksheet workshet = (Worksheet)excel.Worksheets.get_Item(1);
                    workshet.Cells[3, 1] = "11"; //行和列
                    workshet.Cells[4, 1] = "11";
                    workshet.Cells[5, 1] = "11";
                    workshet.Cells[6, 1] = "11";
                    workshet.SaveAs(path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    excel.Quit(); //退出
                    IntPtr t = new IntPtr(excel.Hwnd); //运用句柄
                    int k = 0;
                    GetWindowThreadProcessId(t, out   k);
                    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
                    p.Kill(); //杀死当前进程
                    num = 1;
                }
                catch (Exception e) { }
                return num;
            }
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
        
    }
    一般处理程序代码

    参考了地址 http://www.cnblogs.com/zhangchenliang/archive/2011/07/21/2112430.html

  • 相关阅读:
    jni中调用java方法获取当前apk的签名文件md5值
    Android底层驱动开发(一)
    作为原作者你能忍吗?
    SUN dataset图像数据集下载
    计算机视觉和图像处理常用的一些标准图片
    Docker基础技术:DeviceMapper
    Docker基础技术:AUFS
    Docker基础技术:Linux CGroup
    Docker基础技术:Linux Namespace(下)
    Docker基础技术:Linux Namespace(上)
  • 原文地址:https://www.cnblogs.com/tianyiwuying/p/4375504.html
Copyright © 2020-2023  润新知