Asp.Net MVC export to excel

Questo post vuole essere un riassunto di quanto utilizzato per effettuare l'esportazione di dati su excel da un'applicatione Asp.Net MVC.

Step 1 - Librerie

  1. Open XML Format SDK 2.0 - Libreria microsoft con il necessario per creare e gestire file excel.
  2. ExcelPackage: Office Open XML Format file creation - Sostanzialmente un façade sulla precedente libreria (senza è veramente complesso).

Step 2 - Bug fixies

Sono presenti due problemi principali nella librearia ExcelPackage:
  1. Non è possibile creare documenti su uno stream di dati ma solo su file. Per questo è necessario applicare il seguente fix (che consiste nel ricompilare la libreria e applicare alcune modifiche): Using the ExcelPackage class on a web page
  2. E' presente un errore nella gestione delle query xpath, non viene fatto l'escape della stringa di ricerca. Grazie a questo post C-Sharp function to escape single-quote on XPath expression ho modificato la funzione
    private int SetSharedString(string Value)
    ...

    // Check to see if the string already exists. If so, retrieve its index.
    // This search is case-sensitive, but Excel stores differently cased
    // strings separately within the string file.
    //MODIFIED TO USE ParseXpathString
    XmlNode stringNode = _xlWorksheet.xlPackage.Workbook.
    SharedStringsXml
    .SelectSingleNode(string.Format("//d:si[d:t={0}]",
    ParseXpathString(Value)),
    _xlWorksheet.NameSpaceManager);

Step 3. Asp.Net MVC Code

Ho infine creato un extension method che restituisce un ExcelResult che si occupa di trasformare l'object che riceve in input (che può essere IEnumerable, DataSet, DataTable) e trasformarlo in un foglio excel e fare lo streaming sulla risposta.

Step 4. Further works

Sicuramente aggiungere alcune opzioni all'esportazione, come titolo per il report, descrizione etc., il supporto di template, una sintassi per il template.

1 commenti:

Anonimo ha detto...

Basically, if you're 21+ and in the Keystone States, the best PA online on line casino sites have something to supply all customers. Meanwhile, a number of} states like Colorado and Tennessee have expanded from land-based to online betting, but are unlikely to expand their online playing choices any time quickly. The US Supreme Court paved the way way|the method in which} for cellular and online betting sites to go live in more states, and that is what we're seeing. It's clearly the way way|the method in which} to go - in states that enable them, upwards of 80% of all sports wagers are placed online. However, we have not but seen this as widespread as it may be}. Betway is a world-renown sports betting model, but 소울카지노 it also function a feature-rich playing part.

Posta un commento