Click here to Skip to main content
15,909,039 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
hi all my requirement is like download an excel sheet with header when i click on button

so its working but header is not in bold so i want to change header any body can suggest me where to change in my code below is my code


this  is my controller
public void ExportClientsListToCSV()
{

StringWriter sw = new StringWriter();

sw.WriteLine("\"User Name\",\"In Time\",\"Out Time\",\"Client Info\",\"User Role\",\"Activity\"");

Response.ClearContent(); 
Response.AddHeader("content-disposition", "attachment;filename=User_Track.csv");
Response.ContentType = "text/csv";
var abc = TempData["test"];
IEnumerable<PAN_LOGINTRK> test = abc as IEnumerable<PAN_LOGINTRK>;
//object test = (abc);

foreach (var line in test)
{
var role = "";
if (line.userrole == 1)
{
role = "CSAdmin";
}
else if (line.userrole == 2)
{
role = "Buyer";
}
else if (line.userrole == 3)
{
role = "Supervisor";
}
else if (line.userrole == 4)
{
role = "OrgAdmin";
}
sw.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\"",
line.username,
line.Intime,
line.Outtime,
"Client IP: "+line.ClientIP+" Client XIP: "+line.ClientXIP+" Client Agent: "+line.ClinetAgent,
role,
line.Activity));
}

Response.Write(sw.ToString());

Response.End();

}

in view i have written like

function Export() {

var ExportAction = '@Url.RouteUrl("Default", new RouteValueDictionary(new { action = "ExportClientsListToCSV", controller = "UserTrack" }), "http", ConfigurationManager.AppSettings["ContextPath"])'
location.href = ExportAction;
}


What I have tried:

this  is my controller
public void ExportClientsListToCSV()
{

StringWriter sw = new StringWriter();

sw.WriteLine("\"User Name\",\"In Time\",\"Out Time\",\"Client Info\",\"User Role\",\"Activity\"");

Response.ClearContent(); 
Response.AddHeader("content-disposition", "attachment;filename=User_Track.csv");
Response.ContentType = "text/csv";
var abc = TempData["test"];
IEnumerable<PAN_LOGINTRK> test = abc as IEnumerable<PAN_LOGINTRK>;
//object test = (abc);

foreach (var line in test)
{
var role = "";
if (line.userrole == 1)
{
role = "CSAdmin";
}
else if (line.userrole == 2)
{
role = "Buyer";
}
else if (line.userrole == 3)
{
role = "Supervisor";
}
else if (line.userrole == 4)
{
role = "OrgAdmin";
}
sw.WriteLine(string.Format("\"{0}\",\"{1}\",\"{2}\",\"{3}\",\"{4}\",\"{5}\"",
line.username,
line.Intime,
line.Outtime,
"Client IP: "+line.ClientIP+" Client XIP: "+line.ClientXIP+" Client Agent: "+line.ClinetAgent,
role,
line.Activity));
}

Response.Write(sw.ToString());

Response.End();

}

in view i have written like

function Export() {

var ExportAction = '@Url.RouteUrl("Default", new RouteValueDictionary(new { action = "ExportClientsListToCSV", controller = "UserTrack" }), "http", ConfigurationManager.AppSettings["ContextPath"])'
location.href = ExportAction;
}
Posted
Updated 28-Apr-17 9:04am
Comments
CHill60 28-Apr-17 11:27am    
You are outputting to a CSV file. You cannot have cell attributes in a CSV file.
[no name] 28-Apr-17 11:29am    
CSV files are plain text files. You can't have text formatting in a text file.

1 solution

As said in the comments CSV files are plain text, you can't add decorations or formatting. You could try writing the data as an html table instead and make the headers bold via html. When Excel interprets the table as a datasheet it may well take on the bold formatting also. Google how to export a table to Excel, it's pretty much the same thing you already have only sending an html table and different content types.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900