0

私はグリッドビューでasp.netページに取り組んでいます。グリッドビューを Excel にエクスポートする必要がありますが、エクスポートする前に列データを変更する必要があります。以下のコードを使用してエクスポートしています。

 Response.Clear();

 Response.Buffer = true;         

   Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
   Response.ContentType = "application/vnd.xls";

   StringWriter sw = new StringWriter();
   HtmlTextWriter hw = new HtmlTextWriter(sw);
   gvLogs.RenderControl(hw);
   gvLogs.AllowPaging = false;
   gvLogs.DataBind(); // bind data 
   Response.Output.Write(sw.ToString());
   //need to call Flush and End methods 
   Response.Flush();
   Response.End();

それは機能しますが、グリッドを次のようにグリッドと同じ列データでExcelにエクスポートします。

asif@cc.com my company1aa viewed profile abc@gmail.com my name  Company views profile   7/24/2013 11:18
asif@cc.com my company1aa viewed profile cv3@cc.com cv 3    Company views profile   7/24/2013 11:18
asif@cc.com my company1aa viewed profile cv2@cc.com cv 2    Company views profile   7/24/2013 11:17
asif@cc.com my company1aa viewed profile cv4@cc.com cv 4    Company views profile   7/24/2013 11:17
asif@cc.com my company1aa viewed profile CV1@cc.com cv 1    Company views profile   7/24/2013 11:16

最初の列を4列に分割したい(最初の行の場合)

Employer email = asif@cc.com
company name = my company1aa
Registrant email = abc@gmail.com
Full name = my name

and then remaining 2 columns as it is

Action = Compnay view Profile
Date = 7/24/2013 11:18

これを行う方法を提案してください。バインドにオブジェクト データ ソースを使用していますが、データ テーブルを取得できます。

HRActionLog actionLog = new HRActionLog();
           DataTable dt = actionLog.GetList(Action,DateFrom,DateTo,CompanyId,RegistrantId,VacancyId,CurrentLanguage);

追加の列を含む新しいデータテーブルを作成し、 dt から入力する必要がありますか?

提案してください

4

2 に答える 2

0

@Koenが言ったように、新しいデータテーブルを作成することが最良の選択肢です。

最初の列をより小さな文字列/変数に分割する場合、複雑な正規表現が必要になりますが、最初の列の文字列がおそらく大きく異なるため、エラーが発生しやすくなります。

于 2013-07-25T07:32:28.217 に答える
0

どこかからデータを取得できないと仮定すると、string.Split を使用して文字列をさまざまな部分に分割することが唯一のオプションですが、それは好きではありませんが、他の解決策は見当たりません。

文字列は常に指定した形式になることを考慮して、次のスニペットを使用できます。

    var lst = new List<string>
                  {
                      "asif1@cc.com my company1aa1 viewed profile abc@gmail.com my name",
                      "asif2@cc.com my company1aa2 viewed profile cv3@cc.com cv 3",
                      "asif3@cc.com my company1aa3 viewed profile cv2@cc.com cv 2",
                      "asif4@cc.com my company1aa4 viewed profile cv4@cc.com cv 4",
                      "asif5@cc.com my company1aa5 viewed profile CV1@cc.com cv 1"
                  };
    foreach (var str in lst)
    {
        var i = str.IndexOf("viewed", StringComparison.OrdinalIgnoreCase);
        var part1 = str.Substring(0, i - 1); // -1 ommits the space before "viewed"
        var employerEmail = part1.Substring(0, part1.IndexOf(" ", StringComparison.OrdinalIgnoreCase));
        Console.WriteLine("EmployerEmail :" + employerEmail);
        var companyName = part1.Substring(part1.IndexOf(" ", StringComparison.OrdinalIgnoreCase) + 1); // +1 ommits the space
        Console.WriteLine("CompanyName :" + companyName);

        var part2 = str.Substring(i + 16); // +16 to start right after "viewed profile "
        var registrantEmail = part2.Substring(0, part2.IndexOf(" ", StringComparison.OrdinalIgnoreCase));
        Console.WriteLine("RegistrantEmail :" + registrantEmail);
        var fullName = part2.Substring(part2.IndexOf(" ", StringComparison.OrdinalIgnoreCase) + 1); // +1 ommits the space
        Console.WriteLine("FullName :" + fullName);
    }

このコードは、文字列がフォーマットされている場合にのみ機能します

<email> <companyname> viewed profile <email> <fullname>

これが変更された場合は、スニペットを調整する必要があります。しかし、上で述べたように、このデータがデータベースのどこかにある場合は、はるかに優れています。

于 2013-07-25T08:01:06.817 に答える