我创建了用于文章审阅的网络应用程序。
我有一个名为 Article 的表,每个表Article
有一些ArticleReview
:
Article
ArticleId
ArticleTitle
NumberOfComment
NumberOfView
...
ICollection<ArticleReview> Reviews
管理员可以添加许多用户(我称之为 JueryUser)并将文章发送给他们进行审阅,我使用名为 ArticlePoint 的表来添加/删除可以审阅每篇文章的用户:
文章点
public int ArticlePointId { get; set; }
public DateTime CreateOn { get; set; }
public string Id { get; set; } // Id of User that review Article
public ApplicationUser JuryUser { get; set; }
public string UserId { get; set; } // Id of User that write Article
public int ArticleId { get; set; }
public string JuryReview { get; set; }
当陪审团用户评论文章时,他/她给文章一些分数,该分数基于管理员提供的问题,该评论存储在ArticleReview
table.
文章评论
ArticleReviewId
ReviewPoint
ArticleId
ReviewerId
正如我在我之前的问题 https://stackoverflow.com/questions/53692304/convert-a-table-with-different-relational-values-to-excel-columns/53804622我想从每个 jquery 提供的文章的要点摘要中导出 excel。
我使用下面的代码生成我的 Excel 文件:
public static class ExcelExportHelper
{
public static string ExcelContentType => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
public static DataTable ListToDataTable<T>(List<T> data)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable dataTable = new DataTable();
for (int i = 0; i < properties.Count; i++)
{
PropertyDescriptor property = properties[i];
dataTable.Columns.Add(property.Name, Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType);
}
object[] values = new object[properties.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = properties[i].GetValue(item);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
public static byte[] ExportExcel(DataTable dataTable, string heading = "", bool showSrNo = false, params string[] columnsToTake)
{
byte[] result = null;
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet workSheet = package.Workbook.Worksheets.Add(String.Format("{0} Data", heading));
workSheet.View.RightToLeft = true;
int startRowFrom = String.IsNullOrEmpty(heading) ? 1 : 3;
if (showSrNo)
{
DataColumn dataColumn = dataTable.Columns.Add("#", typeof(int));
dataColumn.SetOrdinal(0);
int index = 1;
foreach (DataRow item in dataTable.Rows)
{
item[0] = index;
index++;
}
}
// add the content into the Excel file
workSheet.Cells["A" + startRowFrom].LoadFromDataTable(dataTable, true);
// format header - bold, yellow on black
using (ExcelRange r = workSheet.Cells[startRowFrom, 1, startRowFrom, dataTable.Columns.Count])
{
r.Style.Font.Color.SetColor(System.Drawing.Color.White);
r.Style.Font.Bold = true;
r.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
r.Style.Fill.BackgroundColor.SetColor(System.Drawing.ColorTranslator.FromHtml("#1fb5ad"));
}
// format cells - add borders
using (ExcelRange r = workSheet.Cells[startRowFrom + 1, 1, startRowFrom + dataTable.Rows.Count, dataTable.Columns.Count])
{
r.Style.Border.Top.Style = ExcelBorderStyle.Thin;
r.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
r.Style.Border.Left.Style = ExcelBorderStyle.Thin;
r.Style.Border.Right.Style = ExcelBorderStyle.Thin;
r.Style.Border.Top.Color.SetColor(System.Drawing.Color.Black);
r.Style.Border.Bottom.Color.SetColor(System.Drawing.Color.Black);
r.Style.Border.Left.Color.SetColor(System.Drawing.Color.Black);
r.Style.Border.Right.Color.SetColor(System.Drawing.Color.Black);
}
if (!String.IsNullOrEmpty(heading))
{
workSheet.Cells["A1"].Value = heading;
workSheet.Cells["A1"].Style.Font.Size = 20;
workSheet.InsertColumn(1, 1);
workSheet.InsertRow(1, 1);
workSheet.Column(1).Width = 5;
}
result = package.GetAsByteArray();
}
return result;
}
public static byte[] ExportExcel<T>(List<T> data, string Heading = "", bool showSlno = false, params string[] ColumnsToTake)
{
return ExportExcel(ListToDataTable<T>(data), Heading, showSlno, ColumnsToTake);
}
}
并在我的操作方法中使用此代码:
public ActionResult ExportToExcel()
{
byte[] filecontent;
try
{
var juryLists = from user in db.Users
where user.Roles.Any(r => r.RoleId == "IdOfRole")
select user;
var articles = db.Articles.Include(r=>r.ArticleReview).Where(r2=>r2.ArticleReviews.Any()).ToList();
List<string> cmnList = new List<string>();
cmnList.Add("Article Title");
cmnList.Add("Point Avarage");
var juryListNames = juryLists.OrderBy(x=>x.Id).Select(x => "JuryPoints : " + x.FullName).ToList();
cmnList.AddRange(juryListNames);
string[] columns = cmnList.ToArray();
var heading = $"Results";
var dt = new DataTable();
foreach (var column in columns)
{
dt.Columns.Add(column, typeof(string));
}
foreach (var item in articles)
{
var _obj = new object[] { item.ArticleTitle, item.ArticleReviews.OrderBy(x=>x.JuryUserId).Select(x=>x.ArticlePoint).Average() }.Concat(
item.ArticleReviews.OrderBy(x => x.JuryUserId).GroupBy(x => x.JuryUserId).Select(x => x.Average(y => y.ReviewPoint))
.Cast<object>()).ToArray();
dt.Rows.Add(_obj);
}
filecontent = ExcelExportHelper.ExportExcel(dt, heading, true, columns);
}
catch (Exception e)
{
return RedirectToAction("Details",new {id= id });
}
return File(filecontent, ExcelExportHelper.ExcelContentType, "Results.xlsx");
}
我的问题:
我为我的 Excel 创建标题,但问题是一些陪审团用户还不会为文章给出分数,当我生成 Excel 时,另一个陪审团的点放在另一个陪审团的列中。
例如:一篇文章发送给 5 个陪审团,陪审团 1,2 和 5 给文章评分,但在第 5 个陪审团的导出点之后,将其放置在第 3 个陪审团列中
我该如何解决这个问题?