簡體   English   中英

打開導出的Excel文件並嘗試保存,並另存為網頁格式

[英]open exported excel file and try to save it, is saving as web page format

當我通過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);
///

請嘗試將您的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);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM