开发者

MVC3 Download Excel Workbook from CSV data

开发者 https://www.devze.com 2023-04-01 01:01 出处:网络
I\'ve got a set of data that exists in memory in a CSV format.I have this method in my controller: public FileContentResult ItemsAsExcelExport(){

I've got a set of data that exists in memory in a CSV format. I have this method in my controller:

     public FileContentResult ItemsAsExcelExport(){
         var model = _itemService.GetExcelExportModel();
        return new FileContentResult(model.CSVData, model.MimeType){FileDownloadName = model.FileName};
     }

The problem here is that my model.CSVData property returns a simple comma delimited set of values. I'm not sure how I can satisfy the fileContents argument of the FileContentResu开发者_StackOverflowlt contructor. It's asking for a byte array.

Thanks in advance.


Take a look at this question How do I get a consistent byte representation of strings in C# without manually specifying an encoding?

The solution is

  byte[] b1 = System.Text.Encoding.UTF8.GetBytes (myString); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号