AddColumnPair
(
Microsoft.Office.Interop.Excel.Workbook workbook,
String worksheetName,
String tableName,
String column1NameBase,
Double column1WidthChars,
String column2NameBase,
Double column2WidthChars
)
{
Debug.Assert(workbook != null);
Debug.Assert( !String.IsNullOrEmpty(worksheetName) );
Debug.Assert( !String.IsNullOrEmpty(tableName) );
Debug.Assert( !String.IsNullOrEmpty(column1NameBase) );
Debug.Assert(column1WidthChars == ExcelTableUtil.AutoColumnWidth ||
column1WidthChars >= 0);
Debug.Assert( !String.IsNullOrEmpty(column2NameBase) );
Debug.Assert(column2WidthChars == ExcelTableUtil.AutoColumnWidth ||
column2WidthChars >= 0);
AssertValid();
ListObject oTable;
if (!ExcelTableUtil.TryGetTable(workbook, worksheetName, tableName,
out oTable) )
{
throw new WorkbookFormatException(String.Format(
"To use this feature, there must be a worksheet named \"{0}\""
+ " that contains a table named \"{1}\"."
+ "\r\n\r\n"
+ "{2}"
,
worksheetName,
tableName,
ErrorUtil.GetTemplateMessage()
) );
}
Int32 iMaximumAppendedNumber = Math.Max(
GetMaximumAppendedNumber(oTable, column1NameBase),
GetMaximumAppendedNumber(oTable, column2NameBase)
);
if (iMaximumAppendedNumber != 0)
{
String sStringToAppend =
" " + (iMaximumAppendedNumber + 1).ToString();
column1NameBase += sStringToAppend;
column2NameBase += sStringToAppend;
}
ListColumn oListColumn1, oListColumn2;
if (
!ExcelTableUtil.TryAddTableColumn(oTable, column1NameBase,
column1WidthChars, null, out oListColumn1)
||
!ExcelTableUtil.TryAddTableColumn(oTable, column2NameBase,
column2WidthChars, null, out oListColumn2)
)
{
FormUtil.ShowWarning("The columns weren't added.");
}
ExcelUtil.ActivateWorksheet(oTable);
}