C # export to excel

What is the best way to export data to an existing xls sheet. I need to support many excel versions. If I used Visual Basic. I would use the CreateObject code ("Excel.application"), which will do what I need. What is equivalent in C #? I would like him to work on any version, and, if possible, then computers without ms office at all. Please do not have third-party components that cost money. we are poor :).

Ty

+1
source share
5 answers

Writing to Excel using C # can be done using Interop.Excel . Here is a good tutorial with screenshots to help do this.

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

+1
source

There are built-in functions for exporting ASP.Net grids for Excel. It gives the user a small warning, but it works.

Described Here: C Sharp Corner

0
source

If you want to open an excel document and then add content, one of the easiest ways (without having to install excel) is to get a third-party lib (maybe there are some open source solutions) that can read and save excel files.

0
source

I know that you said that there are no third parties, but the reason is apparently worth it.

In the library, I use GemBox. It is free until the spreadsheets are too large (150 rows and a maximum of 5 sheets). For my use this is not a problem and it works very well.

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

There are also some open source options.

If you write in XML, then ExcelPackage is free (GPL) and may work for you:

http://excelpackage.codeplex.com/

If you do not want XML, but other formats, I am well versed in ExcelLibrary (LGPL):

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

There is also a JExcel library port (LGPL) in C #:

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

0
source

Excel Export

For this I use EP Plus Excel Export. Add the EP Plus library from Nuget to your project. Then add this class to your project.

ExcelExport.cs

Try

And use it on MVC:

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

You can also determine which column indicates the name in the Excel document. you need to create an attribute class for this.

ExportAttribute

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

Class and implementation

Then we implement this attribute for 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; } } 
0
source

Source: https://habr.com/ru/post/1336164/


All Articles