在使用asp.net导出到excel之前自定义数据

本文关键字:excel 自定义 数据 asp net | 更新日期: 2023-09-27 18:24:44

我正在开发一个带有网格视图的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填充它吗?

请建议

在使用asp.net导出到excel之前自定义数据

假设您无法从某个地方获取数据,我看到的唯一选项是使用字符串将字符串拆分为不同的部分。分裂我不喜欢它,但我看不到其他解决方案。

考虑到字符串将始终采用您给定的格式,您可以使用以下片段:

    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>

如果这种情况发生变化,您将不得不调整代码段。但正如上面提到的:如果你能在数据库中的某个地方找到这些数据,那就好多了。

正如@Koen所说,创建一个新的数据表是最好的选择。

至于将第一列分解为较小的字符串/变量,您需要一个复杂的regexp,而且由于第一列中的字符串很可能会有很大的差异,因此仍然容易出错。