C# ASP.NET Excel导出
我正在寻找一种导出Excel电子表格的方法,使每行单元格中有多个值 我目前正在使用HtmlTextWriter类将Html写入Excel内容类型格式,但它会为相应行中的每个值创建一个新单元格 例如,它当前会写出 行1:行标题值1 第2行:值2 第3行:值3 我想把所有的东西都写在一行。 行1:行标题值1 价值2 价值3 第2行: 这可能吗?有人有任何指示吗 我目前的代码如下:C# ASP.NET Excel导出,c#,asp.net,excel,C#,Asp.net,Excel,我正在寻找一种导出Excel电子表格的方法,使每行单元格中有多个值 我目前正在使用HtmlTextWriter类将Html写入Excel内容类型格式,但它会为相应行中的每个值创建一个新单元格 例如,它当前会写出 行1:行标题值1 第2行:值2 第3行:值3 我想把所有的东西都写在一行。 行1:行标题值1 价值2 价值3 第2行: 这可能吗?有人有任何指示吗 我目前的代码如下: Response.Buffer = true; Response.Content
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
Response.AddHeader("content-disposition", "filename=Compare" + fileName + ".xls");
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(tblComparison);
tblComparison.RenderControl(oHtmlTextWriter);
// Embed style rules here so that when opening the file in Excel separately, the styling will stay in place
// and not be dependent on an external css file.
Response.Write(@"
<head>
<title>Compare</title>
<style>
#tblComparison { empty-cells:show;border-collapse:collapse;border:none; }
#tblComparison tr td { vertical-align:top;text-align:center;padding:0px 5px 2px 8px;border-right:1px solid #094A8C;border-bottom:1px dotted #6699FF;font-size:.7em }
#tblComparison tr td.header { text-align:right;font-weight:bold }
#tblComparison tr td.header span.note { text-align:right;font-weight:normal;color:#094A8C; }
#tblComparison tr td div.itrs { display:none }
#tblComparison tr td textarea { width:20em }
#tblComparison tr td textarea.objective { height:5em;font-size:.9em }
#tblComparison tr td select { width:200px;font-size:1em }
#tblComparison tr td textarea.comments { height:10em;font-size:.9em }
#tblComparison tr td textarea.multiline { height:10em;font-size:.9em }
#tblComparison tr td.requested textarea.comments,
#tblComparison tr td.requested textarea.objective { background-color:#F2F7FB;border:1px solid gray }
#tblComparison tr td.requested input { font-size:1em;background-color:#F2F7FB;border:1px solid gray }
#tblComparison tr .existRequested { background-color:#52697B; color:White;}
div { font-size: 1em }
</style>
</head>
");
Response.Write(oStringWriter.ToString());
Response.End();
提前感谢。希望此代码能帮助您
Response.ContentType = "text/csv";
Response.Cache.SetNoStore();
Response.Buffer = true;
Response.BufferOutput = true;
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.Write("Company, City, Country");
//IF you want each item in each cell of a row
byte[] buffer = System.Text.Encoding.Unicode.GetBytes(Separater(Name) + Separater(City) +Separater(Country) + "\r\n")
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.OutputStream.Flush();
private string Separater(string text)
{
if (text == null)
{
return "\"\",";
}
return "\"" + text.Replace("\"", "'") + "\"" + ",";
}
如果您是从集合或数据集获取表的数据,则可以使用电子表格类动态创建excel,并按照您想要的方式从代码隐藏中格式化它
像这样的东西 当您说导出时,我假设您的意思是希望能够创建一个speadsheet供用户下载。如果这就是您的意思,那么您可能想看看CarlogAg Excel Xml Writer-
如果你想在页面上创建一个包含Excel电子表格中的数据的网格,那么你可能需要考虑使用Excel数据读取器读取数据。这将为您提供一个DataTable,然后您可以将ASP.Net绑定到它,如下所示
谢谢-我明天会调查这件事,让你知道它是否奏效!