Spire.Doc Knowledgebase - Page5
Spire.Doc for .NET

C#/VB.NET: Convert Word to Excel

2022-12-14 01:30:11 Written by Koohji

Word and Excel are two completely different file types. Word documents are used to write essays, letters or create reports, while Excel documents are used to save data in tabular form, make charts, or perform mathematical calculations. It is not recommended to convert a complex Word document to an Excel spreadsheet because Excel can hardly render content according to its original layout in Word.

However, if your Word document is primarily composed of tables and you want to analyze the table data in Excel, you can use Spire.Office for .NET to convert Word to Excel while maintaining good readability.

Install Spire.Office for .NET

To begin with, you need to add the DLL files included in the Spire.Office for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Office

Convert Word to Excel in C# and VB.NET

This scenario actually uses two libraries in the Spire.Office package. They’re Spire.Doc for .NET and Spire.XLS for .NET. The former is used to read and extract content from a Word document, and the latter is used to create an Excel document and write data in the specific cells. To make this code example easy to understand, we created the following three custom methods that preform specific functions.

  • ExportTableInExcel() - Export data from a Word table to specified Excel cells.
  • CopyContentInTable() - Copy content from a table cell in Word to an Excel cell.
  • CopyTextAndStyle() - Copy text with formatting from a Word paragraph to an Excel cell.

The following steps demonstrate how to export data from an entire Word document to a worksheet using Spire.Office for .NET.

  • Create a Document object to load a Word file.
  • Create a Worbbook object and add a worksheet named "WordToExcel" to it.
  • Traverse though all the sections in the Word document, traverse through all the document objects under a certain section, and then determine if a document object is a paragraph or a table.
  • If the document object is a paragraph, write the paragraph in a specified cell in Excel using CoypTextAndStyle() method.
  • If the document object is a table, export the table data from Word to Excel cells using ExportTableInExcel() method.
  • Auto fit the row height and column width in Excel so that the data within a cell will not exceed the bound of the cell.
  • Save the workbook to an Excel file using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Xls;
using System;
using System.Drawing;

namespace ConvertWordToExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\Invoice.docx");

            //Create a Workbook object
            Workbook wb = new Workbook();

            //Remove the default worksheets
            wb.Worksheets.Clear();

            //Create a worksheet named "WordToExcel"
            Worksheet worksheet = wb.CreateEmptySheet("WordToExcel");
            int row = 1;
            int column = 1;

            //Loop through the sections in the Word document
            foreach (Section section in doc.Sections)
            {
                //Loop through the document object under a certain section
                foreach (DocumentObject documentObject in section.Body.ChildObjects)
                {
                    //Determine if the object is a paragraph
                    if (documentObject is Paragraph)
                    {
                        CellRange cell = worksheet.Range[row, column];
                        Paragraph paragraph = documentObject as Paragraph;
                        //Copy paragraph from Word to a specific cell
                        CopyTextAndStyle(cell, paragraph);
                        row++;
                    }

                    //Determine if the object is a table
                    if (documentObject is Table)
                    {
                        Table table = documentObject as Table;
                        //Export table data from Word to Excel
                        int currentRow = ExportTableInExcel(worksheet, row, table);
                        row = currentRow;
                    }
                }
            }

            //Auto fit row height and column width
            worksheet.AllocatedRange.AutoFitRows();
            worksheet.AllocatedRange.AutoFitColumns();

            //Wrap text in cells
            worksheet.AllocatedRange.IsWrapText = true;

            //Save the workbook to an Excel file
            wb.SaveToFile("WordToExcel.xlsx", ExcelVersion.Version2013);
        }

        //Export data from Word table to Excel cells
        private static int ExportTableInExcel(Worksheet worksheet, int row, Table table)
        {
            CellRange cell;
            int column;
            foreach (TableRow tbRow in table.Rows)
            {
                column = 1;
                foreach (TableCell tbCell in tbRow.Cells)
                {
                    cell = worksheet.Range[row, column];
                    cell.BorderAround(LineStyleType.Thin, Color.Black);
                    CopyContentInTable(tbCell, cell);
                    column++;
                }
                row++;
            }
            return row;
        }

        //Copy content from a Word table cell to an Excel cell
        private static void CopyContentInTable(TableCell tbCell, CellRange cell)
        {
            Paragraph newPara = new Paragraph(tbCell.Document);
            for (int i = 0; i < tbCell.ChildObjects.Count; i++)
            {
                DocumentObject documentObject = tbCell.ChildObjects[i];
                if (documentObject is Paragraph)
                {
                    Paragraph paragraph = documentObject as Paragraph;
                    foreach (DocumentObject cObj in paragraph.ChildObjects)
                    {
                        newPara.ChildObjects.Add(cObj.Clone());
                    }
                    if (i < tbCell.ChildObjects.Count - 1)
                    {
                        newPara.AppendText("\n");
                    }
                }
            }
            CopyTextAndStyle(cell, newPara);
        }

        //Copy text and style of a paragraph to a cell
        private static void CopyTextAndStyle(CellRange cell, Paragraph paragraph)
        {
            RichText richText = cell.RichText;
            richText.Text = paragraph.Text;
            int startIndex = 0;
            foreach (DocumentObject documentObject in paragraph.ChildObjects)
            {
                if (documentObject is TextRange)
                {
                    TextRange textRange = documentObject as TextRange;
                    string fontName = textRange.CharacterFormat.FontName;
                    bool isBold = textRange.CharacterFormat.Bold;
                    Color textColor = textRange.CharacterFormat.TextColor;
                    float fontSize = textRange.CharacterFormat.FontSize;
                    string textRangeText = textRange.Text;
                    int strLength = textRangeText.Length;
                    ExcelFont font = cell.Worksheet.Workbook.CreateFont();
                    font.Color = textColor;
                    font.IsBold = isBold;
                    font.Size = fontSize;
                    font.FontName = fontName;
                    int endIndex = startIndex + strLength;
                    richText.SetFont(startIndex, endIndex, font);
                    startIndex += strLength;
                }
                if (documentObject is DocPicture)
                {
                    DocPicture picture = documentObject as DocPicture;
                    cell.Worksheet.Pictures.Add(cell.Row, cell.Column, picture.Image);
                    cell.Worksheet.SetRowHeightInPixels(cell.Row, 1, picture.Image.Height);
                }
            }
            switch (paragraph.Format.HorizontalAlignment)
            {
                case HorizontalAlignment.Left:
                    cell.Style.HorizontalAlignment = HorizontalAlignType.Left;
                    break;
                case HorizontalAlignment.Center:
                    cell.Style.HorizontalAlignment = HorizontalAlignType.Center;
                    break;
                case HorizontalAlignment.Right:
                    cell.Style.HorizontalAlignment = HorizontalAlignType.Right;
                    break;
            }
        }
    }
}

C#/VB.NET: Convert Word to Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

C#/VB.NET: Convert ODT to PDF

2022-09-20 01:05:19 Written by Koohji

ODT files are OpenDocument Text files created with word processing programs such as free OpenOffice Writer. Like DOCX files, ODT files can contain content like text, images, objects and styles, but they may not be readable by some people who don't have the appropriate application installed. If you plan to share an ODT file, it's best to convert it to pdf so everyone can access it. In this article, we will explain how to convert ODT to PDF in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert ODT to PDF using C# and VB.NET

The following are the steps to convert an ODT file to PDF:

  • Create an instance of Document class.
  • Load an ODT file using Document.LoadFromFile() method.
  • Convert the ODT file to PDF using Document.SaveToFile(string fileName, FileFormat fileFormat) method.
  • C#
  • VB.NET
using Spire.Doc;

namespace ConvertOdtToPdf
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //Create a Document instance
            Document doc = new Document();
            //Load an ODT file
            doc.LoadFromFile("Sample.odt");

            //Save the ODT file to PDF
            doc.SaveToFile("OdtToPDF.pdf", FileFormat.PDF);
        }
    }
}

C#/VB.NET: Convert ODT to PDF

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Compared with Word document format, pictures are more convenient to share and preview across platforms, because they do not require MS Word to be installed on machines. Moreover, converting Word to images can preserve the original appearance of the document, which is useful when further modifications are not desired. In this article, you will learn how to convert Word documents to images in C# and VB.NET using Spire.Doc for .NET.

Install Spire.Doc for .NET

To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.Doc

Convert Word to JPG in C#, VB.NET

Spire.Doc for .NET offers the Document.SaveToImages() method to convert a whole Word document into individual Bitmap or Metafile images. Then, a Bitmap or Metafile image can be saved as a BMP, EMF, JPEG, PNG, GIF, or WMF format file. The following are the steps to convert a Word document to JPG images using this library.

  • Create a Document object.
  • Load a Word document using Document.LoadFromFile() method.
  • Convert the document to Bitmap images using Document.SaveToImages() method.
  • Loop through the image collection to get the specific one and save it as a JPG file.
  • C#
  • VB.NET
using Spire.Doc;
using Spire.Doc.Documents;
using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConvertWordToJPG
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx");

            //Convert the whole document into individual images 
            Image[] images = doc.SaveToImages(ImageType.Bitmap);

            //Loop through the image collection
            for (int i = 0; i < images.Length; i++)
            {
                //Save the image to a JPEG format file
                string outputfile = String.Format("‪Image-{0}.jpg", i);‬‬
                images[i].Save("C:\\Users\\Administrator\\Desktop\\Images\\" + outputfile, ImageFormat.Jpeg);
            }
        }
    }
}

Convert Word to SVG in C#, VB.NET

Using Spire.Doc for .NET, you can save a Word document as a queue of byte arrays. Each byte array can then be written as a SVG file. The detailed steps to convert Word to SVG are as follows.

  • Create a Document object.
  • Load a Word file using Document.LoadFromFile() method.
  • Save the document as a queue of byte arrays using Document.SaveToSVG() method.
  • Loop through the items in the queue to get a specific byte array.
  • Write the byte array to a SVG file.
  • C#
  • VB.NET
using Spire.Doc;
using System;
using System.Collections.Generic;
using System.IO;

namespace CovnertWordToSVG
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx");

            //Save the document as a queue of byte arrays
            Queue<byte[]> svgBytes = doc.SaveToSVG();

            //Loop through the items in the queue
            for (int i = 0; i < svgBytes.Count; i++)
            {
                //Convert the queue to an array
                byte[][] bytes = svgBytes.ToArray();

                //Specify the output file name
                string outputfile = String.Format("‪Image-{0}.svg", i);‬‬

                //Write the byte[] in a SVG format file
                FileStream fs = new FileStream("C:\\Users\\Administrator\\Desktop\\Images\\" + outputfile, FileMode.Create);
                fs.Write(bytes[i], 0, bytes[i].Length);
                fs.Close();           
            }   
        }
    }
}

Convert Word to PNG with Customized Resolution in C#, VB.NET

An image with higher resolution is generally more clear. You can customize the image resolution while converting Word to PNG by following the following steps.

  • Create a Document object.
  • Load a Word file using Document.LoadFromFile() method.
  • Convert the document to Bitmap images using Document.SaveToImages() method.
  • Loop through the image collection to get the specific one.
  • Call the custom method ResetResolution() to reset the image resolution.
  • Save the image as a PNG file.
  • C#
  • VB.NET
using Spire.Doc;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using Spire.Doc.Documents;

namespace ConvertWordToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document doc = new Document();

            //Load a Word document
            doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Template.docx");

            //Convert the whole document into individual images 
            Image[] images = doc.SaveToImages(ImageType.Metafile);

            //Loop through the image collection
            for (int i = 0; i < images.Length; i++)
            {
                //Reset the resolution of a specific image
                Image newimage = ResetResolution(images[i] as Metafile, 150);

                //Save the image to a PNG format file
                string outputfile = String.Format("‪Image-{0}.png", i);‬‬
                newimage.Save("C:\\Users\\Administrator\\Desktop\\Images\\" + outputfile, ImageFormat.Png);
            }
        }

        //Set the image resolution by the ResetResolution() method
        public static Image ResetResolution(Metafile mf, float resolution)
        {
            int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
            int height = (int)(mf.Height * resolution / mf.VerticalResolution);
            Bitmap bmp = new Bitmap(width, height);
            bmp.SetResolution(resolution, resolution);
            using (Graphics g = Graphics.FromImage(bmp))
            {
                g.DrawImage(mf, Point.Empty);
            }
            return bmp;
        }
    }
}

C#/VB.NET: Convert Word to Images (JPG, PNG and SVG)

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Page 5 of 57
page 5