Skip to content

Instantly share code, notes, and snippets.

@weiting-tw
Created January 17, 2018 06:41
Show Gist options
  • Save weiting-tw/cd142f2766a9ae597c2ff75ac7544136 to your computer and use it in GitHub Desktop.
Save weiting-tw/cd142f2766a9ae597c2ff75ac7544136 to your computer and use it in GitHub Desktop.
public Stream ExportSheet(DocumentSearchCriteria criteria)
{
var sheets = SpreadsheetServiceFactory.CreateInstance();
var sheetList = new Dictionary<string, IList<IDictionary<string, object>>>();
IElapsedTimePagedList<Document> docList = Query(criteria);
docList.ToList<Document>().ForEach(x =>
{
var docAttrs = new ConcurrentDictionary<string, object>();
foreach (DocumentAttribute attr in x.Attributes)
{
if (attr.DisplayName != "OwnerSubjectId")
{
if (docAttrs.Where(a => a.Key == attr.DisplayName).Count() == 0)
{
docAttrs.TryAdd(attr.DisplayName, attr.Value.FirstOrDefault());
}
else
{
docAttrs[docAttrs.Where(a => a.Key == attr.DisplayName).FirstOrDefault().Key] += "\r\n" + attr.Value.FirstOrDefault();
}
}
}
var selectSheet = sheetList.Where(s => s.Key == x.Form.DisplayName);
if (selectSheet.Count() == 0)
{
var list = new List<IDictionary<string, object>>
{
docAttrs
};
sheetList.Add(x.Form.DisplayName, list);
}
else
{
sheetList.Where(s => s.Key == x.Form.DisplayName).FirstOrDefault().Value.Add(docAttrs);
}
});
var stream = sheets.WriteMutipleSheet(sheetList);
return stream;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment