Beispiel #1
0
        GetAttributeColumn
        (
            ListObject oTable,
            String sAttribute,
            out ListColumn oAttributeColumn,
            out Range oAttributeColumnData
        )
        {
            Debug.Assert(oTable != null);
            Debug.Assert(!String.IsNullOrEmpty(sAttribute));

            if (!ExcelTableUtil.TryGetTableColumn(oTable, sAttribute,
                                                  out oAttributeColumn))
            {
                if (!ExcelTableUtil.TryAddTableColumn(oTable, sAttribute,
                                                      ExcelTableUtil.AutoColumnWidth, null, out oAttributeColumn))
                {
                    goto CannotGetColumn;
                }

                // Wrap the text in the new column's header.

                ExcelTableUtil.WrapTableColumnHeader(oAttributeColumn);

                // This sometimes wraps a single-word header.  Fix it.

                oAttributeColumn.Range.EntireColumn.AutoFit();
            }

            if (ExcelTableUtil.TryGetTableColumnData(oAttributeColumn,
                                                     out oAttributeColumnData))
            {
                // Success.

                return;
            }

CannotGetColumn:

            throw new WorkbookFormatException(
                      "The " + sAttribute + " column couldn't be added."
                      );
        }
Beispiel #2
0
        TryGetRequiredColumnInformation
        (
            GraphMetricColumn oGraphMetricColumn,
            ListObject oTable,
            out Range oVisibleColumnData
        )
        {
            Debug.Assert(oGraphMetricColumn != null);
            Debug.Assert(oTable != null);
            AssertValid();

            oVisibleColumnData = null;

            // Add the specified column if it's not already present.

            String sColumnName  = oGraphMetricColumn.ColumnName;
            String sColumnStyle = oGraphMetricColumn.Style;

            Microsoft.Office.Interop.Excel.ListColumn oColumn;

            if (
                !ExcelTableUtil.TryGetTableColumn(oTable, sColumnName, out oColumn)
                &&
                !ExcelTableUtil.TryAddTableColumn(oTable, sColumnName,
                                                  oGraphMetricColumn.ColumnWidthChars, sColumnStyle, out oColumn)
                )
            {
                // Give up.

                return(false);
            }

            Range oColumnData;

            if (!ExcelTableUtil.TryGetTableColumnData(oTable, sColumnName,
                                                      out oColumnData))
            {
                return(false);
            }

            ExcelUtil.SetRangeStyle(oColumnData, sColumnStyle);

            String sNumberFormat = oGraphMetricColumn.NumberFormat;

            if (sNumberFormat != null)
            {
                oColumnData.NumberFormat = sNumberFormat;
            }

            // Wrapping text makes Range.set_Value() very slow, so turn it off.

            oColumn.Range.WrapText = false;

            // But wrap the text in the column's header.

            ExcelTableUtil.WrapTableColumnHeader(oColumn);

            // Get the visible range.

            if (!ExcelUtil.TryGetVisibleRange(oColumnData,
                                              out oVisibleColumnData))
            {
                return(false);
            }

            return(true);
        }
Beispiel #3
0
        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);
        }