Example #1
0
        public void Test_WindowSizeEqualsOne_flushedRowHasMergedCell()
        {
            workbook = new SXSSFWorkbook(null, 1); // Window size 1 so only last row will be in memory
            sheet    = workbook.CreateSheet() as SXSSFSheet;
            sheet.TrackAllColumnsForAutoSizing();

            ICell a1 = CreateRowWithCellValues(sheet, 0, LONG_CELL_VALUE);

            assumeRequiredFontsAreInstalled(workbook, a1);
            sheet.AddMergedRegion(CellRangeAddress.ValueOf("A1:B1"));

            CreateRowWithCellValues(sheet, 1, SHORT_CELL_VALUE, SHORT_CELL_VALUE);

            /**
             *    A      B
             * 1 LONGMERGED
             * 2 SHORT SHORT
             */

            sheet.AutoSizeColumn(0, useMergedCells);
            sheet.AutoSizeColumn(1, useMergedCells);

            if (useMergedCells)
            {
                // Excel and LibreOffice behavior: ignore merged cells for auto-sizing.
                // POI behavior: evenly distribute the column width among the merged columns.
                //               each column must be auto-sized in order for the column widths
                //               to add up to the best fit width.
                int colspan          = 2;
                int expectedWidth    = (10000 + 1000) / colspan; //average of 1_000 and 10_000
                int minExpectedWidth = expectedWidth / 2;
                int maxExpectedWidth = expectedWidth * 3 / 2;
                assertColumnWidthStrictlyWithinRange(sheet.GetColumnWidth(0), minExpectedWidth, maxExpectedWidth); //short
            }
            else
            {
                assertColumnWidthStrictlyWithinRange(sheet.GetColumnWidth(0), COLUMN_WIDTH_THRESHOLD_BETWEEN_SHORT_AND_LONG, MAX_COLUMN_WIDTH); //long
            }
            assertColumnWidthStrictlyWithinRange(sheet.GetColumnWidth(1), 0, COLUMN_WIDTH_THRESHOLD_BETWEEN_SHORT_AND_LONG);                    //short
        }
Example #2
0
        public void updateColumnWidths_and_getBestFitColumnWidth()
        {
            tracker.TrackAllColumns();
            IRow row1 = sheet.CreateRow(0);
            IRow row2 = sheet.CreateRow(1);

            // A1, B1, D1
            foreach (int column in columns)
            {
                row1.CreateCell(column).SetCellValue(LONG_MESSAGE);
                row2.CreateCell(column + 1).SetCellValue(SHORT_MESSAGE);
            }
            tracker.UpdateColumnWidths(row1);
            tracker.UpdateColumnWidths(row2);
            sheet.AddMergedRegion(CellRangeAddress.ValueOf("D1:E1"));

            assumeRequiredFontsAreInstalled(workbook, row1.GetCell(columns.GetEnumerator().Current));

            // Excel 2013 and LibreOffice 4.2.8.2 both treat columns with merged regions as blank

            /**    A     B    C      D   E
             * 1 LONG  LONG        LONGMERGE
             * 2       SHORT SHORT     SHORT
             */

            // measured in Excel 2013. Sizes may vary.
            int longMsgWidth  = (int)(57.43 * 256);
            int shortMsgWidth = (int)(4.86 * 256);

            CheckColumnWidth(longMsgWidth, 0, true);
            CheckColumnWidth(longMsgWidth, 0, false);
            CheckColumnWidth(longMsgWidth, 1, true);
            CheckColumnWidth(longMsgWidth, 1, false);
            CheckColumnWidth(shortMsgWidth, 2, true);
            CheckColumnWidth(shortMsgWidth, 2, false);
            CheckColumnWidth(-1, 3, true);
            CheckColumnWidth(longMsgWidth, 3, false);
            CheckColumnWidth(shortMsgWidth, 4, true); //but is it really? shouldn't autosizing column E use "" from E1 and SHORT from E2?
            CheckColumnWidth(shortMsgWidth, 4, false);
        }