• Asp.net操作Excel (用Com方式)


       protected void Page_Load(object sender, EventArgs e)
            {
                
    if (!Page.IsPostBack)
                {
                    ApplicationClass app 
    = new ApplicationClass();
                    Workbook workBook 
    = app.Workbooks.Open(@"e:\tt.xls", 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);
                    Worksheet sheet 
    = (Worksheet)workBook.Worksheets[1];
                    Response.Write(sheet.Name + "<br/>");
                    Range range 
    = sheet.get_Range("E7:J112", Type.Missing);
                    Range item 
    = null;
                    
    for (int row = 0; row < range.Rows.Count; row++)
                    {
                        
    for (int col = 0; col < range.Columns.Count; col++)
                        {
                            item 
    = (Range)range.get_Item(row, col);
                            Response.Write(item.Value2);
                            Response.Write(
    " | ");
                        }
                        Response.Write(
    "<br/>");
                    }
                    
    //关闭
                    app.Workbooks.Close();
                    app.Quit();
                    
    //释放
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(item);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

                    item 
    = null;
                    app 
    = null;
                    workBook 
    = null;
                    range 
    = null;

                    GC.Collect();
                    
    //杀进程
                    try{
                    KillExcelProcess();
                    }
    catch{}
                }
            }

            
    public void KillExcelProcess()
            {
                Process[] myProcesses;

                myProcesses 
    = Process.GetProcessesByName("Excel");

                
    foreach (Process myProcess in myProcesses)
                {
                    myProcess.Kill();
                }
            }


  • 相关阅读:
    Python requests乱码的五种解决办法
    apache安装
    Python 跨文件调用函数 + 在一个文件中执行另一个文件
    配置IIS使用Python
    gradle编译
    字符串UTF8转JIS
    cannot be loaded because running scripts is disabled on this system的对策
    您知道eval中的和括号的含义吗
    Python 学习笔记
    windows装mysql一定要装vs和python吗
  • 原文地址:https://www.cnblogs.com/jmax/p/1921532.html
Copyright © 2020-2023  润新知