当我通过asp.net / C#代码导出Excel文件时,它将保存为网页: 它正在导出,但是当我打开相同的excel文件并尝试保存它时,它将保存为网页格式,我该如何更改? 这些是我用于导出到excel的函数:
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Report1.xls");
Response.ContentType = "application/ms-excel";
const int BUFFER_SIZE = 16 * 1024;
int bytesRead = BUFFER_SIZE;
byte[] bytesBuffer = new byte[bytesRead];
///
memoryStream.Position = 0;
do
{
bytesRead = memoryStream.Read(bytesBuffer, 0, bytesRead);
Response.BinaryWrite(bytesBuffer);
memoryStream.Flush();
} while (bytesRead > 0);
///
答案 0 :(得分:0)
请尝试将您的aspx代码(请参阅下文)删除到您的网页中,并保留此信息:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test.Default" %>
要删除的部分:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
...并在您的代码中:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Test
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=Report1.xls");
Response.ContentType = "application/ms-excel";
const int BUFFER_SIZE = 16 * 1024;
int bytesRead = BUFFER_SIZE;
byte[] bytesBuffer = new byte[bytesRead];
///
memoryStream.Position = 0;
do
{
bytesRead = memoryStream.Read(bytesBuffer, 0, bytesRead);
Response.BinaryWrite(bytesBuffer);
memoryStream.Flush();
} while (bytesRead > 0);
}
}
}