简体   繁体   English

C#导出到Excel

[英]C# export to excel

Which is the best way to export data to an existing xls sheet. 这是将数据导出到现有xls表的最佳方法。 I needs to support many versions of excel. 我需要支持许多版本的excel。 If i was using Visual basic. 如果我使用的是Visual Basic。 I would use the CreateObject("Excel.application") code which will do what i need. 我会使用CreateObject(“ Excel.application”)代码来完成我需要的工作。 What is the equivalent in c#? C#中的等效项是什么? I would love for it to work on any version, and if possible then also computers without ms office at all. 我希望它可以在任何版本上使用,如果可能的话,也可以在没有ms office的计算机上使用。 Please no 3rd party components that cost money. 请不要花费任何第三方资源。 we are poor :). 我们很穷:)。

TY TY

Writing to Excel using C# can be accomplished using the Interop.Excel Here is a nice tutorial with screen shots to help make this happen. 使用Interop.Excel可以完成使用C#编写Excel的操作。这是一个不错的教程,提供了屏幕截图,可帮助您实现这一目标。

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

Excel Export Excel导出

I use EP Plus Excel Export for this. 我为此使用EP Plus Excel Export。 Add EP Plus library from Nuget to your project. Nuget的 EP Plus库添加到您的项目中。 Then add this class to your project. 然后将此类添加到您的项目。

ExcelExport.cs ExcelExport.cs

Try 尝试

And use it on MVC: 并在MVC上使用它:

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

    return null;
}

Also you can define which column with which name shows on Excel Document. 您也可以定义在Excel文档上显示哪个名称的列。 you have to create an attribute class for this. 您必须为此创建一个属性类。

ExportAttribute ExportAttribute

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

Class & Implementation 类与实现

Then, implement this attribute to your class: 然后,对该类实施此属性:

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; }
}

There are built in functions for exporting an ASP.Net grid to excel. 有内置的功能可将ASP.Net网格导出为ex​​cel。 It gives a little warning message to the end user but it works. 它会向最终用户发出一些警告消息,但它可以工作。

It is described here: C Sharp Corner 描述如下: C Sharp Corner

如果要打开excel文档然后添加内容,最简单的方法之一(不要求安装excel)是获取可以读取和保存的3rd party lib(可能有一些开源解决方案) Excel文件。

I know you said no third-party but the reason seems to be cost. 我知道您说没有第三方,但原因似乎是成本。

The library I use is GemBox. 我使用的库是GemBox。 It is free as long as the spreadsheets are not too large (150 rows and 5 worksheets max). 只要电子表格不是太大(150行和最多5个工作表),它是免费的。 For my use this is no problem and it works very well. 对于我来说这没问题,而且效果很好。

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

There are also some open source options. 也有一些开源选项。

If you are writing to the XML format then ExcelPackage is free (GPL) and may work for you: 如果您正在编写XML格式,则ExcelPackage是免费(GPL)的,并且可以为您工作:

http://excelpackage.codeplex.com/ http://excelpackage.codeplex.com/

If you want no XML but the other formats, I hear good things about ExcelLibrary (LGPL): 如果您不希望使用XML,而只希望使用其他格式,那么我会听说有关ExcelLibrary(LGPL)的好消息:

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

There is also a port of the JExcel library (LGPL) to C#: 还有一个JExcel库(LGPL)到C#的端口:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM