Excel PrintPreview

时间:2010-01-12 06:11:45

标签: c# .net excel

以下是我打印excel文档预览的代码。

编译成功。

但预览窗口不可见。任何人都可以指出我的错误是什么。是否需要添加更多编码或dll。

(注意:文档包含数据)

        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];
        bool userDidntCancel = excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrintPreview].Show(                
                                    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, 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, Type.Missing, Type.Missing);




        GC.Collect();
        GC.WaitForPendingFinalizers();
        wb.Close(false, Type.Missing, Type.Missing);
        excelApp.Quit();

2 个答案:

答案 0 :(得分:3)

您可能必须手动显示Excel窗口:

excelApp.Visible = true;

但是,我听说它们是与特定Excel版本相关的一些问题(它可能适用于某些版本,而不适用于其他版本)。

答案 1 :(得分:0)

您需要将 excelApp Visible 属性设置为true。

Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;