-1

我已经编写了代码来将工作表的副本复制到一个新的 excel 文件中。下面是我的 C# 代码,问题是缺少一些命名单元格,我在 Excel-2010 中检查了这一点。

C#代码:

string strPath=@"C:\NewFolder\somexcelfile.xls";
Excel.Workbook souWorkbook = xCel.Workbooks.Open(strPath);
Excel.Workbook destWorkbook = xCel.Workbooks.Add();

foreach (Excel.Worksheet wkSheet in souWorkbook.Worksheets)
{
   wkSheet.Copy(destWorkbook.Worksheets[1]);
}
destWorkbook.SaveCopyAs("C:\NewFolder\copy.xls");
4

1 回答 1

0

如果您需要复制命名单元格,您可以使用以下代码示例

Sub Copy_All_Defined_Names()
   ' Loop through all of the defined names in the active
   ' workbook.
     For Each x In ActiveWorkbook.Names
      ' Add each defined name from the active workbook to
      ' the target workbook ("Book2.xls" or "Book2.xlsm").
      ' "x.value" refers to the cell references the
      ' defined name points to.
      Workbooks("Book2.xls").Names.Add Name:=x.Name, _
         RefersTo:=x.Value
   Next x
End Sub
于 2013-04-18T15:55:43.587 回答