我想要执行以下操作的主要原因是在Excel中显示不是指数格式的大数字。如果有一种比修改列宽标题更好的方法而不修改当前结构/方法,只是添加或稍微修改以下代码,那么请告诉我。感谢。
以下是我用于使用memorystream将视图中的数据写入excel电子表格的代码。目前,当数据从内存流写入excel文件时,excel列宽度都是标准的默认宽度。有没有办法通过保留当前的代码结构/方法并只添加代码或稍微修改代码来修改Excel电子表格中的这些列标题宽度?
public ActionResult ExportStudentRes()
{
//PropertyInfo[] Props = typeof(vw_CheckList).GetProperties(BindingFlags.Public | BindingFlags.Instance);
Placeholder db = new PlaceholderEntities();
List<vw_StudentResponse> studentres = db.vw_StudentResponse.ToList();
PropertyInfo[] Props = typeof(vw_StudentResponse).GetProperties(BindingFlags.Public | BindingFlags.Instance);
MemoryStream stream = new MemoryStream();
using (var writer = new StreamWriter(stream))
{
StringBuilder colHeading = new StringBuilder();
foreach (PropertyInfo prop in Props)
{
colHeading.Append(prop.Name);
colHeading.Append(",");
}
string cHeading = colHeading.ToString();
cHeading = cHeading.TrimEnd(',');
writer.Write(cHeading);
writer.Write("\n");
foreach (vw_StudentResponse item in studentres)
{
StringBuilder rowValues = new StringBuilder();
for (int i = 0; i < Props.Length; i++)
{
rowValues.Append(Props[i].GetValue(item));
rowValues.Append(",");
}
string rvalues = rowValues.ToString();
rvalues = rvalues.TrimEnd(',');
writer.Write(rvalues);
writer.Write("\n");
}
}
string downloadFileName = "export_StudentResponse.csv";
string contentType = "text/csv";
Response.Clear();
Response.ContentType = contentType;
Response.AddHeader("content-disposition", "attachment;filename=" + downloadFileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(stream.ToArray());
Response.End();
return View();
}