简体   繁体   中英

Export to Excel spreadsheet in Cross Platform C# .NET app

So I'm writing a cross platform app that will run on Mac, Linux, and Windows. I need a library that can export to a.xlsx without having to have Excel installed on the host computer. Also, I will need to include simple formulas in the spreadsheet. I'm brand new to .NET so I don't know which version of .NET Core the library should be compatible with in order for the app to be cross platform capable. I've heard of ClosedXML which looks perfect for what I'm trying to do. But absolutely no where on the NuGet page nor on their GitHub page does it state whether it's compatible with .NET Core or cross-platform, etc. Please help. I've written whole programs before only to find out I've written it with the completely wrong tools.

You can try my SwiftExcel library. Besides that it is cross platform (if you install it into your .NET Core application), this library is also very efficient as it writes directly to the file. For example you can write 100k rows in few seconds without any memory usage.

Here is a simple example of usage:

using (var ew = new ExcelWriter("C:\\temp\\test.xlsx"))
{
    for (var row = 1; row <= 10; row++)
    {
        for (var col = 1; col <= 5; col++)
        {
            ew.Write($"row:{row}-col:{col}", col, row);
        }
    }
}

You can also use some basic formulas like Count , Max , Sum or Average . Here is an example from the same GitHub page:

using (var ew = new ExcelWriter("C:\\temp\\test.xlsx"))
{
    for (var row = 1; row <= 20; row++)
    {
        ew.Write(row.ToString(), 1, row, DataType.Number);
    }

    ew.WriteFormula(FormulaType.Average, 1, 22, 1, 1, 20);
    ew.WriteFormula(FormulaType.Count, 1, 23, 1, 1, 20);
    ew.WriteFormula(FormulaType.Max, 1, 24, 1, 1, 20);
    ew.WriteFormula(FormulaType.Sum, 1, 25, 1, 1, 20);
}

When confronted with this, I start with a simple proof of concept application. In your case that would be: Create an.xls file with just a single entry. With such a simple project, it's easy to try out several libraries quickly.

Start by creating a .NET core project, then import a nuget package and see if it still compiles. If it does, great, try and let it run on android instead of windows. If it still runs, great. Now you can start writing the first lines of code where you use the library. Just try to create a.xls with a simple entry. Try it on windows, android. If that works test out if the library has all the functionality you need.

If all that passes you've got something that's probably going to work.

Divide and conquer, always go step by step.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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