1

エクセルシートレポートを生成するためのエクセル相互運用コードを作成しました。レポートが生成され、フォルダーに保存されます。この後、同じファイルをダウンロードしたいのですが、同じアプリケーションでダウンロード用のコードを記述しました。ファイル形式は.xlsxです。ダウンロード中に次のエラーが発生します。

Cannot open the file, file fotmat may be different or file may be corrected.

しかし、ドライブに直接アクセスすると、同じファイルを開くことができます。

コードのダウンロード

private void DownloadFile(string getpathfromappconfig, string FileName)
{
    string path = @getpathfromappconfig + "\\" + FileName + ".xlsx";
    System.IO.FileInfo file = new System.IO.FileInfo(path);
    string Outgoingfile = FileName + ".xlsx"; 
    if (file.Exists)
    { 
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + Outgoingfile);
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        Response.WriteFile(file.FullName);

    }
    else
    {
        Response.Write("This file does not exist.");
    }

}

誰かが私がこれを解決するのを手伝ってくれる?

4

1 に答える 1

3

このコードで試すことができます

Response.ContentType = "application/vnd.ms-excel"

これを交換

Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

または、このコードで試すことができます

            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Open("YourFile.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);


            //your treatment ...

            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            //Clean your objects
于 2013-03-15T10:37:40.443 に答える