如何在Excel电子表格上通过C#更改工作表名称 [英] How do I Change the Sheet Name from C# on an Excel Spreadsheet

查看:358
本文介绍了如何在Excel电子表格上通过C#更改工作表名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#应用程序,在其中我从数据库中的数据创建许多Excel文件.这部分工作正常.但是,我的用户问是否可以修改工作表标签以反映数据库中的字段.这听起来很简单,但是,当我尝试重设名称时,它告诉我它是只读的,无法设置.我尝试了以下方法,但没有用:

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:

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

也尝试过:

xlApp.Name = "NewTabName";

我进行了一次Google搜索,并发现了一些对我也无效的方法.并且一些响应表明它是只读的,无法完成.

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

这篇关于如何在Excel电子表格上通过C#更改工作表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆