SetDataFlags .InserCells 枚举选项应允许在仅由您的 DataTable 填充的范围内选择格式。但是,如文档中所述,要使其正常工作有确切的要求:
“指定此标志 [InsertCells] 时,如果未指定 SpreadsheetGear.Data.SetDataFlags.NoColumnHeaders,则必须提供恰好包含两行数据的范围,前面是列标题行。”
“两行数据”应该按照您的需要进行格式化(内部/填充颜色等)。SpreadsheetGear 附带的 Explorer 示例解决方案(位于开始菜单/屏幕的 SpreadsheetGear 文件夹中)在Reporting > DataSet to Workbook下包含一个示例来演示这一点。
更新
下面的示例演示了如何将 InsertCells 标志与 NoColumnHeaders 结合使用。此代码假定您worksheet
已经设置了格式化范围,并且您可以获得一个 DataTable,其大小对应于格式化范围中的列数。
using SpreadsheetGear;
using SpreadsheetGear.Data;
// Generate DataTable. For this example, let's say it is 5 columns wide and
// 10 rows deep...
DataTable dataTable = // ...get DataTable...
// We're not interested in a "Column Header" row, so for the above dataTable,
// an IRange 5 columns wide and 2 rows deep needs to be specified where those
// two rows are already setup with the desired formatting. In this case B2:F3
// are formatted accordingly.
worksheet.Cells["B2:F3"].CopyFromDataTable(dataTable, SetDataFlags.InsertCells |
SetDataFlags.NoColumnHeaders);
// Sheet should how have B2:F11 populated with your dataTable data, with all
// rows having picked up the formatting as originally specified in B2:F3