How to generate a complex json file from excel worksheet - c#

I have an excel file with few columns. I want to write a react
application that will upload/open the excel file, retrieve the data
row by row and generate a complex json file and store it on a server.
I have the schema of what the json file will look like.
How do I start the generation of this json file? Is it also better to
do this on the client side or server side? I am using json.net and new to this.

I recommend you look into using the EPPlus library with the Newtonsoft Json.NET library.
You can read a file upload directly into an Excel package without saving anything to disk, extract the data you need by reading the Excel Worksheet and then create a generic List of a custom class that represents the data you want to serialize, then serialize that list into JSON using JsonConvert.SerializeObject.

Related

Without first parsing, convert Excel file to Json string

Okay, this is different than other posts I'm seeing. I'm not trying to first open an Excel file and parse the contents into a Json object. I'm trying to take the file and convert it to a stream object of some sort or byte[] and then convert that to Json so I can use it as an input parameter to a POST method for a WebAPI.
Here is the full scenario.
I have clients that will use an internal-only website to select one or more Excel files. The workstations the users work on may or may not have Excel installed, thus, all of my Excel processing has to be done on the server. Once the Excel files are processed, they are combined into a System.Data.DataTable and the values are aggregated into one master report. This aggregated report needs to be returned to the client system so it can be saved.
I currently have this site working just fine in ASP.NET using C#. However, I need the "guts" of the website to be a WebAPI so that automation programs I have can make calls directly to the WebAPI and accomplish the same task that the internal-only website does. This will allow all processing for this sort of task to run through one code base (right now, there are about 4 versions of this task and they all behave differently, providing differing output).
The way I thought to do this was to, from the client, convert the Excel files to an array of System.IO.MemoryStream objects, then serialize the full array as a Json.NET stream and upload the stream to the webserver where it will be deserialized back into an array of MemoryStream. Once that is done, I can iterate the array and process each Excel file by the MemoryStream.
My problem is I can't figure out how to convert the MemoryStream[] into Json and then deserialize that up on the server.
Rather than trying to pass the excel file around as JSON let the user upload the file to the server and then process it from there.
In the JSON rather than giving the content of the file put a link to the file.

Convert a xml to xlsx in C#

I have a large grid and i need those data exported to excel.i implemented my solution using openxml, however having nested loops fill each row's columns to set cell values is time consuming.
I'm now trying to export it to xml and then export to xlsx to improve performance.i converted the dataset to xml, now i'm stuck with converting to xlsx. is there a way to convert to xlsx without having nested loops to fill each cell value ?
UPDATE : I was able to do it using following library -
Open XML Format SDK 2.0 Sample - Convert XML to Excel File
There is no direct conversion, but your best bet if you have the XML is to deserialise this so you have access to the data via code, then build an Excel document using an Excel Library (e.g.: http://epplus.codeplex.com)

Round-tripping XML -> Excel -> XML

I'd like to use Excel's XML Map feature from server-side C# in a web app. XML maps enable you to associate an XML schema with a workbook and specify which cells map to which parts of the schema. From there you can import XML files to update cell values, and export XML containing the latest values (i.e. if they have been manually changed). You can do this manually on the Developer tab in Excel.
We already have a licence for Aspose Cells, but (despite some recent work in this area) it doesn't seem to fully support XML maps. Has anyone had any success with this?
I've created a test using Excel Interop, which works (i.e. can set up an XML Map programatically and use it to both import and export values). It boils down to this:
XmlMap map = workbook.XmlMaps.Add(xmlSchema, Missing.Value);
// map cell C3 to the question text
var worksheet = (Worksheet)workbook.Worksheets[1];
Range cell = (Range)worksheet.Cells[3, 3];
cell.XPath.Clear(); // just in case
cell.XPath.SetValue(map, "/Root/Question/Text", Missing.Value, false);
// import the XML to populate the mapped cells
map.ImportXml(xmlData, true);
However, that's not really suitable for use on a server because it relies on running the Excel process in the background.
Is it possible to use Microsoft's Open XML SDK to import or export XML via an XML Map? I've found the Map class, but I think that just represents the metadata for a map that already exists. Does the SDK contain this sort of transformation logic or does it just represent a worksheet's content as classes?
To further providing you details about XMLMaps feature, currently, Aspose.Cells only supports the following options regarding XML Maps:
It can preserve XmlMaps in the template file and re-save the file with it.
It supports to Import XML Mapping from external sources.
It can remove or check the XML mappings.
Currently it does not support the following at the moment:
It cannot add XML Maps to the Excel file.
It does not export XML Maps to save an external file (We will try to support exporting xml Maps in the second quarter of 2013)
My name is Nayyer and I am developer evangelist at Aspose.

Need to stream Excel file to web clients

I have a method that converts a SQL query into a DataView and now I'm trying to convert a DataView to an Excel document that can be streamed from an asp.net site.
I've tried generating a .csv file but Excel would mess up the format.
Then tried OpenXML it appears super slow.
I just need to write out some basic table data so it can be modified in Excel and re-imported.
For creating an excel file you could use the Infragistics Excel engine. There is an example of how to create an excel file from a DataTable here: http://help.infragistics.com/NetAdvantage/ASPNET/Current/CLR3.5/?page=ExcelEngine_Populating_a_Worksheet_from_a_DataSet.html
You could then load the data again from the excel file format as well. If you want to test this you could do so with a trial of NetAdvantage for ASP.NET if you don't already have this:
http://www.infragistics.com/dotnet/netadvantage/aspnetdownloads.aspx

How can I automatically retrieve a CSV file from web, save it in a directory, and access it in C#?

I am working on an application which has to retrieve data from a CSV file online
and display it with a click of a button. However, how can I automatically store
the CSV file in a safe place where I can access the information? I am working with Visual Studio C#.
Thank you.
You want to use the WebClient class to make an http request to the server for the csv file. It should read the whole contents as a string which you can then parse and manipulate at your leisure.
http://msdn.microsoft.com/en-us/library/system.net.webclient(VS.100).aspx
Use System.IO.File to write the contents to a file.
http://msdn.microsoft.com/en-us/library/system.io.file.aspx
The FileHelpers are a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.
The FileHelpers Library
http://www.filehelpers.com/

Categories