如何在MVC中下载标题为粗体的excelsheet [英] How to download excelsheet with header as bold in MVC

查看:73
本文介绍了如何在MVC中下载标题为粗体的excelsheet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我的要求就是下载带有标题的excel表格,当我点击按钮



所以它的工作但标题不是粗体所以我想改变标题任何正文可以建议我在下面的代码中更改的位置是我的代码





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;
}

推荐答案

如评论中所述,CSV文件是纯文本,您无法添加装饰或格式。您可以尝试将数据写为html表,并通过html使标题变为粗体。当Excel将表解释为数据表时,它也可能采用粗体格式。谷歌如何将表格导出到Excel,这与你只发送一个html表格和不同的内容类型几乎相同。
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.


这篇关于如何在MVC中下载标题为粗体的excelsheet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆