Convert Excel to DOCX in memory using C# and .NET


Complete code

using System;
using System.IO;
using SautinSoft;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            ConvertExcelAsByteArray();
            //ConvertExcelAsMemoryStream();
        }
        public static void ConvertExcelAsByteArray()
        {
            // Convert Excel to DOCX in memory
            ExcelToPdf x = new ExcelToPdf();

            // Set DOCX as output format.
            x.OutputFormat = SautinSoft.ExcelToPdf.eOutputFormat.Docx;

            string excelFile = Path.GetFullPath(@"..\..\..\test.xlsx");
            string docxFile = Path.ChangeExtension(excelFile, ".docx"); ;

            byte[] excelBytes = File.ReadAllBytes(excelFile);
            byte[] docxBytes = null;

            try
            {
                docxBytes = x.ConvertBytes(excelBytes);

                // Save docxBytes to a file for demonstration purposes.
                File.WriteAllBytes(docxFile, docxBytes);
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(docxFile) { UseShellExecute = true });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
        public static void ConvertExcelAsMemoryStream()
        {
            // Convert Excel to DOCX in memory
            ExcelToPdf x = new ExcelToPdf();

            // Set DOCX as output format.
            x.OutputFormat = SautinSoft.ExcelToPdf.eOutputFormat.Docx;

            string excelFile = Path.GetFullPath(@"..\..\..\test.xlsx");
            string docxFile = Path.ChangeExtension(excelFile, ".docx");
            byte[] docxBytes = null;

            try
            {
                // Let us say, we have a memory stream with Excel data.
                using (MemoryStream ms = new MemoryStream(File.ReadAllBytes(excelFile)))
                {
                    docxBytes = x.ConvertBytes(ms.ToArray());
                }
                // Save docxBytes to a file for demonstration purposes.
                File.WriteAllBytes(docxFile, docxBytes);
                System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(docxFile) { UseShellExecute = true });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
    }
}

Download

Imports System
Imports System.IO
Imports SautinSoft

Module Sample

    Sub Main()
        ConvertExcelAsByteArray()
        'ConvertExcelAsMemoryStream()
    End Sub

    Public Sub ConvertExcelAsByteArray()

        ' Convert Excel to DOCX in memory
        Dim x As New ExcelToPdf()

        ' Set DOCX as output format.
        x.OutputFormat = SautinSoft.ExcelToPdf.eOutputFormat.Docx

        Dim excelFile As String = Path.GetFullPath("..\..\..\test.xlsx")
        Dim docxFile As String = Path.ChangeExtension(excelFile, ".docx")


        Dim excelBytes() As Byte = File.ReadAllBytes(excelFile)
        Dim docxBytes() As Byte = Nothing

        Try
            docxBytes = x.ConvertBytes(excelBytes)

            ' Save docxBytes to a file for demonstration purposes.
            File.WriteAllBytes(docxFile, docxBytes)
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(docxFile) With {.UseShellExecute = True})
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.ReadLine()
        End Try
    End Sub

    Public Sub ConvertExcelAsMemoryStream()

        ' Convert Excel to DOCX in memory
        Dim x As New ExcelToPdf()

        ' Set DOCX as output format.
        x.OutputFormat = SautinSoft.ExcelToPdf.eOutputFormat.Docx

        Dim excelFile As String = Path.GetFullPath("..\..\..\test.xlsx")
        Dim docxFile As String = Path.ChangeExtension(excelFile, ".docx")
        Dim docxBytes() As Byte = Nothing

        Try
            ' Let us say, we have a memory stream with Excel data.
            Using ms As New MemoryStream(File.ReadAllBytes(excelFile))
                docxBytes = x.ConvertBytes(ms.ToArray())
            End Using
            ' Save docxBytes to a file for demonstration purposes.
            File.WriteAllBytes(docxFile, docxBytes)
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(docxFile) With {.UseShellExecute = True})
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.ReadLine()
        End Try
    End Sub
End Module

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.