DevExpressr“共享網盤”上使用時導出EXCEL功能出錯的解決辦法

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
   at DevExpress.XtraPrinting.ComponentPrinter.CreateLink()
   at DevExpress.XtraPrinting.ComponentExporter.get_LinkBase()
   at DevExpress.XtraGrid.Views.Base.BaseView.ClearDocumentIfNeeded()
   at DevExpress.XtraGrid.Views.Base.BaseView.ExecutePrintExport(Action0 method)
   at DevExpress.XtraGrid.Views.Base.BaseView.ExportToXls(String filePath)
   at CreateTech.Retail.WinForm.FrePortForm2.ctBtnCommon5_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at CreateTech.Retail.Controls.CtButtons.CtBtnCommon.simpleButton1_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at DevExpress.XtraEditors.BaseButton.OnClick(EventArgs e)
   at DevExpress.XtraEditors.BaseButton.OnMouseUp(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at DevExpress.Utils.Controls.ControlBase.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** 解決辦法 **************
/// <summary>
/// 方式一:導出報表文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ctBtnCommon5_Click(object sender, EventArgs e)
{
    string filePath = string.Empty;
    SaveFileDialog openFileDialog1 = new SaveFileDialog();
    openFileDialog1.Filter = "Office Excel 1997 - Office Excel 2003(*.xls)|*.xls|Office Excel 2007 - Office Excel 2010(*.xlsx)|*.xlsx|PDF(*.pdf)|*.pdf";
    openFileDialog1.AddExtension = true;
    openFileDialog1.ShowDialog();
    filePath = openFileDialog1.FileName;
    if (filePath != string.Empty && filePath != "")
    {
        //DevExpress.Data.Utils.Helpers.LoadWithPartialName(AssemblyInfo.SRAssemblyPrinting + ", Version=" + AssemblyInfo.Version);
        //quan2012/6/14:
        //System.NullReferenceException: Object reference not set to an instance of an object.
        //at DevExpress.XtraPrinting.ComponentPrinter.CreateLink()
        //at DevExpress.XtraPrinting.ComponentExporter.get_LinkBase()
        //at DevExpress.XtraGrid.Views.Base.BaseView.ClearDocumentIfNeeded()
        //at DevExpress.XtraGrid.Views.Base.BaseView.ExecutePrintExport(Action0 method)
        //at DevExpress.XtraGrid.Views.Base.BaseView.ExportToXls(String filePath)
        //var x = new DevExpress.XtraPrintingLinks.CompositeLink();
        if (PrintHelperBase.IsPrintingAvailable)
        {
            if (filePath.EndsWith(".xls") == true)
            {
                gridView5.ExportToXls(filePath);
            }
            else if (filePath.EndsWith(".xlsx") == true)
            {
                gridView5.ExportToXlsx(filePath); ;
            }
            else if (filePath.EndsWith(".pdf") == true)
            {
                gridView5.Appearance.HeaderPanel.Font = new Font("Arial Unicode MS", 10, FontStyle.Regular);
                gridView5.Appearance.Row.Font = new Font("Arial Unicode MS", 10, FontStyle.Regular);
                gridView5.Appearance.Row.Options.UseFont = true;
                gridView5.Appearance.HeaderPanel.Options.UseFont = true;
                gridView5.ExportToPdf(filePath);
            }
            else
            {
                gridView5.ExportToPdf(filePath + ".xls");
            }
            MessageHandle.MessageShow(this, "export_success", resources);
            GC.Collect();
        }
    }
}

/// <summary>
/// 列印方式二
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ctBtnCommon6_Click(object sender, EventArgs e)
{
    //DevExpress.Data.Utils.Helpers.LoadWithPartialName(AssemblyInfo.SRAssemblyPrinting + ", Version=" + AssemblyInfo.Version);
    //quan2012/6/14:
    //System.NullReferenceException: Object reference not set to an instance of an object.
    //at DevExpress.XtraPrinting.ComponentPrinter.CreateLink()
    //at DevExpress.XtraPrinting.ComponentExporter.get_LinkBase()
    //at DevExpress.XtraGrid.Views.Base.BaseView.ClearDocumentIfNeeded()
    //at DevExpress.XtraGrid.Views.Base.BaseView.ExecutePrintExport(Action0 method)
    //at DevExpress.XtraGrid.Views.Base.BaseView.ExportToXls(String filePath)
    //var x = new DevExpress.XtraPrintingLinks.CompositeLink();
    if (PrintHelperBase.IsPrintingAvailable)
    {
        pivotGridControl1.ShowPrintPreview();
        GC.Collect();
    }
}

來源:
http://www.devexpress.com/Support/Center/p/B204522.aspx

Hi.

This is quite strange.
Please allow me to quickly recap.

***

1.
CODE:
gridControl1.ShowPrintPreview();

a) from local drive => works as expected
b) from network drive => throws with "Object reference not set to..." at DevExpress.XtraPrinting.ComponentPrinter.CreateLink()

***

2.
CODE:
if (gridControl1.IsPrintingAvailable) {
  gridControl1.ShowPrintPreview();
} else {
  MessageBox.Show("Y U NO PRINT!?");
}

a) from local drive => works as expected
b) from network drive => shows message box

***
BUT!
***

3.
CODE:
if (PrintHelperBase.IsPrintingAvailable) {
  gridControl1.ShowPrintPreview();
} else {
  MessageBox.Show("Y U NO PRINT!?");
}

a) from local drive => works as expected
b) from network drive => WORKS AS EXPECTED

***

4.
SOURCE:
DevExpress.Data.Utils.Helpers.LoadWithPartialName(AssemblyInfo.SRAssemblyPrinting + ", Version=" + AssemblyInfo.Version);
gridControl1.ShowPrintPreview();

Also works as expected.

***

CONCLUSION:
Apparently gridControl1.IsPrintingAvailable does something different than PrintHelperBase.IsPrintingAvailable and does not successfully load needed assembly?
How to workaround?

WBR,
Dusan


你可能感兴趣的:(DevExpress)