[英]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.