How to set Shrink To Fit property for cell so Excel will automatically change font size to fit text in cell - ByteScout

How to set Shrink To Fit property for cell so Excel will automatically change font size to fit text in cell

  • Home
  • /
  • Articles
  • /
  • How to set Shrink To Fit property for cell so Excel will automatically change font size to fit text in cell

How to enable Shrink To Fit property for cells so Excel will automatically fit text for the cell according to changed cell width in spreadsheet XLS document made with Bytescout Spreadsheet SDK

This source code sample demonstrates how to use Shrink To Fit property for cell in spreadsheet generated with Bytescout Spreadsheet SDK so generated XLS document will automatically fit text in the cell when opened in Excel 2003 or 2007

Download example source code: bytescoutxls_using_shrink_to_fit_for_cells.zip (10 KB)

Output XLS document screenshot (in Office 2003):

source Excel document with data table

Output XLS document screenshot (in Office 2007):
source Excel document with data table

Modified document screenshot (see Homer’s Full Name changed)

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Bytescout.Spreadsheet;

namespace HelloWorld
{
  class Program
  {
    static void Main(string[] args)
    {
      // Create new Spreadsheet
      Spreadsheet document = new Spreadsheet();

      // Add new worksheet
      Worksheet worksheet = document.Workbook.Worksheets.Add("HelloWorld");

      // Set cell value
      worksheet.Cell(0, 0).Value = "Very, very, very, very long text";

      // Shrink text
      worksheet.Cell(0, 0).ShrinkToFit = true;

      // Save document
      document.SaveAs("CellShrinkToFit.xls");

      // Close document
      document.Close();

      // open generated XLS document in default program
      Process.Start("CellShrinkToFit.xls");
    }
  }
}

Download example source code: bytescoutxls_using_shrink_to_fit_for_cells.zip (10 KB)

Tutorials:

prev
next