SetDataFlags .InserCells enum選項應該允許在僅由DataTable填充的範圍內選擇格式。不過,也有具體要求,使這項工作正常,如在規定的documentation:
「當指定該標誌[而insertCells],你必須提供確切兩行數據的範圍,由一排列前如果未指定SpreadsheetGear.Data.SetDataFlags.NoColumnHeaders,則標題爲「。
「數據的兩行」應格式化,但您需要(內部/填充顏色等)。 SpreadsheetGear附帶的Explorer示例解決方案(可在開始菜單/屏幕的SpreadsheetGear文件夾中找到)包含一個示例,該示例演示了Reporting> DataSet to Workbook。
UPDATE
以下是一個演示如何使用NoColumnHeaders使用而insertCells標誌相結合的例子。此代碼假定您已經使用格式化範圍設置了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
澄清 - 表中的列數和行數是否不確定?否則,如果總是有固定數量的列和行,可以預設工作表模板中目標區域的格式。 – Xcheque
@Xcheque - 列數是已知的,它們在模板中被預先格式化,它只是行(不確定的數量)是問題。我不想格式化超過所用行數的列,如果我在模板中的整個列上設置填充顏色,會發生這種情況。我無法使SpreadsheetGear插入行爲像Excel中的手動插入,其中插入的行採用上面插入的格式。 –