• C# Excel 单元格编辑


    public class ExcelReport
        {
      //Excel 文件修改要引用COM组件Microsoft Excel 11.0 Object Library
      //using Microsoft.Office.Interop.Excel;
      /// <summary>
            /// 单元格修改
            /// </summary>
            /// <param name="filePath">excel路径</param>
      public void writeExcelEdit(string filePath)
      {
       Application myExcel = null;//引用Excel Application类別
                Workbook myBook = null;//引用活页簿类別
                Worksheet mySheet = null;//引用工作表类別
                Range myRange = null;//引用Range类別
                try
                {
                    myExcel = new Application();//实例化Excel Application
                    myBook = myExcel.Workbooks._Open(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    myExcel.DisplayAlerts = false;//停用警告
                    myExcel.Visible = false;      //Excel 不可见
                    mySheet = myBook.Worksheets[1];//Excel文件打开工作簿的第一个文件
        
        int rowindex = 1;//要修改的单元格行
                    int columnIndex = 2;//单元格列
        {//修改单元格
         myRange = mySheet.Cells[rowindex, columnIndex];
                        myRange.Value = 12;//把坐标是[1,2]的单元格值修改成12
        }
        {//修改单元格引用函数=Sum(A1:A3)
         myRange = mySheet.Cells[rowindex, columnIndex];
                        string strColName1 = getExcelColumnLabel(cindex + 1);
                        myRange.Formula = string.Format("=SUM({0}:{1})", strColName1 + (rindex-3), strColName1 + (rindex - 1));
        }
        
        myBook.SaveAs(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                    myBook.Close(false, Type.Missing, Type.Missing);//关闭
                    myExcel.Quit();//关闭Excel
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);//释放Excel资源
       }
       catch(Exception ex)
       {
        throw new Exception(ex);
       }
       finally
       {
        myRange = null;
                    mySheet = null;
                    myBook = null;
                    myExcel = null;
                    GC.Collect();//强制垃圾回收
       }
      }
      
      //
      //单元格格式修改都可以用Range类来实现
      //
      
      /// <summary>
            /// 根据列编号获取列别名
            /// </summary>
            /// <param name="num">列编号(从0开始)</param>
            /// <returns></returns>
            private String getExcelColumnLabel(int num)
            {
                String temp = "";
                double i = Math.Floor(Math.Log(25.0 * (num) / 26.0 + 1) / Math.Log(26)) + 1;
                if (i > 1)
                {
                    double sub = num - 26 * (Math.Pow(26, i - 1) - 1) / 25;
                    for (double j = i; j > 0; j--)
                    {
                        //temp = temp + (char)(sub / Math.Pow(26, j - 1) + 65);//列号从0开始
                        temp = temp + (char)(sub / Math.Pow(26, j - 1) + 64);  //列号从1开始
                        sub = sub % Math.Pow(26, j - 1);
                    }
                }
                else
                {
                    //temp = temp + (char)(num + 65);//列号从0开始
                    temp = temp + (char)(num + 64);  //列号从1开始
                }
                return temp;
            }
        }

  • 相关阅读:
    windows 服务中托管asp.net core
    asp.net core自定义端口
    asp.net core 2.1 部署IIS(win10/win7)
    Centos7 安装Tomcat并运行程序
    centos7 安装java运行环境
    linux(centos7) 常用命令和快捷键 持续验证更新中...
    CentOS7 设置yum源
    dotnet core 入门命令
    二项式系数学习笔记
    [容斥原理][莫比乌斯反演] Codeforces 803F Coprime Subsequences
  • 原文地址:https://www.cnblogs.com/lonelyofsoul/p/ExcelEdti.html
Copyright © 2020-2023  润新知