tags:

views:

144

answers:

3

Hello Techies,

In my windows application in .net, i need to have Print Preview option for an excel file. The followings are my codings.

//Excel.Application excelApp = new Excel.Application();

        Excel.Workbook wb = excelApp.Workbooks.Open(@"C:\\Documents and Settings \\Admin \\Desktop \\DoCoMo\\ news5.xls",
                       Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
        ws.PrintPreview(Type.Missing);

The compilation is successful. but i didn't get the print preview window. Is there any requirement of additional parameters or any other. I don't know how to achieve it . Please Guide me.I will be so great full to Our "Techies" for this Timely help. From, M.Thillai

A: 

Make sure that the worksheet actually has some data in it, and make sure that it is visible.

Fiona Holder
Yes Fiona, The Worksheet has data in it and i made it visible throughws.Visible = XlSheetVisibility.xlSheetVisible;Still the print preview window is viewable.
M.Thillai
A: 

You seem to be using wb.Worksheets[1], are you sure that this shouldn't be wb.Worksheets[0]. I had a similar issue with excel but turned out the data was in a different worksheet to the one that I was looking at, so it didn't load anything.

Seph
A: 

Are you launching the print preview dialog? When coding for printing functionality, you need a PrintDocument object. This is what sends all relevant information to the printer. To get a Print Preview working, you need a PrintPreviewDialog. Also, this dialog needs to link to a PrintDocument object.

To do this, you need to assign a PrintPreviewDialog Control (call it printPreview) and a PrintDocument Control (rename it to printDocument) to the form that needs print functionality. Then create a Click event handler somewhere on the form and insert the following code:

PrintPreviewDialog printPreview = new PrintPreviewDialog();  
printPreview.Document = printDocument
printPreview.ShowDialog();

That will get you a print preview dialog on "click" .

RedEye