Генерация электронных таблиц Excel приводит к «другим формату файла, чем ошибка расширения» при открытии в Excel 2007

StackOverflow https://stackoverflow.com/questions/652377

  •  19-08-2019
  •  | 
  •  

Вопрос

Электронная таблица все еще отображается, но с предупреждающим сообщением. Проблема, по -видимому, возникает потому, что Excel 2007 более разборчив в отношении форматов, соответствующих их расширениям, чем более ранние версии Excel.

Проблема была первоначально обнаружена программой ASP.NET и создает в ошибке Excel «Файл, который вы пытаетесь открыть,« Spreadsheet.aspx-18.xls », находится в другом формате, чем указано расширением файла. Проверьте ... ». Однако, когда я открываю файл, он отображается просто отлично. Я использую Excel 2007. Firefox идентифицирует файл как рабочий лист Excel 97-2003.

Вот страница ASP.NET, которая генерирует проблему:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Spreadsheet.aspx.cs" Inherits="Spreadsheet" %>

Код за файлом выглядит как:

public partial class Spreadsheet : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "application/vnd.ms-excel";
        Response.Clear();
        Response.Write("Field\tValue\tCount\n");

        Response.Write("Coin\tPenny\t443\n");
        Response.Write("Coin\tNickel\t99\n"); 

    } 

}

Т

Это было полезно?

Решение

http://blogs.msdn.com/vsofficeedeveloper/pages/excel-2007-extension-warning.aspx

Это ссылка, в основном описывающая, что MS знает о проблеме, которую вы описываете, и о том, что ее нельзя подавить из кода ASP.NET. Он должен быть подавлен/исправлен в реестре клиента.

Другие советы

Если вы похожи на меня и генерируете лист Excel как документ XML 2003 года, вы можете удалить предупреждения, выполнив следующее:

Добавлено к выходу XML:

<?xml version="1.0" encoding="utf-16"?>
  <?mso-application progid="Excel.Sheet"?>
  ...

Добавлено на страницу загрузки:

// Properly outputs the xml file
response.ContentType = "text/xml";

// This header forces the file to download to disk
response.AddHeader("content-disposition", "attachment; filename=foobar.xml");

Теперь Excel 2007 не будет отображать предупреждение о том, что содержимое файла и расширение файла не совпадают.

Я видел этот вопрос много раз. Я столкнулся с той же сложностью сегодня, поэтому я исправил проблему, используя NPOI npoi.codeplex.com/

public static class ExcelExtensions
{
    /// <summary>
    /// Creates an Excel document from any IEnumerable returns a memory stream
    /// </summary>
    /// <param name="rows">IEnumerable that will be converted into an Excel worksheet</param>
    /// <param name="sheetName">Name of the Ecel Sheet</param>
    /// <returns></returns>
    public static FileStreamResult ToExcel(this IEnumerable<object> rows, string sheetName)
    {
        // Create a new workbook and a sheet named by the sheetName variable
        var workbook = new HSSFWorkbook();
        var sheet = workbook.CreateSheet(sheetName);

        //these indexes will be used to track to coordinates of data in our IEnumerable
        var rowIndex = 0;
        var cellIndex = 0;

        var excelRow = sheet.CreateRow(rowIndex);

        //Get a collection of names for the header by grabbing the name field of the display attribute
        var headerRow = from p in rows.First().GetType().GetProperties()
                        select rows.First().GetAttributeFrom<DisplayAttribute>(p.Name).Name;


        //Add headers to the file
        foreach (string header in headerRow)
        {
            excelRow.CreateCell(cellIndex).SetCellValue(header);
            cellIndex++;
        }

        //reset the cells and go to the next row
        cellIndex = 0;
        rowIndex++;

        //Inset the data row
        foreach (var contentRow in rows)
        {
            excelRow = sheet.CreateRow(rowIndex);

            var Properties = rows.First().GetType().GetProperties();

            //Go through each property and inset it into a single cell
            foreach (var property in Properties)
            {
                var cell = excelRow.CreateCell(cellIndex);
                var value = property.GetValue(contentRow);

                if (value != null)
                {
                    var dataType = value.GetType();

                    //Set the type of excel cell for different data types
                    if (dataType == typeof(int) ||
                        dataType == typeof(double) ||
                        dataType == typeof(decimal) ||
                        dataType == typeof(float) ||
                        dataType == typeof(long))
                    {
                        cell.SetCellType(CellType.NUMERIC);
                        cell.SetCellValue(Convert.ToDouble(value));
                    }
                    if (dataType == typeof(bool))
                    {
                        cell.SetCellType(CellType.BOOLEAN);
                        cell.SetCellValue(Convert.ToDouble(value));
                    }
                    else
                    {
                        cell.SetCellValue(value.ToString());
                    }
                }
                cellIndex++;
            }

            cellIndex = 0;
            rowIndex++;
        }

        //Set the width of the columns
        foreach (string header in headerRow)
        {
            sheet.AutoSizeColumn(cellIndex);
            cellIndex++;
        }


        return workbook.GetDownload(sheetName);
    }

    /// <summary>
    /// Converts the NPOI workbook into a byte array for download
    /// </summary>
    /// <param name="file"></param>
    /// <param name="fileName"></param>
    /// <returns></returns>
    public static FileStreamResult GetDownload(this NPOI.HSSF.UserModel.HSSFWorkbook file, string fileName)
    {
        MemoryStream ms = new MemoryStream();

        file.Write(ms); //.Save() adds the <xml /> header tag!
        ms.Seek(0, SeekOrigin.Begin);

        var r = new FileStreamResult(ms, "application/vnd.ms-excel");
        r.FileDownloadName = String.Format("{0}.xls", fileName.Replace(" ", ""));

        return r;
    }

    /// <summary>
    /// Get's an attribute from any given property
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="instance"></param>
    /// <param name="propertyName"></param>
    /// <returns></returns>
    public static T GetAttributeFrom<T>(this object instance, string propertyName) where T : Attribute
    {
        var attrType = typeof(T);
        var property = instance.GetType().GetProperty(propertyName);
        return (T)property.GetCustomAttributes(attrType, false).First();
    }
}

Надеюсь, вы найдете это полезным.

Я пытался решить эту проблему в течение нескольких дней. Наконец, я нашел решение здесь: http://www.aspsnippets.com/articles/solution-aspnet-gridview-export-to-excel-the-file-you-arting-to-open-is-in-a-different-format-whan- Указано за файлом extension.aspx

Пространства имен:

using System.IO;
using System.Data;
using ClosedXML.Excel;

Код:

DataTable dt = new DataTable("GridView_Data");
// Fill your DataTable here...

//Export:
    using (XLWorkbook wb = new XLWorkbook())
    {
        wb.Worksheets.Add(dt);

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.AddHeader("content-disposition", "attachment;filename=GridView.xlsx");
        using (MemoryStream MyMemoryStream = new MemoryStream())
        {
            wb.SaveAs(MyMemoryStream);
            MyMemoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();
        }
    }

Мне больше нравится использовать сетку и менять тип ответа, который у меня еще не возникал проблемы с этой методологией. Я не использовал прямые файлы с разграниченными вкладками. Одна возможность - это, возможно, должно быть r n. Просто слепой выстрел.

Use

content-type=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

And specify extension as xlsx

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top