Skip to content

darnley/ExcelConverter

Repository files navigation

ExcelConverter

This package allows you to convert Microsoft Excel files to a Dictionary type. So, with that, you may have a JSON code.

Installation

The main package that have the code is .NET Standard 2.0. So, it is compatible with many versions of .NET Framework and .NET Core.

Usage

Use the method Converter.ExcelConverter.Services.ConvertExcelToDictionary(). It receives a Stream that must contains the Microsoft Excel file (XLSX).

using (var stream = System.IO.File.Open("C:/file.xlsx", FileMode.Open, FileAccess.Read))
{
    var dictionary = service.ConvertExcelToDictionary(stream);
    // Dictionary<string, ICollection<Dictionary<string, object>>>
}

If you would like to use it directly into a DataTableCollection object, use the extension method Converter.ExcelConverter.Extensions.AsDictionary(). In this case, once you are using the extension method, you just have to call it as a DataTableCollection method.

var result = dataSet.Tables.AsDictionary();
// Dictionary<string, ICollection<Dictionary<string, object>>>

Calling from Web API

By default, there is a basic Web API project you can use to send the Microsoft Excel file and receive the JSON code in response's body.

To do this, make a call as the following.

GET /api/converter HTTP/1.1
Host: localhost:5001
Content-Type: multipart/form-data;

Content-Disposition: form-data; name="file"; filename="C:\file_excel.xlsx"

Pay attention to the Content-Disposition name. The default request method uses the file parameter; so, you should put your Microsoft Excel file into it.

Calling from Console application

By default, there is a basic console application you can use to sendn the Microsoft Excel file and write the JSON result in a file in the sabe directory of Excel's file.

To do this, execute the console application (EXE) passing the Microsoft Excel file's full path as parameter.

Converter.Presentation.Console.exe "C:\file_excel.xlsx"

After processing, it will create an .json file in the same directory of .xlsx file. The .json file will have the same name of .xlsx file.

Compatibility

In the version 1.0, this package only supports XLSX files.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT