C#/VB.NET: Convert Excel to Open XML or Open XML to Excel
Open XML is an XML-based file format developed by Microsoft that allows users to store and exchange various types of files such as word processing documents, spreadsheets, presentations, charts, and diagrams. It is widely recognized and supported by various applications, making it a reliable choice for long-term data preservation.
In some cases, it may be necessary to convert Excel files to Open XML format to ensure that these files can be opened and read by other software. On the other hand, there may be situations where users need to convert Open XML files to Excel format to take advantage of the data analysis tools available in Excel, such as pivot tables and charts. In this article, we will introduce how to convert Excel to Open XML or Open XML to Excel in C# and VB.NET using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS 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.XLS
Convert Excel to Open XML in C# and VB.NET
To convert an Excel file to Open XML, you need to load the Excel file using the Workbook.LoadFromFile(string fileName) method, then call the Workbook.SaveAsXml(string fileName) method to save it in Open XML format.
The following steps demonstrate how to convert an Excel file to Open XML:
- Initialize an instance of the Workbook class.
- Load an Excel file using the Workbook.LoadFromFile(string fileName) method.
- Call the Workbook.SaveAsXml(string fileName) method to save the Excel file in Open XML format.
- C#
- VB.NET
using Spire.Xls; namespace ConvertExcelToOpenXML { internal class Program { static void Main(string[] args) { //Initialize an instance of the Workbook class Workbook workbook = new Workbook(); //Load an Excel file workbook.LoadFromFile("Sample.xlsx"); //Save the Excel file in Open XML file format workbook.SaveAsXml("ExcelToXML.xml"); } } }
Convert Open XML to Excel in C# and VB.NET
To convert an Open XML file to Excel, you need to load the Open XML file using the Workbook.LoadFromXml(string fileName) method, then call the Workbook.SaveToFile(string fileName, ExcelVersion version) method to save it in Excel format.
The following are the steps to convert an Open XML file to Excel:
- Initialize an instance of the Workbook class.
- Load an Open XML file using the Workbook.LoadFromXml(string fileName) method.
- Call the Workbook.SaveToFile(string fileName, ExcelVersion version) method to save the Open XML file in Excel format.
- C#
- VB.NET
using Spire.Xls; namespace ConvertOpenXMLToExcel { internal class Program { static void Main(string[] args) { //Initialize an instance of the Workbook class Workbook workbook = new Workbook(); //Load an Open XML file workbook.LoadFromXml("ExcelToXML.xml"); //Save the Open XML file in Excel XLSX file format workbook.SaveToFile("XMLToExcel.xlsx", ExcelVersion.Version2016); } } }
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 Excel to HTML
When you create an Excel table and want to publish it online as a web page, the simplest way is to convert it to an HTML file. This article will demonstrate how to convert Excel to HTML programmatically from the following two aspects using Spire.XLS for .NET.
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS 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.XLS
Convert Excel to HTML
Spire.XLS for .NET supports converting a specific Excel worksheet to HTML using Worksheet.SaveToHtml() method. Detailed steps are listed below.
- Create a Workbook instance.
- Load an Excel sample document using Workbook.LoadFromFile() method.
- Get a specific worksheet using Workbook.Worksheets[] property
- Save the worksheet as an HTML file using Worksheet.SaveToHtml() method.
- C#
- VB.NET
using Spire.Xls; namespace XLSToHTML { class Program { static void Main(string[] args) { //Create a Workbook instance Workbook workbook = new Workbook(); //Load an Excel sample document workbook.LoadFromFile(@"sample.xlsx"); //Get the first worksheet of the document Worksheet sheet = workbook.Worksheets[0]; //Save the worksheet to HTML sheet.SaveToHtml("ExcelToHTML.html"); } } }
Convert Excel to HTML with Images Embedded
The following are steps to convert an Excel worksheet to HTML with images embedded.
- Create a Workbook instance.
- Load an Excel sample document using Workbook.LoadFromFile() method.
- Get a specific worksheet using Workbook.Worksheets[] property.
- Create an HTMLOptions instance.
- Set the ImageEmbedded as true to embed images to HTML.
- Save the worksheet as an HTML file using Worksheet.SaveToHtml() method.
- C#
- VB.NET
using Spire.Xls; using Spire.Xls.Core.Spreadsheet; namespace XLSToHTML { class Program { static void Main(string[] args) { //Create a Workbook instance Workbook workbook = new Workbook(); //Load an Excel sample document workbook.LoadFromFile(@"sample.xlsx"); //Get the first worksheet of the document Worksheet sheet = workbook.Worksheets[0]; //Create an HTMLOptions instance HTMLOptions options = new HTMLOptions(); //Embed images to HTML options.ImageEmbedded = true; //Save the worksheet to HTML sheet.SaveToHtml("XLS2HTML.html", options); } } }
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.
How to Export DataTable to HTML through DataGridView?
Spire.DataExport supports exporting data from DataTable into HTML. HTML (HyperText Markup Language) is a language for describing web pages. HTML documents are also called web pages which contain HTML tags and plain text. Use Spire.DataExport exporting DataTable to HTML to share your data on internet.
Download Spire.DataExport (or Spire.Office) with .NET Framework together. Only 2 Simple steps you can finish the whole datatable to HTML exporting process.
Step 1: Load Data Information
Before exporting data from DataTable, we should load data information from data source. And select which information we need export. Through DataGridVew, we even can preview and modify data information. So, in this step, our job is to prepare data which are about to be exported out.
private void btnLoad_Click(object sender, EventArgs e) { using (OleDbConnection oleDbConnection = new OleDbConnection()) { oleDbConnection.ConnectionString = this.tbCS.Text; System.Data.OleDb.OleDbCommand oleDbCommand = new OleDbCommand(); oleDbCommand.CommandText = this.tbCT.Text; oleDbCommand.Connection = oleDbConnection; using (OleDbDataAdapter da = new OleDbDataAdapter(oleDbCommand)) { DataTable dt = new DataTable(); da.Fill(dt); dataGridView1.DataSource = dt; } } }
Effect Screenshot
Step 2: Set Export into HTML
Spire.DataExport allows user to export data into most popular file formats including MS Excel, MS Word, HTML, PDF, XML, CSV, DBF, DIF, etc. Here we need set it as HTML format. Spire.DataExport will create a new HTML file and through DataGridView export data into HTML file. You also can rename the file as you like.
private void btnRun_Click(object sender, EventArgs e) { Spire.DataExport.HTML.HTMLExport HTMLExport = new Spire.DataExport.HTML.HTMLExport(); HTMLExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable; HTMLExport.DataTable = this.dataGridView1.DataSource as DataTable; HTMLExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView; HTMLExport.SaveToFile("20110223.html"); }
Effect Screenshot
C#/VB.NET: Convert RTF to Word Doc/Docx and Vice Versa
Rich Text Format (RTF) is a proprietary text file format which can serve as an exchange format between word processing programs from different manufacturers on different operating systems. Rich text documents include page formatting options, such as custom page margins, line spacing, and tab widths. With rich text, it's easy to create columns, add tables, and format your documents in a way that makes them easier to read. This article will demonstrate how to convert Word Doc/Docx to RTF files and convert RTF to Word Doc/Docx with the help of 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 DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Convert Word Doc/Docx to RTF
The following steps show you how to convert Word to RTF using Spire.Doc for .NET.
- Create a Document instance.
- Load a Word sample document using Document.LoadFromFile() method.
- Save the document as an RTF file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; namespace ConvertToRtf { class Program { static void Main(string[] args) { //Create a Word document instance Document document = new Document(); //Load a Word sample document document.LoadFromFile("sample.docx"); // Save the document to RTF document.SaveToFile("Result.rtf", FileFormat.Rtf); } } }
Convert RTF to Word Doc/Docx
The steps to convert an RTF document to Word Doc/Docx is very similar with that of the above example:
- Create a Document instance.
- Load an RTF document using Document.LoadFromFile() method.
- Save the RTF document to Doc/Docx format using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; using System; public class RtfToDocDocx { public static void Main(String[] args) { // Create a Document instance Document document = new Document(); // Load an RTF document document.LoadFromFile("input.rtf", FileFormat.Rtf); // Save the document to Doc document.SaveToFile("toDoc.doc", FileFormat.Doc); // Save the document to Docx document.SaveToFile("toDocx.docx", FileFormat.Docx2013); } }
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 OpenXML to Word or Word to OpenXML
OpenXML is an XML-based file format for MS Office documents. OpenXML is popular among developers as it allows creating and editing Office documents, such Word documents, with no need for MS Office. But when presented to ordinary users, OpenXML files usually need to be converted to Word documents to facilitate reading and editing. In this article, you can learn how to convert XML files to Word documents or Word documents to XML files with the help of 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 an OpenXML File to a Word Document
The detailed steps of the conversion are as follows:
- Create an object of Document class.
- Load an OpenXML file from disk using Document.LoadFromFile() method.
- Convert the OpenXML file to a Word document and save it using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; using System; namespace DocExample { internal class Program { static void Main(string[] args) { //Create a Document class instance Document document = new Document(); //Load an OpenXML file from disk document.LoadFromFile(@"C:\Samples\Sample.xml", FileFormat.Xml); //Convert the OpenXML file to a Word document and save it document.SaveToFile("OpenXMLToWord.docx", FileFormat.Docx2013); } } }
Convert a Word Document to an OpenXML File
The detailed steps of the conversion are as follows:
- Create an object of Document class.
- Load a Word document from disk using Document.LoadFromFile() method.
- Convert the Word document to an OpenXML file and save it using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; using System; namespace DocExample { internal class Program { static void Main(string[] args) { //Create a Document class instance Document document = new Document(); //Load a Word document from disk document.LoadFromFile(@"C:\Samples\Sample.docx"); //Convert the Word document to an OpenXML file and save it //Change WordXML to WordML to convert to ML file document.SaveToFile("WordToOpenXMl.xml", FileFormat.WordXml); } } }
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 HTML to Word
HTML is the standard file format of webpage files that are designed to be displayed in web browsers. Due to the lack of full support for HTML elements in Word, most HTML files cannot be rendered properly in Word. If you do want to maintain the HTML layout while exporting it to Word, you need to change the HTML code and avoid using the elements, attributes, and cascading style sheet properties that are not supported. This article will show you how to convert simple HTML files to Word 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 HTML to Word
The detailed steps are as follows:
- Create a Document instance.
- Load an HTML file from disk using Document.LoadFromFile().
- Convert the HTML file to Word and save it using Document.SaveToFile().
- C#
- VB.NET
using System; using Spire.Doc; using Spire.Doc.Documents; namespace ConvertHTMLtoWord { internal class Program { static void Main(string[] args) { //Create an instance of Document Document document = new Document(); //Load an HTML file form disk document.LoadFromFile(@"D:\testp\test.html"); //Save the HTML file as Word String result = "HtmltoWord.docx"; document.SaveToFile(result, FileFormat.Docx2013); } } }
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 Word to HTML
When you'd like to put a Word document on the web, it's recommended that you should convert the document to HTML in order to make it accessible via a web page. This article will demonstrate how to convert Word to HTML programmatically 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 HTML
The following steps show you how to convert Word to HTML using Spire.Doc for .NET.
- Create a Document instance.
- Load a Word sample document using Document.LoadFromFile() method.
- Save the document as an HTML file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; namespace WordToHTML { class Program { static void Main(string[] args) { //Create a Document instance Document mydoc = new Document(); //Load a Word document mydoc.LoadFromFile("sample.docx"); //Save to HTML mydoc.SaveToFile("WordToHTML.html", FileFormat.Html); } } }
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 Text to Word or Word to Text
Text files are simple and versatile, but they don't support formatting options and advanced features like headers, footers, page numbers, and styles, and cannot include multimedia content like images or tables. Additionally, spell-checking and grammar-checking features are also not available in plain text editors.
If you need to add formatting, multimedia content, or advanced features to a text document, you'll need to convert it to a more advanced format like Word. Similarly, if you need to simplify the formatting of a Word document, reduce its file size, or work with its content using basic tools, you might need to convert it to a plain text format. In this article, we will explain how to convert text files to Word format and convert Word files to text format in C# and VB.NET using Spire.Doc for .NET library.
- Convert a Text File to Word Format in C# and VB.NET
- Convert a Word File to Text Format in C# and VB.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 a Text File to Word Format in C# and VB.NET
Spire.Doc for .NET offers the Document.LoadText(string fileName) method which enables you to load a text file. After the text file is loaded, you can easily save it in Word format by using the Document.SaveToFile(string fileName, FileFormat fileFormat) method. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a text file using the Document.LoadText(string fileName) method.
- Save the text file in Word format using the Document.SaveToFile(string fileName, FileFormat fileFormat) method.
- C#
- VB.NET
using Spire.Doc; namespace ConvertTextToWord { internal class Program { static void Main(string[] args) { //Initialize an instance of the Document class Document doc = new Document(); //Load a text file doc.LoadText("Sample.txt"); //Save the text file in Word format doc.SaveToFile("TextToWord.docx", FileFormat.Docx2016); doc.Close(); } } }
Convert a Word File to Text Format in C# and VB.NET
To convert a Word file to text format, you just need to load the Word file using the Document.LoadFromFile(string fileName) method, and then call the Document.SaveToFile(string fileName, FileFormat fileFormat) method to save it in text format. The detailed steps are as follows:
- Initialize an instance of the Document class.
- Load a Word file using the Document.LoadFromFile(string fileName) method.
- Save the Word file in text format using the Document.SaveToFile(string fileName, FileFormat fileFormat) method.
- C#
- VB.NET
using Spire.Doc; namespace ConvertWordToText { internal class Program { static void Main(string[] args) { //Initialize an instance of the Document class Document doc = new Document(); //Load a Word file doc.LoadFromFile(@"Sample.docx"); //Save the Word file in text format doc.SaveToFile("WordToText.txt", FileFormat.Txt); doc.Close(); } } }
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 Images to PDF
Images are a device-friendly file format that can be easily shared across a variety of devices. However, in some circumstances, a more professional format such as PDF is needed to replace images. In this article, you will learn how to convert an image to PDF in C# and VB.NET using Spire.PDF for .NET.
Spire.PDF does not provide a straightforward method to convert images to PDF. But you could create a new PDF document and draw images at the specified locations of a certain page. Depending on whether to generate PDF in a page size equal to that of the image, this topic can be divided into the following two subtopics.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF 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.PDF
Add an Image to PDF at a Specified location
The following are the steps to add an image as a part of a new PDF document using Spire.PDF for .NET.
- Create a PdfDocument object.
- Set the page margins using PdfDocument.PageSettings.SetMargins() method.
- Add a page using PdfDocument.Pages.Add() method
- Load an image using Image.FromFile() method, and get the image width and height.
- If the image width is larger than the page (the content area) width, resize the image to make it to fit to the page width.
- Create a PdfImage object based on the scaled image or the original image.
- Draw PdfImage object on the first page at (0, 0) using PdfPageBase.Canvas.DrawImage() method.
- Save the document to a PDF file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics; namespace AddImageToPdf { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Set the margins doc.PageSettings.SetMargins(20); //Add a page PdfPageBase page = doc.Pages.Add(); //Load an image Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\announcement.jpg"); //Get the image width and height float width = image.PhysicalDimension.Width; float height = image.PhysicalDimension.Height; //Declare a PdfImage variable PdfImage pdfImage; //If the image width is larger than page width if (width > page.Canvas.ClientSize.Width) { //Resize the image to make it to fit to the page width float widthFitRate = width / page.Canvas.ClientSize.Width; Size size = new Size((int)(width / widthFitRate), (int)(height / widthFitRate)); Bitmap scaledImage = new Bitmap(image, size); //Load the scaled image to the PdfImage object pdfImage = PdfImage.FromImage(scaledImage); } else { //Load the original image to the PdfImage object pdfImage = PdfImage.FromImage(image); } //Draw image at (0, 0) page.Canvas.DrawImage(pdfImage, 0, 0, pdfImage.Width, pdfImage.Height); //Save to file doc.SaveToFile("AddImage.pdf"); } } }
Convert an Image to PDF with the Same Width and Height
The following are the steps to convert an image to PDF with the same page width and height as the image using Spire.PDF for .NET.
- Create a PdfDocument object.
- Set the page margins to zero using PdfDocument.PageSettings.SetMargins() method.
- Load an image using Image.FromFile() method, and get the image width and height.
- Add a page to PDF based on the size of the image using PdfDocument.Pages.Add() method.
- Create a PdfImage object based on the image.
- Draw PdfImage object on the first page from the coordinate (0, 0) using PdfPageBase.Canvas.DrawImage() method.
- Save the document to a PDF file using PdfDocument.SaveToFile() method.
- C#
- VB.NET
using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics; namespace ConvertImageToPdfWithSameSize { class Program { static void Main(string[] args) { //Create a PdfDocument object PdfDocument doc = new PdfDocument(); //Set the margins to 0 doc.PageSettings.SetMargins(0); //Load an image Image image = Image.FromFile(@"C:\Users\Administrator\Desktop\announcement.jpg"); //Get the image width and height float width = image.PhysicalDimension.Width; float height = image.PhysicalDimension.Height; //Add a page of the same size as the image PdfPageBase page = doc.Pages.Add(new SizeF(width, height)); //Create a PdfImage object based on the image PdfImage pdfImage = PdfImage.FromImage(image); //Draw image at (0, 0) of the page page.Canvas.DrawImage(pdfImage, 0, 0, pdfImage.Width, pdfImage.Height); //Save to file doc.SaveToFile("ConvertPdfWithSameSize.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.
C#/VB.NET: Convert XML to PDF
An Extensible Markup Language (XML) file is a standard text file that utilizes customized tags to describe the structure and other features of a document. By converting XML to PDF, you make it easier to share with others since PDF is a more common and ease-to-access file format. This article will demonstrate how to convert XML 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 XML to PDF
The following are steps to convert XML to PDF using Spire.Doc for .NET.
- Create a Document instance.
- Load an XML sample document using Document.LoadFromFile() method.
- Save the document as a PDF file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; namespace XMLToPDF { class Program { static void Main(string[] args) { //Create a Document instance Document mydoc = new Document(); //Load an XML sample document mydoc.LoadFromFile(@"XML Sample.xml", FileFormat.Xml); //Save it to PDF mydoc.SaveToFile("XMLToPDF.pdf", FileFormat.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.