如何在Excel电子表格中从c#更改表名

时间:2022-09-28 16:35:11

I have a C# application where I am creating numerous Excel Files from Data in a Database. 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:

我有一个c#应用程序,我正在从数据库中的数据创建大量Excel文件。这零件工作正常。但是,我的用户询问是否可以修改sheet选项卡来反映来自数据库的字段。这听起来很简单,但是当我尝试重新设置名字时,它告诉我它是只读的,不能设置。

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.

这看起来应该很简单。我怎么做呢?

1 个解决方案

#1


22  

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”;

#1


22  

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”;