0

我正在处理带有gridview 的asp.net 页面。我需要将gridview导出到excel,但需要在导出前修改colmnns​​数据。我正在使用以下代码导出:

 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 回答