在asp.net中显示word / excel文件内容mvc4

时间:2015-08-29 06:06:34

标签: c# asp.net excel asp.net-mvc-4 ms-word

我是ASP.net MVC4的初学者。我正在尝试在我的网页中显示单词和excel文件的内容。 我在HomeController中编写了此函数,但它没有显示内容。它只下载了文件。 如何在我的网页中预览文件? 感谢

[HttpGet]
public FileResult downloadFile()
{
  byte[] fileBytes = System.IO.File.ReadAllBytes("J:\\asp.net\\ViewInformation\\ViewInformation\\Content\\book.xlsx");
  return File(fileBytes, "application/vnd.ms-excel", "book.xlsx");
}

1 个答案:

答案 0 :(得分:0)

过去我使用过Microsoft.Office.Interop.Excel.Application。

excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.Workbooks.Open(filePath);

然后像这样访问单元格:

int newId = Convert.ToInt32(values[i, GetColumnPosition(newIdColumn)]);
int oldId = Convert.ToInt32(values[i, GetColumnPosition(oldIdColumn)]);

其中

public static int GetColumnPosition(char alpha)
{
  return char.ToUpper(alpha) - 64;
}

private const char oldIdColumn = 'F';
private const char newIdColumn = 'A';

一些资源: