简体   繁体   English

使用C#在excel中制作多个工作表

[英]Producing multiple worksheets in excel using C#

I need to produce a C# program which will create two intro worksheets, and a 3rd worksheet that will be made repeatedly for every entry in a database. 我需要生成一个C#程序,该程序将创建两个介绍性工作表,以及一个将为数据库中的每个条目重复制作的第三个工作表。 I am unsure which is the best way to produce multiple worksheets. 我不确定哪种是生成多个工作表的最佳方法。 This last sheet must be able to retain some formating and range names. 最后一页必须能够保留一些格式和范围名称。 Is copying this last sheet better, or is creating a new worksheet better? 是复制最后一张纸更好还是创建新工作表更好?

SpreadsheetGear for .NET can do it. .NET的SpreadsheetGear可以做到。

See the " Worksheet with Chart to Multiple Worksheets with Charts " sample on the Excel Reporting samples page here (there are other samples on that page which might prove useful). 请参见此处的Excel Reporting示例页面上的“ 带有图表的工作表到带有图表的多个工作表 ”示例(该页面上还有其他示例可能证明是有用的)。

You can download a free trial here . 您可以在此处下载免费试用版。

Disclaimer: I own SpreadsheetGear LLC 免责声明:我拥有SpreadsheetGear LLC

It depends on what you mean by better. 这取决于您的意思是更好。 If it's easier to start with a clean slate for each sheet and just recopy the elements that are common, then creating new worksheets is better. 如果从一张干净的表开始比较容易,而只是重新复制常见的元素,那么创建新的工作表会更好。

You could also create a temporary "template" sheet with the common elements already on it, and make copies of that. 您还可以创建一个临时的“模板”工作表,上面已经有常见元素,并对其进行复制。

Documentation for the Copy function is here: 复制功能的文档在这里:
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.worksheet.copy(VS.80).aspx http://msdn.microsoft.com/zh-cn/library/microsoft.office.tools.excel.worksheet.copy(VS.80).aspx

Thank you for your reply, is if possible to copy a "template" worksheet and retain the formating and ranged names form the original or not? 感谢您的答复,是否可以复制“模板”工作表并保留原始格式和名称? – Slapdash 58 mins ago – Slapdash 58分钟前

Yes; 是; that's possible. 那是可能的。 In the situation you describe I'd go for the copying option. 在这种情况下,您描述我会选择复制选项。

Copying the third worksheet would be best. 复制第三张工作表是最好的。 Using Microsoft.Office.Interop.Excel.dll, this is what the copy could look like (mExcelWorkbook is the template Workbook you want to edit): 使用Microsoft.Office.Interop.Excel.dll,这是副本的样子(mExcelWorkbook是您要编辑的模板Workbook):

  /// <summary>
  /// Copy a worksheet to the specified index
  /// </summary>
  /// <param name="worksheetIndex"></param>
  private void CopyWorksheetToIndex(int worksheetIndex, int copyToIndex)
  {
     if (mExcelWorkbook != null)
     {
        // Reference to the worksheet
        Worksheet sheet_to_copy = (Worksheet)mExcelWorkbook.Worksheets[worksheetIndex];
        // Set the Tab Name to something
        sheet_to_copy.Name = "Copied From " + worksheetIndex + " To " + copyToIndex;
        // Copy the worksheet to the end of the worksheets
        sheet_to_copy.Copy(System.Reflection.Missing.Value, mExcelWorkbook.Worksheets[copyToIndex]);
     }
  }

Then you could just call like this: 然后,您可以像这样调用:

  if (mExcelWorkbook != null)
  {
     CopyWorksheetToIndex(3, mExcelWorkbook.Worksheets.Count);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM