1

現在、ページに 2 つのグリッドビューがあります。1 つのグリッドビューを Excel ファイルにエクスポートできます。しかし、2 番目のグリッドビューについては、同じファイル内の別のワークシートで開きたいと思います。誰かが私が始めるのを手伝ってくれて、その方法を教えてくれませんか?

私のコードは次のようになります。

protected void btnExport_Click(object sender, EventArgs e)
{
    GridView gridView = null;
    System.IO.StringWriter stringWrite = null;
    System.Web.UI.HtmlTextWriter htmlWrite = null;
    try
    {
        gridView = new GridView();
        List<string> columns = new List<string>();
        columns.Add("CREATE_AR_YN");
        columns.Add("GL_DEBIT_ACCT");
        columns.Add("ALLOC_METHOD");
        columns.Add("CUST_NAME");
        columns.Add("DEFAULT_PYMT_TERMS");
        columns.Add("AR_BILL_GROUP");

        BoundField bf = null;
        for (int i = 0; i < columns.Count; i++)
        {
            bf = new BoundField();
            bf.DataField = columns[i];
            bf.HeaderText = columns[i];
            bf.HeaderStyle.BackColor = System.Drawing.Color.FromName("#81DAF5");
            gridView.Columns.Add(bf);
            gridView.AutoGenerateColumns = false;
        }

        List<FMAProfile> custList = GetSelectedCustomerProfile();
        gridView.DataSource = custList;
        gridView.DataBind();
        Response.Clear();
        Response.ClearHeaders();
        Response.Cache.SetCacheability(HttpCacheability.Private);
        Response.AddHeader("content-disposition", "attachment;filename=" + "CustomerProfile" + ".xls");
        Response.Charset = "";
        //Response.ContentType = "application/vnd.xls";
        Response.ContentType = "application/vnd.ms-excel";
        //Response.Buffer = false;
        stringWrite = new System.IO.StringWriter();
        htmlWrite = new HtmlTextWriter(stringWrite);
        gridView.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
    catch (Exception ex)
    {
        if (ex.Message.Trim().IndexOf("Thread was being aborted") == -1)
        {
            Log(ex.ToString());
        }
    }
    finally
    {
        if (gridView != null)
        {
           gridView.Dispose();
           gridView = null;
        }
        if (stringWrite != null)
        {
            stringWrite.Dispose();
        }
        stringWrite = null;
        if (htmlWrite != null)
        {
            htmlWrite.Dispose();
        }
        htmlWrite = null;
    }
}
4

1 に答える 1

1

あなたは実際のXLSをエクスポートしていませんが、基本的にExcelがインポートできる/インポートできるHTMLです...

サンプル コードを含む Excel ファイルを作成するための HTML アプローチに関する詳細については、http://www.c-sharpcorner.com/UploadFile/kaushikborah28/79Nick08302007171404PM/79Nick.aspxを参照してください。

http://msdn.microsoft.com/en-us/library/Aa155477%28office.10%29.aspxの公式ドキュメントもチェックしてください。

HTMLで複数のワークシートを作成できるかどうかはよくわかりません...

実際の Excel ファイルを作成するためのオプション:

MS は OpenXML SDK V 2.0 (無料) を提供しています - http://msdn.microsoft.com/en-us/library/bb448854%28office.14%29.aspxを参照してください

これは、MS Office ファイル (Excel を含む) を読み書きできます。

別のオプションについては、http://www.codeproject.com/KB/office/OpenXML.aspxを参照してください。

レンダリングや数式などが必要な場合は、 ClosedXMLEPPlusAspose.CellsSpreadsheetGearLibXLFlexcelなどのさまざまな無料および商用のライブラリがあります。

注意: Office Interop は ASP.NET ではサポートされていません (プロジェクトが組み込まれているようです)。

于 2012-12-11T20:32:54.740 に答える