【问题标题】:How to name excel sheets using interop如何使用互操作命名 Excel 工作表
【发布时间】:2015-08-29 11:34:36
【问题描述】:

我在 Excel 文件中制作了一些表格。我用这段代码同时做了很多:

newSheet2 = (Microsoft.Office.Interop.Excel._Worksheet)newWorkbook_First.Sheets.Add(Type.Missing,Type.Missing,5,Type.Missing);

...但我不知道如何单独命名它们。我认为它必须这样做:

newSheet2[2].name = "hello"

但这给出了一个错误。我该怎么做?

        private static Microsoft.Office.Interop.Excel.ApplicationClass appExcel;
        private static Workbook newWorkbook_First = null;
        private static _Worksheet newSheet2 = null; 

public void excel_create(String path)
        {
            try
            {
                appExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                appExcel.Visible = true;
                newWorkbook_First = appExcel.Workbooks.Add(1);
                newSheet2 = (Microsoft.Office.Interop.Excel._Worksheet)newWorkbook_First.Sheets.Add(Type.Missing,Type.Missing,5,Type.Missing);
                //How to name the sheets now? 
            }
            catch (Exception e)
            {
                Console.Write("Error");
            }
            finally
            {
            }
        }

【问题讨论】:

    标签: c# excel rename excel-interop


    【解决方案1】:

    你很亲密,使用“姓名”,而不是“姓名”:

    Application.ActiveSheet.Name="myName";
    

    【讨论】:

      【解决方案2】:

      请参考以下示例。

      static void Main(string[] args)
          { 
              Microsoft.Office.Interop.Excel.Application oXL;
              Microsoft.Office.Interop.Excel._Workbook oWB;
              Microsoft.Office.Interop.Excel._Worksheet oSheet;
              Microsoft.Office.Interop.Excel.Range oRng;
              object misvalue = System.Reflection.Missing.Value;
              try
              {
                  //Start Excel and get Application object.
                  oXL = new Microsoft.Office.Interop.Excel.Application();
                  oXL.Visible = true;
      
                  //Get a new workbook.
                  oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add(""));
                  oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.Sheets.Add();
      
                  //Add table headers going cell by cell.
                  List<string> header=new List<string>();
                  header.Add("steve");
                  header.Add("JOb");
                  oSheet.Cells[1, 1] = header[0];
                  oSheet.Cells[1, 2] = header[1];
                  oSheet.Cells[1, 3] = "Salary";
                  oSheet.Cells[1, 4] = "Token 1 Rs";
      
                  //Format A1:D1 as bold, vertical alignment = center.
                  oSheet.get_Range("A1", "D1").Font.Bold = true;
                  oSheet.get_Range("A1", "D1").VerticalAlignment =
                      Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
      
                  // Create an array to multiple values at once.
                  string[,] saNames = new string[5, 2];
      
                  saNames[0, 0] = "John";
                  saNames[0, 1] = "Smith";
                  saNames[1, 0] = "Tom";
                  saNames[4, 1] = "Johnson";
      
                  //Fill A2:B6 with an array of values (First and Last Names).
                  oSheet.get_Range("A2", "B6").Value2 = saNames;
      
                  //Fill C2:C6 with a relative formula (=A2 & " " & B2).
                  oRng = oSheet.get_Range("C2", "C6");
                  oRng.Formula = "=A2 & \" \" & B2";
      
                  //Fill D2:D6 with a formula(=RAND()*100000) and apply format.
                  oRng = oSheet.get_Range("D2", "D6");
                  oRng.Formula = "=RAND()*100000";
                  oRng.NumberFormat = "$0.00";
      
                  //AutoFit columns A:D.
                  oRng = oSheet.get_Range("A1", "D1");
                  oRng.EntireColumn.AutoFit();
      
                  oXL.Visible = false;
                  oXL.UserControl = false;
      
                  oWB.SaveAs("c:\\test506.xls", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing,
              false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,
              Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
      
                  oWB.Close();
              }
              catch (Exception ex)
              {
      
              }
      

      【讨论】:

      • 请不要只是转储一些代码。解释您的代码实际上是如何解决问题的。
      猜你喜欢
      • 1970-01-01
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多