C#export to excel

时间:2010-08-15 05:07:06

标签: c# .net excel

将数据导出到现有xls工作表的最佳方法是哪种。 我需要支持许多版本的excel。 如果我使用的是Visual Basic。我会使用CreateObject(“Excel.application”)代码,它将完成我需要的工作。 c#中的等价物是什么? 我希望它可以在任何版本上工作,如果可能的话,还可以在没有ms办公室的计算机上工作。 请不要花钱的第三方组件。我们很穷:)。

TY

5 个答案:

答案 0 :(得分:1)

使用C#写入Excel可以使用Interop.Excel来完成这是一个很好的教程,屏幕截图可以帮助实现这一目标。

http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm

答案 1 :(得分:0)

内置函数可将ASP.Net网格导出为ex​​cel。它向最终用户发出一点警告信息但是有效。

此处描述:C Sharp Corner

答案 2 :(得分:0)

如果您想打开Excel文档然后添加内容,最简单的方法之一(无需安装excel)就是获得第三方库(可能有一些开源解决方案)可以读取并保存excel文件。

答案 3 :(得分:0)

我知道你说没有第三方,但原因似乎是成本。

我使用的库是GemBox。只要电子表格不是太大(最多150行和5个工作表),它就是免费的。对我来说这没问题,而且效果很好。

http://www.gemboxsoftware.com/GBSpreadsheet.htm

还有一些开源选项。

如果您正在编写XML格式,那么ExcelPackage是免费的(GPL),可能适合您:

http://excelpackage.codeplex.com/

如果您不想使用XML而是其他格式,我会听到有关ExcelLibrary(LGPL)的好消息:

http://code.google.com/p/excellibrary/

还有一个JExcel库(LGPL)到C#的端口:

http://www.chrislaforetsoftware.com/products.html

答案 4 :(得分:0)

Excel导出

我使用EP Plus Excel导出。将{+ 3}}中的EP Plus库添加到您的项目中。然后将此类添加到项目中。

Nuget

尝试

在MVC上使用它:

public ActionResult ExportToExcel(string cid)
{
    var customerList = new List<DtoCustomer>();
    customerList = CustomerFactory.Gets();
    ExcelExport.EpplusExportToExcelFromObjectList(customerList.ToList<object>(), "Customer List", true);

    return null;
}

您还可以定义Excel Document上显示哪个名称的列。你必须为此创建一个属性类。

ExportAttribute

using System.Attribute;
public class ExportAttribute : Attribute
{
    public string DisplayName { get; set; }
    public bool IsVisible { get; set; }
}

Class&amp;实施

然后,将此属性应用于您的类:

public class DtoCustomer
{
    [ExportAttribute(DisplayName = "#", IsVisible = false)]
    public int ID { get; set; }
    [ExportAttribute(DisplayName = "Customer Name", IsVisible = true)]
    public string Name{ get; set; }
    [ExportAttribute(DisplayName = "Customer Surname", IsVisible = true)]
    public string Surname { get; set; }
}