我正在使用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填充它?
请建议
答案 0 :(得分:0)
正如@Koen所说,创建一个新的数据表是最好的选择。
至于将第一列拆分为较小的字符串/变量,您需要复杂的正则表达式,并且由于第一列中的字符串很可能会发生很大变化,因此仍然容易出错。
答案 1 :(得分: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>
如果此更改,您将不得不调整代码段。 但正如前面提到的:如果你能在数据库的某个地方找到这些数据,那就更好了。