首页 > 代码库 > C# Excel 单元格编辑

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 = http://www.mamicode.com/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;
        }
    }