internal void InsertSheets(int CopyFrom, int BeforeSheet, int SheetCount, TSheetInfo SheetInfo, bool CopyFilterDatabase) { int aCount = SheetInfo.SourceNames.Count; int DestCount = Count; //Get this before adding the new sheets. for (int i = DestCount - 1; i >= 0; i--) { this[i].ArrangeInsertSheets(BeforeSheet, SheetCount); } for (int i = 0; i < aCount; i++) { if ( CopyFrom >= 0 && NameMustBeCopiedToSheet(CopyFrom, SheetInfo, i) && (CopyFilterDatabase || SheetInfo.SourceNames[i].Name != TXlsNamedRange.GetInternalName(InternalNameRange.Filter_DataBase)) //filterdatabase will be copied when copying autofilters. ) { for (int k = 0; k < SheetCount; k++) { SheetInfo.InsSheet = BeforeSheet + k; TNameRecord name = (TNameRecord.Clone(SheetInfo.SourceNames[i], SheetInfo) as TNameRecord).ArrangeCopySheet(SheetInfo); //this could add its own names, so we need to recheck name wasn't added in next line. if (!NameAlreadyExistsInLocal(SheetInfo.DestFormulaSheet, name.Name)) { Add(name); } CheckInternalNames(SheetInfo.SourceNames[i].OptionFlags, SheetInfo.SourceGlobals); } } } }
/// <summary> /// 创建 [Excel报表模版] 中的“数据表”部分 /// </summary> /// <typeparam name="T">要绑定的集合元素的类型</typeparam> /// <param name="startRow">“数据表”的开始行(从1开始)</param> /// <param name="startColumn">“数据表”的开始列(从1开始)</param> public void CreateExcelReportTemlate <T>(int startRow = 2, int startColumn = 1) where T : class, new() { // 属性集合 int columnCount = 0; PropertyInfo[] propertyArray = typeof(T).GetProperties(); foreach (PropertyInfo property in propertyArray) { object[] attributes = property.GetCustomAttributes(typeof(DisplayNameAttribute), true); if (attributes.Length == 0) { continue; } columnCount++; string propertyName = property.Name; string displayName = ((DisplayNameAttribute)attributes[0]).DisplayName; // 设置列标题 int titleRowIndex = startRow; int titleColumnIndex = columnCount + (startColumn - 1); SetTitleCell(titleRowIndex, titleColumnIndex, displayName); // 设置属性绑定标记 int dataRowIndex = titleRowIndex + 1; int dataColumnIndex = titleColumnIndex; SetDataBindCell(dataRowIndex, dataColumnIndex, propertyName); } // 定义名称区域 char[] excelColumn = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; string 数据表区域 = string.Format("='{0}'!${2}${1}:${3}${1}", SheetName, startRow + 1, excelColumn[startColumn - 1], excelColumn[columnCount - 1]); xls.SetNamedRange(new TXlsNamedRange("__数据表__", 0, 0, 数据表区域)); string 打印区域 = string.Format("='{0}'!$A$1:${2}${1}", SheetName, startRow + 3, excelColumn[columnCount]); xls.SetNamedRange(new TXlsNamedRange(TXlsNamedRange.GetInternalName(InternalNameRange.Print_Area), 1, 32, 打印区域)); }