У меня есть фф.код, который создает файл CSV.Для этого я использовал библиотеку SpreadsheetLight, и она прекрасно работает, если открыть ее через MS Excel, но через текстовый редактор, похоже, что текст зашифрован или поврежден.Я подумал, что это проблема кодирования, но я не уверен, что отсутствует или должно быть исправлено в моем коде.
MemoryStream ms = new MemoryStream();
SLDocument slDoc = new SLDocument();
slDoc.RenameWorksheet(SLDocument.DefaultFirstSheetName, detailList.exam_module_value);
slDoc.SetCellValue(1, 1, "[Question#]");
slDoc.SetCellValue(1, 2, "[Question]");
slDoc.SetCellValue(1, 3, "[Letter Choices]");
slDoc.SetCellValue(1, 4, "[Choices]");
slDoc.SetCellValue(1, 5, "[Tagging Correct Answer]");
int questionCnt = 1;
foreach (var q in detailList.ExamQuestions)
{
char ltr = 'a';
slDoc.SetCellValue(rowCtr, 1, questionCnt++);
slDoc.SetCellValue(rowCtr, 2, q.question);
foreach (var c in detailList.ExamChoices.Where(x => x.question_id == q.question_id).OrderBy(x => x.orderNo))
{
slDoc.SetCellValue(rowCtr, 3, ltr.ToString());
slDoc.SetCellValue(rowCtr, 4, c.choice);
slDoc.SetCellValue(rowCtr, 5, c.answer_state ? 1 : 0);
ltr++;
rowCtr++;
}
}
slDoc.SaveAs(ms);
ms.Position = 0;
FileStreamResult file = new FileStreamResult(ms, "text/csv")
{
FileDownloadName = string.Concat(detailList.exam_module_value, ".csv")
};
using (var fileStream = System.IO.File.Create(Server.MapPath(string.Concat(examPath, "/", detailList.exam_module_value, ".csv"))))
{
file.FileStream.Seek(0, SeekOrigin.Begin);
file.FileStream.CopyTo(fileStream);
file.FileStream.Seek(0, SeekOrigin.Begin); //reset position to beginning. If there's any chance the FileResult will be used by a future method, this will ensure it gets left in a usable state
}
ms.Close();

