sharepoint将gridview导出到excel

虽然sharepoint基于.net技术,但是对于一些.net基本实现方法在sharepoint仍然是有区别的,sharepoint 中将gridview导出到excel,就与.net有所区别。具体见讲解


方法一:使用GridViewExportUtil 辅助类

protected void lkbtnExport_Click(object sender, EventArgs e)
        {
            try
            {
                GridViewExportUtil.Export("PlannedTools.xls", gvPlanedTools, null);
            }
            catch(Exception ex) {
                panError.Visible = true;
                lbError.Text = ex.Message;
            }
        }

方法二:用另一种方法将gridview导出到excel,但是当gridveiw中有一些特殊列,如:身份证,这是导出的excel用此种方法会出现科学计数,而并非真实的身份证号码,如何转换?则只需用将Response.Write(@""); Response.Write("

"); Response.Write(sbTotal.ToString()); Response.Write("
"); Response.Flush(); } catch (Exception ex) { SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory(" Send Mail Web Part", TraceSeverity.Unexpected, EventSeverity.ErrorCritical), TraceSeverity.Unexpected, ex.Message, ex.StackTrace); string error = string.Format("javascript:alert('{0}');", "Application error occurred, please contact system administrator."); this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Error Message", error, true); } } } }

注意:此两种方法在.net中将gridview导出到execl是没有问题的,但是在sharepoint中只能导出一次,如果重复导出操作,将会无效,如何修复此种bug?只需在后台添加如下两行代码。

在Page_Load方法上面添加:

  private static string script= "_spOriginalFormAction =document.forms[0].action;\n_spSuppressFormOnSubmitWrapper = true;";

在Page_Load方法里面添加,注意不要将下面代码添加到IsPostBack里面:

Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(),script, true);


希望对从事sharepoint开发的朋友有所帮助




你可能感兴趣的:(sharepoint,sharepoint,excel,exception,string,.net,object)