Transform document xls / xlsx to html - without interop

0

I have a document xls and another xlsx already saved in bytes[] in my code and I need to pass it to html+css to be able to display it on a page.

It is important to point out that I also need the css and that means that reading each row individually from the excel is not an option as it would lose the style.

I am using Visual Studio with .NET to develop it in .

Currently I already work from pdf that is easy to transform to html but it is not the case of xls , xlsx or any product of Microsoft since I can not use the native library interop since it does not it is a guarantee that the server has it installed.

The result would be something like this:

strFinalXls = strFinalXls.Replace("<body>", "<body>" + documentInfoHtml + "<BR /><BR />");

Where documentInfoHtml is the result of transforming the bytes[] to html and strFinalXls is simply the content that replaces the body of a page.

I have found some solution but practically all use interop or else payment libraries.

Do you know any way to do it with free software or open projects?

It is also important to maintain the existing CSS styles as much as possible so answers that simply extract the content to generate it in HTML is not enough in the sense that I would lose all the format.

    
asked by Miquel Coll 19.07.2016 в 09:00
source

1 answer

0

From the library NPOI for C # (downloaded from the github, NOT Nuget, since in the Nuget some DLLs are missing):

Main Code - Important to note that an XLS is different from an XLSX (it is marked in the code)

Stream stream = new MemoryStream(document.Fichero); //document.Fichero son los byte[] puros del XLS/XLSX
Stream stream2 = new MemoryStream();
stream.Position = 0;
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter();
excelToHtmlConverter.OutputColumnHeaders = false;
excelToHtmlConverter.OutputHiddenColumns = true;
excelToHtmlConverter.OutputHiddenRows = true;
excelToHtmlConverter.OutputLeadingSpacesAsNonBreaking = false;
excelToHtmlConverter.OutputRowNumbers = true;
excelToHtmlConverter.UseDivsToSpan = true;

if (document.Extension.ToUpper() == "XLSX")
{
    //XLSX
    XSSFWorkbook workbook = new XSSFWorkbook(stream);
    excelToHtmlConverter.ProcessWorkbook(workbook);
}
else
{
    //XLS
    HSSFWorkbook workbook = new HSSFWorkbook(stream);
    excelToHtmlConverter.ProcessWorkbook(workbook);
}

excelToHtmlConverter.Document.Save(stream2);
stream2.Position = 0;
var mmmm = ReadFully(stream2);
var msAux2 = ConvertDocumentToHtmlMemoryStream(mmmm, "");
var documentInfoHtml = BytesToStringConverted(msAux2.ToArray());

Define the functions ReadFully , ConvertDocumentToHtmlMemoryStream and BytesToStringConverted :

ReadFully - Transform a Stream to byte[] :

public static byte[] ReadFully(Stream input)
{
    using (MemoryStream ms = new MemoryStream())
    {
        input.CopyTo(ms);
        return ms.ToArray();
    }
}

ConvertDocumentToHtmlMemoryStream - Add information for generation to HTML

private MemoryStream ConvertDocumentToHtmlMemoryStream(byte[] document, string documentInfoHtml)
{
    byte[] result = null;

    //Documento
    var strBuilder = new System.Text.StringBuilder();
    foreach (var b in document)
    {
        strBuilder.Append((char)b);
    }
    var strFinalDoc = strBuilder.ToString();
    strFinalDoc = strFinalDoc.Replace("<a href=", "<br /><a href=");
    strFinalDoc = strFinalDoc.Replace("</a>", "</a><br />");

    //Información
    if (!String.IsNullOrEmpty(documentInfoHtml))
    {
        strFinalDoc = strFinalDoc.Replace("<body>", "<body>" + documentInfoHtml + "<BR /><BR />");
    }

    //Imprimir
    if (Action.ToLower() == Enumerators.ActionType.Print.ToString().ToLower())
    {
        var strImprime = "<script language='javascript' type='text/javascript'>";
        strImprime += "     window.print();";
        strImprime += "</script>";
        strImprime += "</html>";

        strFinalDoc = strFinalDoc.Replace("</html>", strImprime);
    }

    var bytes = System.Text.Encoding.Default.GetBytes(strFinalDoc);
    if (bytes.Length > 0)
    {
        return new MemoryStream(bytes);
    }
    else
    {
        return null;
    }
}

BytesToStringConverted - Transforms from byte[] to string

static string BytesToStringConverted(byte[] bytes)
{
    using (var stream = new MemoryStream(bytes))
    {
        using (var streamReader = new StreamReader(stream))
        {
            return streamReader.ReadToEnd();
        }
    }
}
    
answered by 19.07.2016 / 09:00
source