gpt4 book ai didi

c# - 在 EPPlus 中编写 Excel 文件

转载 作者:IT王子 更新时间:2023-10-29 04:29:10 25 4
gpt4 key购买 nike

我已经坚持了好几天,尽管提供了所有帮助,但这些解决方案都没有为我工作。我想要做的是使用 EPPlus 库创建一个 excel 文件,其中包含我从存储过程中提取的一些基本数据。这是我在 ExportDocument.cs 文件中的代码:

public static ExcelPackage CreateExcelDocument(int [] arr)
{
String path = @"D:\temp\testsheet3.xlsx";
//FileInfo newFile = null;
/*if (!File.Exists(path + "\\testsheet2.xlsx"))
newFile = new FileInfo(path + "\\testsheet2.xlsx");
else
return newFile;*/
using (ExcelPackage package = new ExcelPackage())
{
ExcelWorksheet ws = package.Workbook.Worksheets.Add("testsheet");
ws.Cells["B1"].Value = "Number of Used Agencies";
ws.Cells["C1"].Value = "Active Agencies";
ws.Cells["D1"].Value = "Inactive Agencies";
ws.Cells["E1"].Value = "Total Hours Volunteered";
ws.Cells["B1:E1"].Style.Font.Bold = true;

int x = 2;
char pos = 'B';
foreach (object o in arr)
{
String str = pos + x.ToString();
ws.Cells[str].Value = o.ToString();
if (pos > 'E')
{
pos = 'B';
x++;
}
pos++;
}
package.Save();
return package;
}
}

所有注释代码都是我在互联网上找到的不同的东西来尝试。请注意,这是一个学校组织,我们没有使用 MVC。然后我使用代码隐藏文件来拉这个方法是这样的:

protected void GenerateReport(Object o, EventArgs e)
{
Session["reportSession"] = ddReport.SelectedItem.Value.ToString();
int [] arr = new int [ReportRepository.GetAgencyCounts().Count];
ReportRepository.GetAgencyCounts().CopyTo(arr, 0);

ExcelPackage pck = ExportDocument.CreateExcelDocument(arr);
/*try
{
byte [] data = ExportDocument.CreateExcelDocument(arr).GetAsByteArray();
Response.Clear();
Response.Buffer = true;
Response.BinaryWrite(data);
Response.AddHeader("content-length", data.Length.ToString());
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.Flush();
Response.Close();
Response.End();
}
catch (Exception ex) { }*/

/*var stream = new MemoryStream();
pck.SaveAs(stream);

String filename = "myfile.xlsx";
String contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
var cd = new System.Net.Mime.ContentDisposition
{
Inline = false,
FileName = filename
};
Response.AppendHeader("Content-Disposition", cd.ToString());
stream.Position = 0;

return File(stream, contentType, filename);*/

/*Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.TransmitFile(Path.GetFullPath(file.Name));
Response.Flush();
Response.Close();*/

/*Response.ClearHeaders();
Response.BinaryWrite(pck.GetAsByteArray());
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=Sample2.xlsx");
Response.Flush();
Response.Close();*/
}

再次注意,所有带注释的代码都是我从各种来源找到的,但没有用。

所以我没有收到任何错误,但是当我单击应用程序上的按钮以执行代码隐藏方法时,什么也没有发生。它正在加载并运行,但没有创建文件,也没有打开任何文件。这是我第一次使用 EPPlus,而且我并不完全熟悉以编程方式将内容导出到 excel,所以我在这里感到迷茫。

你们有什么建议吗?我也很乐意澄清我没有完全理解的任何观点。

最佳答案

您是否看过随 EPPlus 提供的样本?

本节向您展示如何创建文件 http://epplus.codeplex.com/wikipage?title=ContentSheetExample

这个向您展示了如何使用它来流回文件 http://epplus.codeplex.com/wikipage?title=WebapplicationExample

这就是我们使用包生成文件的方式。

var newFile = new FileInfo(ExportFileName);
using (ExcelPackage xlPackage = new ExcelPackage(newFile))
{
// do work here
xlPackage.Save();
}

关于c# - 在 EPPlus 中编写 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15651272/

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