• C# 后台更新Excel内容


    private void UpdateSheet(string strFilePath, string strSheetName)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass xlApp = new ApplicationClass();
            Microsoft.Office.Interop.Excel.Workbooks xlWorkbooks;
            Microsoft.Office.Interop.Excel.Workbook xlWorkbook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorksheet;
            System.Type tyWorkbooks;
            System.Reflection.MethodInfo[] methods;
            object objFilePath;
            object oMissing = System.Reflection.Missing.Value;
            strFilePath = Server.MapPath(strFilePath);
            if (!System.IO.File.Exists(strFilePath))
            {
                throw new System.IO.FileNotFoundException();
                return;
            }
            try
            {
                xlApp.Visible = false;
                xlWorkbooks = xlApp.Workbooks;
                tyWorkbooks = xlWorkbooks.GetType();
                methods = tyWorkbooks.GetMethods();
                objFilePath = strFilePath;
                object Nothing = System.Reflection.Missing.Value;
                xlWorkbook = xlApp.Workbooks.Open(strFilePath, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
                
                //xlWorkbook = (Microsoft.Office.Interop.Excel.Workbook)tyWorkbooks.InvokeMember("Open ",
                //System.Reflection.BindingFlags.InvokeMethod,
                //null,
                //xlWorkbooks,
                //new object[] { objFilePath, true, true });
                xlWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkbook.Worksheets[strSheetName];
                xlWorksheet.Cells["10", "M"] = "wenchaozhen";
                xlWorkbook.Save();
                xlWorkbook.Close(oMissing, oMissing, oMissing);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (xlApp != null)
                {
                    xlApp.Quit();
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
                    xlApp = null;
                }
                GC.Collect();
            }
        }
  • 相关阅读:
    JZOJ 1075. 【GDKOI2006】新红黑树
    [CQOI2018]异或序列
    JZOJ 1077. 【GDKOI2006】防御力量
    [TJOI2014]匹配
    JZOJ 1073. 【GDOI2005】山海经
    JZOJ 3745. 【NOI2014模拟7.14】Problem A
    SQL Server 2008 数据库快照
    vSphere 5.0 开发系列(一)vSphere 5.0 环境搭建手顺
    Crm系统帮助
    SQL Server 2008 Entity Data Model 和 Linq to Entities
  • 原文地址:https://www.cnblogs.com/TNTZWC/p/1943109.html
Copyright © 2020-2023  润新知