我需要工作表名称而不是默认工作表名称 [英] I need worksheet name instead of default sheet name
本文介绍了我需要工作表名称而不是默认工作表名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
// 创建Excel应用程序
Microsoft。 Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// 在Excel应用程序中创建新的WorkBook
Microsoft.Office.Interop。 Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
// 在工作簿中创建新的Excel表格
// Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
工作表工作表= 空跨度>;
// 查看该计划背后的Excel工作表
app.Visible = < span class =code-keyword> false ;
// 获取第一张表的参考。默认情况下,其名称为Sheet1。
// 存储其对工作表的引用
worksheet =(工作表)workbook.Sheets [ Sheet1];
// worksheet =(工作表)workbook.Worksheets.get_Item(0);
worksheet =(工作表)workbook.ActiveSheet;
// 更改活动工作表的名称
int m = 1 ;
int totcolumns = ds.Tables [ 0 ]。Columns.Count; // 获取列号
// worksheet.Name =Sheet1;
ReportsProgress( 35 , 将数据添加到新的Excel文件);
#region add column header
DataRow ch = ds.Tables [ 0 ].Rows [ 0 ];
for ( int j = 1 ; j < totcolumns + 1 ; j ++) // 由raghu30082013添加的行
{
// worksheet.Cells [m,j] =F+ j.ToString();
worksheet.Cells [m,j] = ch [J-1]的ToString(); // 由Siva添加(与excel相同的列标题)2016年4月5日
}
#endregion
我尝试过:
// worksheet =(工作表)workbook.Sheets [Sheet1];
工作表=(工作表)workbook.Worksheets.get_Item( 0 跨度>);
解决方案
参见 _ Worksheet.Name属性(Microsoft.Office.Interop.Excel) [ ^ ]。
Excel interop使用基于1的索引,因此要获得第一张表,您可以这样做:
string name = workbook.Sheets [ 1 ]。姓名;
您不能认为Sheet1将存在,或者默认工作表将始终是该集合中的第一个。
如果您想要当前活动的工作表名称:
string name = ((Excel._Worksheet)Workbook.ActiveSheet)请将.Name;
// creating Excel Application Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); // creating new WorkBook within Excel application Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); // creating new Excelsheet in workbook //Microsoft.Office.Interop.Excel._Worksheet worksheet = null; Worksheet worksheet = null; // see the excel sheet behind the program app.Visible = false; // get the reference of first sheet. By default its name is Sheet1. // store its reference to worksheet worksheet = (Worksheet)workbook.Sheets["Sheet1"]; //worksheet = (Worksheet)workbook.Worksheets.get_Item(0); worksheet = (Worksheet)workbook.ActiveSheet; // changing the name of active sheet int m = 1; int totcolumns = ds.Tables[0].Columns.Count; //get column numbers //worksheet.Name = "Sheet1"; ReportsProgress(35, "adding data into new excel file"); #region adding column header DataRow ch = ds.Tables[0].Rows[0]; for (int j = 1; j < totcolumns + 1; j++) //Line added by raghu30082013 { //worksheet.Cells[m, j] = "F" + j.ToString(); worksheet.Cells[m, j] = ch[j-1].ToString(); //Added by Siva (Same column header as source excel) 05 April 2016 } #endregion
What I have tried:
//worksheet = (Worksheet)workbook.Sheets["Sheet1"]; worksheet = (Worksheet)workbook.Worksheets.get_Item(0);解决方案See _Worksheet.Name property (Microsoft.Office.Interop.Excel)[^].
Excel interop uses 1-based indexes, so to get the first sheet, you can do this:
string name = workbook.Sheets[1].Name;
You can't assume that "Sheet1" will exist, or that the "default sheet" will always be the first one in the collection.
If you want the currently active sheet name:
string name = ((Excel._Worksheet)Workbook.ActiveSheet).Name;
这篇关于我需要工作表名称而不是默认工作表名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文