2009-03-16 13 views
29

を開くときに、スプレッドシートがまだ表示されますが、警告メッセージを持ちます。問題は、Excel 2007以前のバージョンのExcelよりもその拡張子に一致する形式の詳細うるさいですので、発生するようです。エクセルのスプレッドシート生成結果Excel 2007で

この問題は、ASP.Netプログラムによって最初に検出され、Excelのエラー "開こうとしているファイル" Spreadsheet.aspx-18.xls "が、ファイルで指定されている形式とは異なる形式拡張。確認してください... "私は、ファイルを開くと。しかし、それがうまく表示されます。私はFirefoxはエクセル97-2003ワークシートとしてファイルを特定するエクセル2007を使用しています。ここで

が生成する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"); 

    } 

}

T

+0

をhttp://support.microsoft.com/kbを参照してください/ 948615 –

答えて

28

http://blogs.msdn.com/vsofficedeveloper/pages/Excel-2007-Extension-Warning.aspx

として拡張子を指定あなたの記述問題について、それはASP.NETコード内から抑えることができないことを知っています。クライアントのレジストリで抑制/修正する必要があります。

+0

私はこれに遭遇しました、これは問題です。 – brendan

+3

この資料には、レジストリを変更する必要のない別の修正も含まれています。私は次の行を追加しました:Response.AddHeader( "Content-Disposition"、 "Attachment; Filename = Spreadsheet.csv");コンマ区切りのファイルを生成します。 .txtファイルでタブを使用していた可能性があります。 –

+2

Jeff Bloomに加えて、出力がxmlとしてExcelの場合は、私の答えをご覧ください –

0

私は、グリッドを使用して、私はその方法論に問題があるためには至っていない応答タイプを変更するより好きAAM。私はストレートタブで区切られたファイルを使用していません。 1つの可能性は、\ n \ nでなければならない可能性があります。ちょうど盲目のショット。基本的にMSを説明するリンクである

-2

使用

コンテンツタイプ=アプリケーション/ vnd.openxmlformats-officedocument.spreadsheetml.sheet

そしてXLSX

18

あなたが私に似ていると2003年のXMLドキュメントとしてExcelシートを生成する場合は、次の操作を実行して、警告を削除することができます: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は警告を表示しませんファイルの内容とファイルの拡張子が一致していません。私はこの質問を見てきました

+3

これは、ファイルがExcelで自動的に開かないことを意味します。 – gb2d

+2

@BombDefusedこれはExcelで自動的に開きます。 excel> 2003のバージョンを入手している場合は、 '<?mso-application progid =" Excel.Sheet "?>'という行がそれを容易にします。動作していない場合は、ファイルの関連付けまたはExcel設定が原因である可能性があります。 –

2

は何度も尋ねました。私は、私はあなたがこの便利を願っ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(); 
    } 
} 

使用して問題を修正し、今日と同じ難易度に走りました。

1

私はこの問題をいくつかの日に解決しようとしていました。最後に、私はここで解決策を見つけた:http://www.aspsnippets.com/Articles/Solution-ASPNet-GridView-Export-to-Excel-The-file-you-are-trying-to-open-is-in-a-different-format-than-specified-by-the-file-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(); 
     } 
    } 
関連する問題