简体   繁体   English

如何在 Excel 电子表格上从 C# 更改工作表名称

[英]How do I Change the Sheet Name from C# on an Excel Spreadsheet

I have a C# application where I am creating numerous Excel Files from Data in a Database.我有一个 C# 应用程序,我正在从数据库中的数据创建大量 Excel 文件。 This part is working fine.这部分工作正常。 However, my user asked if the sheet tab could be modified to reflect a field from the database.但是,我的用户询问是否可以修改工作表选项卡以反映数据库中的字段。 This sounds simple, however, when I try to reset the name, it tells me that it is read only and cannot be set.这听起来很简单,但是,当我尝试重置名称时,它告诉我它是只读的,无法设置。 I have tried the following and it has not worked:我尝试了以下方法,但没有奏效:

xlApp.Sheets[0].Range["A1"].Value = "NewTabName";

ALSO TRIED:还尝试过:

xlApp.Name = "NewTabName";

I did a google search and saw some other approaches which did not work for me as well.我做了一个谷歌搜索,看到了一些对我也不起作用的其他方法。 And a few responses indicated that it is readonly and could not be done.并且一些回复表明它是只读的,无法完成。

This seems like something that should be simple.这看起来应该很简单。 How can I do it.我该怎么做。

You need to get access to the actual worksheet.您需要访问实际工作表。 Try something like:尝试类似:

  Microsoft.Office.Interop.Excel.Worksheet worksheet = (Worksheet)xlApp.Worksheets["Sheet1"];
  worksheet.Name = “NewTabName”;

Here is a fairly complete example I am copying in from existing code.这是我从现有代码复制的一个相当完整的示例。

Works perfectly on Windows 10 with Excel from Office 365使用 Office 365 中的 Excel 在 Windows 10 上完美运行

Ensure you add a reference to - Microsoft.Office.Interop.Excel确保添加对 - Microsoft.Office.Interop.Excel 的引用

My path for this DLL (may differ depending on office version) - C:\\WINDOWS\\assembly\\GAC_MSIL\\Microsoft.Office.Interop.Excel\\15.0.0.0__71e9bce111e9429c\\Microsoft.Office.Interop.Excel.dll我的这个 DLL 的路径(可能因办公版本而异) - C:\\WINDOWS\\assembly\\GAC_MSIL\\Microsoft.Office.Interop.Excel\\15.0.0.0__71e9bce111e9429c\\Microsoft.Office.Interop.Excel.dll

// Add this at top of C# file -
using Excel = Microsoft.Office.Interop.Excel;

// In your class or function -
private static Excel.Application XlApp = null;
private static Excel.Workbook XlWorkbook = null;

// In your function -
XlApp = new Excel.ApplicationClass();

// Load workbook
XlWorkbook = XlApp.Workbooks.Open(@"Filename.xls",
    0, false, Type.Missing, "", "", true, Excel.XlPlatform.xlWindows,
    Type.Missing, Type.Missing, Type.Missing,
    Type.Missing, true, Type.Missing,
    Type.Missing);

// Get reference to sheet
XlWorksheet = (Excel.Worksheet)XlWorkbook.Worksheets["Sheet1"];
                        
int numsheets = XlWorkbook.Sheets.Count;

// iterates through all sheets (1-n inclusive, not zero based)
for(int i=1;i<=numsheets;i++)
{
    Excel.Worksheet sht = (Excel.Worksheet)XlWorkbook.Worksheets[i];
    
    // Show sheet name
    Console.WriteLine(i+" "+sht.Name);
}

// To save with a same or different filename
XlWorkbook.SaveAs(@"Filename.xls",
    Excel.XlFileFormat.xlWorkbookNormal, "",
    "", false, false,
    Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
    true, Type.Missing, Type.Missing, Type.Missing);

// Close Excel  
XlWorkbook.Close(true, Type.Missing, Type.Missing);
XlApp.Quit();

// Ensure you release resources
releaseObject(XlApp);
releaseObject(XlWorkbook);
releaseObject(XlWorksheet);

Separate function called from above从上面调用的单独函数

private static void releaseObject(object obj)
{
  // try .. catch
  System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为C#生成的Excel电子表格设置工作表名称? - How to set the sheet name for a C# generated Excel spreadsheet? 如何在C#中修改Excel电子表格,另存为其他名称并删除原始电子表格 - How do I in C# modify an Excel spreadsheet, save it as another name and delete the original spreadsheet 如何将C#对象列表导出到Excel电子表格? - How do I export a C# object list to an excel spreadsheet? 从C#更改Excel工作表中的值 - change a value in an excel sheet from c# 如何使用OLEDB for C#在Excel中执行“从[sheet1 $]其中列为L的*中选择*”? - How can I do this “select * from [sheet1$] where column is L” in Excel using OLEDB for C#? 如何使用C#清除Google Spreadsheet中指定工作表的所有格式? - How do I clear all formatting for a specified sheet within a Google Spreadsheet using C#? 如何按名称获取和比较Excel电子表格中单元格的内容? - How do I get and compare the contents of a cell in an Excel spreadsheet by name? 如何使用C#在下拉列表中显示Excel工作表名称 - How can show Excel Sheet Name in dropdownlist using C# 如何在C#中动态重命名Excel工作表名称 - How to rename excel sheet name dynamically in C# 如何使用索引C#获取Excel工作表名称 - how to get excel sheet name using index c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM