MVC プロジェクトでレポートを生成しています。ユーザーは、レポートを .pdf 形式または .xls 形式で取得するオプションがあります。
Excel ファイルの生成に Aspose.Cells を使用しています。以下の ActionResult メソッドが呼び出されます。
[HttpGet]
public ActionResult GenerateReport(string format, string filterDate = "")
{
//Processing occurs here to get the appropriate info from Db.
var fileFormat = format.ToUpper() == "PDF" ? Format.Pdf : Format.Csv;
var contentType = fileFormat == Format.Pdf ? "application/pdf" : "application/vnd.ms-excel";
var makePdf = fileFormat == Format.Pdf;
var fileContents = register.GetReport(makePdf, filterDate);
return File(fileContents, contentType, "Report");
}
register.GetReport() は、GetExcelVersion() または GetPdfVersion() が呼び出されたかどうかを判断するだけです。
private void GetExcelVersion(MemoryStream stream, string name, string dateRequested = "")
{
var license = new Aspose.Cells.License();
license.SetLicense("Aspose.Total.lic");
var workbook = new Workbook();
var worksheet = workbook.Worksheets[0];
var cells = worksheet.Cells;
//writes out the appropriate information to the excel spreadsheet here
workbook.Save(stream, new XlsSaveOptions(Aspose.Cells.SaveFormat.Excel97To2003));
}
これは Firefox と IE10 では魅力的に機能しますが、IE8 でテストすると、Excel から次の警告が表示されます。
「XXXXX」を開こうとしているファイルは、ファイル拡張子で指定された形式とは異なる形式です。ファイルを開く前に、ファイルが破損しておらず、信頼できるソースからのものであることを確認してください。今すぐファイルを開きますか? はい・いいえ
私が間違っていることについてのアイデアはありますか?
乾杯!