Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# excel保存和下载_C#_Asp.net_Excel_Download - Fatal编程技术网

C# excel保存和下载

C# excel保存和下载,c#,asp.net,excel,download,C#,Asp.net,Excel,Download,我正试图从代码中保存一个excel文件,并将其下载给用户 创建文件的代码: Excel.Application ExcelObj = new Excel1.Application(); Excel.Workbook wb = ExcelObj.Workbooks.Add(); Excel.Worksheet wsh = wb.ActiveSheet; wsh.Cells[1, 1] ="....."; //fill data in the file string erro

我正试图从代码中保存一个excel文件,并将其下载给用户

创建文件的代码:

  Excel.Application ExcelObj = new Excel1.Application();
  Excel.Workbook wb = ExcelObj.Workbooks.Add();
  Excel.Worksheet wsh = wb.ActiveSheet;
  wsh.Cells[1, 1] =".....";
  //fill data in the file
  string errorsFileName = "Error.xls";
  string url = @"..." + errorsFileName;
  wb.SaveAs(url);
  wb.Close();
创建后,我可以从他的位置打开文件保存得很好。

下一步是将该文件下载给用户,我使用以下代码(在不同情况下对我有效!):

下载已完成,但下载的文件包含胡言乱语


是什么原因造成的?

文件是否包含多种语言的字符?是的。。英语和utf8字符。我能做些什么来解决它?一切都是胡言乱语或只是utf8字符?尝试通过控制面板->区域和语言将键盘语言设置为其他语言。它是否改变了某些内容?请尝试更改内容编码响应。ContentEncoding=System.Text.encoding.UTF8;user3165438-一切都是胡言乱语,不仅仅是utf8字符。
 System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
 response.ClearContent();
 response.Clear();
 //response.ContentType = "text/plain";
 response.ContentType = "application/ms-excel";
 response.ContentEncoding = System.Text.Encoding.Default;
 response.BinaryWrite(System.Text.Encoding.Default.GetPreamble());
 response.Charset = "";
 response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
 response.TransmitFile(url);
 response.Flush();