gpt4 book ai didi

c# - 如何将在 Interop 中创建的 Excel 文件发送到 Razor MVC 中的浏览器?

转载 作者:行者123 更新时间:2023-11-30 16:17:30 25 4
gpt4 key购买 nike

下面是我用 C# 和 Interop 生成 excel 文件的代码:

         Dictionary<Item, Shift_ItemSales> data = getData(curPage, depID);

Application excelapp = new Application();
excelapp.Visible = false;

_Workbook workbook = (_Workbook)(excelapp.Workbooks.Add(Type.Missing));
_Worksheet worksheet = (_Worksheet)workbook.ActiveSheet;

worksheet.Cells[0, 1] = "ID";
worksheet.Cells[0, 2] = "Description";

int row = 1;
foreach (Item curItem in data.Keys) {
Shift_ItemSales curIS = data[curItem];
worksheet.Cells[row, 0] = curItem.ID;
worksheet.Cells[row, 1] = curItem.Description;
row++;
}

如何将它发送到浏览器并让用户下载它?

最佳答案

您应该为此目的使用 FileResult

    [HttpPost]
public FileResult ExcelDownload(ReportDownloadRequest reportRequest)
{
//Code to generate Excel file with GemBox.Spreadsheet

return new FileStreamResult(report, "application/ms-excel")
{
FileDownloadName = "SalesReport.xls"
};
}

对于 Excel 表格,您可以使用 GemBox.Spreadsheet 免费版提供与专业版相同的性能和功能集。但是,存在以下限制:

  • 每张纸的最大行数为 150。
  • 每个工作簿的最大工作表数为 5。

GemSheet 示例代码:

 // Set license key to use GemBox.Spreadsheet in a Free mode.
SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

// Create new empty Excel file.
var workbook = new ExcelFile();

// Create new worksheet and set cell A1 value to 'Hello world!'.
workbook.Worksheets.Add("Test").Cells["A1"].Value = "Hello world!";

// Save to XLSX file.
workbook.Save("Test.xlsx");

关于c# - 如何将在 Interop 中创建的 Excel 文件发送到 Razor MVC 中的浏览器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17050460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com