1

GridView を Excel にエクスポートしたいと思いますが、これは簡単です。しかし、グリッドの上では、Excel で、識別のために他の情報が必要です。下のグリッドビューに入れながら、グリッドビュー以外のものをエクスポートできますか?

編集: 何らかの理由で GridView1 が表示されているときにエクスポートしようとすると、グリッドビューだけでなくページ全体がエクスポートされます。理由がわからない!

Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
    'Create a StringWriter and HtmlTextWriter
    Dim sw As System.IO.StringWriter = New System.IO.StringWriter()
    Dim htw As New System.Web.UI.HtmlTextWriter(sw)

    'Clear the Response object's content and specify the header for the HTML response and type of application file to create
    Response.ClearContent()
    Response.AddHeader("content-disposition", "attachment; filename=SaveFile.xls")
    Response.ContentType = "application/vnd.ms-excel"
    Response.Charset = ""
    EnableViewState = False

    htw.WriteLine("Test, test, test")

    Try
        'Check for the number of GridView rows
        If GridView1.Rows.Count < 65535 Then
            'Turn sorting and paging off and rebind the GridView control
            GridView1.AllowSorting = False
            GridView1.AllowPaging = False
            GridView1.PageSize = GridView1.Rows.Count
            GridView1.AutoGenerateSelectButton() = False
            GridView1.DataBind()


            'Render the GridView1 as HTML - this will cause an error that will fire the VerifyRenderingInServerForm event -- this event is trapped by the Overriding sub procedure given at the end of the program listing
            GridView1.RenderControl(htw)

            'Write the response
            Response.Write(sw.ToString())
            Response.End()

            'Turn sorting and paging on and rebind the GridView control
            GridView1.AllowSorting = True
            GridView1.AllowPaging = True
            '.GridView1.PageSize = 10
            GridView1.AutoGenerateSelectButton() = True
            GridView1.DataBind()
        End If
    Catch ex As Exception

    End Try

End Sub
4

5 に答える 5

3

はい、できます。

次のようにします。

HttpContext.Current.Response.Write("some string value")

グリッドビューを渡す前に。

于 2009-03-30T19:04:50.220 に答える
1

同じことをするための私のコードは次のとおりです

protected void ExportExcel_OnClick(オブジェクト送信者, EventArgs e) {
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=brugere.xls");
    Response.Charset = "windows-1252";
    Response.ContentType = "application/vnd.xls";
    (StringWriter stringWrite = new StringWriter()) を使用して
    using (HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite)) {
        GridView1.AllowPaging = false;
        GridView1.DataBind();
        GridView1.RenderControl(htmlWrite);
        文字列 html = stringWrite.ToString();
        文字列の結果 = Replacer.Replace(html, "");
        Response.Write(結果);
    }
    Response.End();
}

フォーマット、画像、divなどを避けるために、正規表現を使用して結果のhtmlをトリミングしていることに注意してください。

static readonly Regex Replacer = new Regex("(<input[^<>]*>)|"+
  "(class=\"[^\"]*\")|(style=\"[^\"]*\")|"+
  "(<a[^]*>)|(</a>)|(<div>)|(</div>)|" +
  "(cellspacing=\"[^\"]*\")|(cellpadding=\"[^\"]*\")|" +
  "(id=\"[^\"]*\")|(border=\"[^\"]*\")", RegexOptions.IgnoreCase);

グリッドがページの外側でレンダリングされるように、次をオーバーライドすることを忘れないでください

public override void VerifyRenderingInServerForm(コントロール コントロール) {
    戻る;
}
于 2009-06-29T10:38:09.600 に答える
1

このファイルを Excel で開くと、警告メッセージが生成されます。NPOI のようなオープンソースのエクスポート ライブラリの 1 つを使用します。 http://npoi.codeplex.com/

それでも HTML 出力を使用したい場合は、次のリンクから Office HTML 形式に関する Microsoft のドキュメントをダウンロードすることを検討して ください。

このアーカイブ (EXE でパックされた) の CHM ファイルのみが必要です。

幸運を。

于 2012-02-24T09:56:58.217 に答える
0

GridViewがDataTable、DataSet、またはList <>のデータを使用して入力されている場合、次のライブラリを使用すると、1つの「CreateExcelDocument」関数を呼び出すだけでExcel 2007(.xlsx)ファイルにエクスポートできます。

// Step 1: Create a DataSet, and put some sample data in it
DataSet ds = CreateSampleData();

// Step 2: Create the Excel .xlsx file
try
{
    string excelFilename = "C:\\Sample.xlsx";
    CreateExcelFile.CreateExcelDocument(ds, excelFilename);
}
catch (Exception ex)
{ 
    MessageBox.Show("Couldn't create Excel file.\r\nException: " + ex.Message);
    return;
}

完全なソースコードが提供されているので、1つ以上のワークシートの上部に、データの行を追加するように調整できます。

このライブラリはOpenXMLライブラリを使用しているため、完全に無料です。 http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

于 2011-11-23T12:12:28.103 に答える