views:

582

answers:

1

hi, i am working with c# and excel-2007: can anyone show me how to put the excel sheet in a c# print preview dialog, plus to fit the excel sheet in a printable area of one paper. thanks in advance!

is it possible to add extra buttons on the printpreview dialog like for example i want to put text editor there so that user can press it and put it on a printed paper area and type his/her text... thanks a lot!

A: 

SpreadsheetGear for .NET has a WorkbookView.PrintPreview() method which displays an Excel workbook in the .NET Print Preview window and supports the IPageSetup.FitToPagesTall / FitToPagesWide properties to force printing on one page.

You can download the free trial and run this code in a C# Console Application (first add a reference to SpreadsheetGear.dll and to System.Windows.Forms.dll):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpreadsheetGear;

namespace SpreadsheetGearPrintPreview
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a workbook to preview.
            IWorkbook workbook = Factory.GetWorkbook();
            IWorksheet worksheet = workbook.Worksheets[0];
            worksheet.Name = "MySheet";
            worksheet.Cells["A1:J100"].Formula = "=ROUND(RAND()*ROW()*COLUMN(),0)";

            // Set the print area.
            IPageSetup pageSetup = worksheet.PageSetup;
            pageSetup.PrintArea = "A1:J100";

            // This will force the workbook to be printed on one page.
            pageSetup.FitToPagesTall = 1;
            pageSetup.FitToPagesWide = 1;

            // Create a WorkbookView and display the workbook in Print Preview.
            var workbookView = new SpreadsheetGear.Windows.Forms.WorkbookView(workbook);
            workbookView.PrintPreview();
        }
    }
}

You can also use the SpreadsheetGear WorkbookView control to allow the user to edit the workbook. SpreadsheetGear does not support modifying the Print Preview window. It does have the SpreadsheetGear.Drawing.Image APIs which you could potentially use in your own Print Preview window, but that would likely be quite a bit of work and I'm not sure how customizable the .NET Print Preview framework is.

Disclaimer: I own SpreadsheetGear LLC

Joe Erickson