5

ユーザーが Web サイトでアドホック クエリを作成できるようにします。ユーザーに基準を選択してから [送信] をクリックしてもらい、結果を Excel に自動的にストリーミングさせたいと考えています。アプリケーションで DataTable を設定し、データテーブルを使用してタブ区切りの文字列を作成します。問題はそれを上達させることです。

データを Excel にストリーミングする最良の方法は何ですか? できれば、送信ボタンをクリックした後にユーザーが空のウィンドウを閉じないようにする必要があります。

4

7 に答える 7

10

ページのファイル タイプを Excel に変更し、テーブルの作成に必要な HTML のみをページにストリーミングします。コードはこちらから

//for demo purpose, lets create a small datatable & populate it with dummy data
System.Data.DataTable workTable = new System.Data.DataTable();

//The tablename specified here will be set as the worksheet name of the generated Excel file. 
workTable.TableName = "Customers";
workTable.Columns.Add("Id");
workTable.Columns.Add("Name");
System.Data.DataRow workRow;

for (int i = 0; i <= 9; i++)
{
workRow = workTable.NewRow();
workRow[0] = i;
workRow[1] = "CustName" + i.ToString();
workTable.Rows.Add(workRow);
}

//...and lets put DataTable2ExcelString to work
string strBody = DataTable2ExcelString(workTable);

Response.AppendHeader("Content-Type", "application/vnd.ms-excel");
Response.AppendHeader("Content-disposition", "attachment; filename=my.xls");
Response.Write(strBody);
于 2008-09-29T14:37:21.983 に答える
1

結果を含む単なる表であるページを作成し、ページのコンテンツ タイプを "application/vnd.ms-excel" に設定すると、出力は Excel になります。

Response.ContentType = "application/vnd.ms-excel";

強制的に保存したい場合は、次のようにします。

Response.AddHeader("Content-Disposition", "attachment; filename=somefilename.xls");
于 2008-09-29T14:47:27.293 に答える
1

私はすでにこれを行うユーティリティ機能を持っています。それをデータテーブルに入れたら、次を使用してレスポンスでエクスポートできます

        public static void DataTabletoXLS(DataTable DT, string fileName)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "utf-16";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        string tab = "";
        foreach (DataColumn dc in DT.Columns)
        {
            HttpContext.Current.Response.Write(tab + dc.ColumnName.Replace("\n", "").Replace("\t", ""));
            tab = "\t";
        }
        HttpContext.Current.Response.Write("\n");

        int i;
        foreach (DataRow dr in DT.Rows)
        {
            tab = "";
            for (i = 0; i < DT.Columns.Count; i++)
            {
                HttpContext.Current.Response.Write(tab + dr[i].ToString().Replace("\n", "").Replace("\t", ""));
                tab = "\t";
            }
            HttpContext.Current.Response.Write("\n");
        }
        HttpContext.Current.Response.End();
               }
于 2008-09-29T19:56:44.523 に答える
0

このコードを使用して問題を解決してください。このコードは Excel シートをテキスト形式に変換します。問題が解決することを願っています。

    grdSrcRequestExport.RenderControl(oHtmlTextWriter);
    string s = "";
    s=oStringWriter.ToString().Replace("<table cellspacing=\"0\" rules=\"all\" border=\"1\" style=\"border-collapse:collapse;\">", "");
    s="<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"><head><meta http-equiv=Content-Type content=\"text/html; charset=us-ascii\"><meta name=ProgId content=Excel.Sheet><meta name=Generator content=\"Microsoft Excel 11\"><table x:str border=0 cellpadding=0 cellspacing=0 width=560 style='border-collapse: collapse;table-layout:fixed;width:420pt'>"+s.ToString()+"</table></body></html>";
    //Byte[] bContent = System.Text.Encoding.GetEncoding("utf-8").GetBytes();
    Response.Write(s);
于 2009-06-29T09:51:59.053 に答える
0

データセットを取得したら、それをオブジェクト [,] に変換し、Excel ドキュメントに挿入できます。次に、ドキュメントをディスクに保存し、ユーザーにストリーミングできます。

        //write the column headers
        for (int cIndex = 1; cIndex < 1 + columns; cIndex++)
            sheet.Cells.set_Item(4, cIndex, data.Columns[cIndex - 1].Caption);
        if (rows > 0)
        {

            //select the range where the data will be pasted
            Range r = sheet.get_Range(sheet.Cells[5, 1], sheet.Cells[5 + (rows - 1), columns]);

            //Convert the datatable to an object array
            object[,] workingValues = new object[rows, columns];

            for (int rIndex = 0; rIndex < rows; rIndex++)
                for (int cIndex = 0; cIndex < columns; cIndex++)
                    workingValues[rIndex, cIndex] = data.Rows[rIndex][cIndex].ToString();

            r.Value2 = workingValues;
         }
于 2008-09-29T15:06:07.110 に答える
0

.xls ファイル拡張子のハンドラーと無料のコンポーネントを使用して、DataTable をネイティブ xls 形式に変換します。このサイトhttp://www.csvreader.com/のコンポーネントは、URL が示す以上のことを行います。最新バージョンの Excel では、HTML 形式の XLS ファイルについて問題が発生します。また、返されるデータのサイズにも注意してください。Web サーバーはこの拡張子に対して圧縮を使用する必要があり、コードは、返された行数が Excel が 1 つのワークシートに表示できる数よりも多いかどうかを確認する必要があります。複数のシートが必要な場合があります。http://www.mrexcel.com/archive2/23600/26869.htm

于 2008-09-29T15:24:48.843 に答える
0

ファイルハンドラー (.ashx)の使用をお勧めします。唯一の問題は、DataTable から Excel ファイルを作成することです。これを行う多くのサード パーティ製品があります (たとえば、Infragistics はこれを行うコンポーネントを提供します)。

私が強くお勧めしないことの 1 つは、サーバーで Excel 相互運用機能を使用することです...これは非常に重量があり、サポートされていません。

于 2008-09-29T14:39:20.963 に答える