Convert Shapes and SmartArt in Excel to Image in C#, VB.NET
This article demonstrates how to convert shapes and SmartArt graphics in Excel to Image in C# using Spire.XLS for .NET.
The input Excel file:
using Spire.Xls; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; namespace Convert_Shapes_and_SmartArt_to_Image { class Program { static void Main(string[] args) { //Create a Workbook object Workbook workbook = new Workbook(); //Load the Excel file workbook.LoadFromFile("Sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Create a SaveShapeTypeOption object SaveShapeTypeOption shapelist = new SaveShapeTypeOption(); //Save shapes and SmartArt graphics in the worksheet to images List images = sheet.SaveShapesToImage(shapelist); //Save images to file int index = 0; foreach (Image img in images) { img.Save("Image/" + "toImage" + index + ".Png", ImageFormat.Png); index++; } } } }
Imports Spire.Xls Imports System.Collections.Generic Imports System.Drawing.Imaging Namespace Convert_Shapes_and_SmartArt_to_Image Friend Class Program Private Shared Sub Main(ByVal args As String()) 'Create a Workbook object Dim workbook As Workbook = New Workbook() 'Load the Excel file workbook.LoadFromFile("Sample.xlsx") 'Get the first worksheet Dim sheet As Worksheet = workbook.Worksheets(0) 'Create a SaveShapeTypeOption object Dim shapelist As SaveShapeTypeOption = New SaveShapeTypeOption() 'Save shapes and SmartArt graphics in the worksheet to images Dim images As List(Of Bitmap) = sheet.SaveShapesToImage(shapelist) 'Save images to file Dim index As Integer = 0 For Each img As Image In images img.Save("Image/" & "toImage" & index & ".Png", ImageFormat.Png) index += 1 Next End Sub End Class End Namespace
Converted images:
C#: Convert HTML to Excel
HTML (HyperText Markup Language) is primarily used for structuring content on web pages. While it excels at presenting information visually on the web, it lacks the robust analytical capabilities and data manipulation features found in spreadsheet software like Excel. By converting HTML data to Excel, users can leverage Excel's advanced functionalities like formulas, charts, tables, and macros to organize and analyze data efficiently. In this article, we will explain how to convert HTML to Excel in C# 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 HTML to Excel in C#
Spire.XLS for .NET offers the Workbook.LoadFromHtml() method to load an HTML file. After loading the HTML file, you can easily save it in Excel format using the Workbook.SaveToFile() method. The detailed steps are as follows.
- Create an object of the Workbook class.
- Load an HTML file using the Workbook.LoadFromHtml() method.
- Save the HTML file in Excel format using the Workbook.SaveToFile() method.
- C#
using Spire.Xls; namespace ConvertHtmlToExcel { internal class Program { static void Main(string[] args) { // Specify the input HTML file path string filePath = @"C:\Users\Administrator\Desktop\Sample.html"; // Create an object of the workbook class Workbook workbook = new Workbook(); // Load the HTML file workbook.LoadFromHtml(filePath); // Save the HTML file in Excel XLSX format string result = @"C:\Users\Administrator\Desktop\ToExcel.xlsx"; workbook.SaveToFile(result, ExcelVersion.Version2013); workbook.Dispose(); } } }
Insert HTML String to Excel in C#
In addition to converting HTML files to Excel, Spire.XLS for .NET also allows you to insert HTML strings into Excel cells by using the CellRange.HtmlString property. The detailed steps are as follows.
- Create an object of the Workbook class.
- Get a specific worksheet by its index (0-based) using the Workbook.Worksheets[index] property.
- Get the cell that you want to add an HTML string to using the Worksheet.Range[] property.
- Add an HTML sting to the cell using the CellRange.HtmlString property.
- Save the resulting workbook to a new file using the Workbook.SaveToFile() method.
- C#
using Spire.Xls; namespace InsertHtmlStringInExcel { internal class Program { static void Main(string[] args) { // Create an object of the workbook class Workbook workbook = new Workbook(); // Get the first sheet Worksheet sheet = workbook.Worksheets[0]; // Specify the HTML string string htmlCode = "<p><font size='12'>This is a <b>paragraph</b> with <span style='color: red;'>colored text</span>.</font></p>"; // Get the cell that you want to add the HTML string to CellRange range = sheet.Range["A1"]; // Add the HTML string to the cell range.HtmlString = htmlCode; // Auto-adjust the width of the first column based on its content sheet.AutoFitColumn(1); // Save the resulting workbook to a new file string result = @"C:\Users\Administrator\Desktop\InsertHtmlStringIntoCell.xlsx"; workbook.SaveToFile(result, ExcelVersion.Version2013); workbook.Dispose(); } } }
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 save Excel chart sheet to SVG in C#
A ChartSheet represents a chart sheet. It is a worksheet that contains only a chart. This article will demonstrate how to convert a chart sheet to SVG stream by using Spire.XLS.
Firstly, view the sample Excel worksheets with two chart sheets.
Convert all the chart sheets to SVG stream:
using System.Drawing.Imaging; using System.IO; namespace Convert { class Program { static void Main(string[] args) { //load the document from file Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx"); //call ToSVGStream(Stream stream) method to save each chart sheet to SVG stream for (int i = 0; i < workbook.Chartsheets.Count; i++) { FileStream fs = new FileStream(string.Format("chartsheet-{0}.svg", i), FileMode.Create); workbook.Chartsheets[i].ToSVGStream(fs); fs.Flush(); fs.Close(); } } } }
Effective screenshot of the two chart sheets to SVG.
using System.Drawing.Imaging; using System.IO; namespace Convert { class Program { static void Main(string[] args) { //load the document from file Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx"); //get the second chartsheet by name ChartSheet cs = workbook.GetChartSheetByName("Chart2"); //save to SVG stream FileStream fs = new FileStream(string.Format("chart2.svg"), FileMode.Create); cs.ToSVGStream(fs); fs.Flush(); fs.Close(); } } }
C#/VB.NET: Convert Charts in Excel to Images
Charts are commonly used in Microsoft Excel files to visualize numeric data. In some cases, you may need to save the charts in an Excel file as images in order to use them in other programs or other files such as PDFs and PowerPoint presentations. In this article, we will demonstrate how to convert charts in Excel to images in C# and VB.NET using Spire.XLS for .NET.
- Convert a Specific Chart in an Excel Worksheet to Image
- Convert All Charts in an Excel Worksheet to Images
- Convert a Chart Sheet to Image in Excel
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 DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Convert a Specific Chart in an Excel Worksheet to Image in C# and VB.NET
Spire.XLS provides the Workbook.SaveChartAsImage(Worksheet worksheet, int chartIndex) method which enables you to convert a specific chart in a worksheet as image. The following are the detailed steps:
- Initialize an instance of the Workbook class.
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet by its index through Workbook.Worksheets[int worksheetIndex] property.
- Save a specific chart in the worksheet as image using Workbook.SaveChartAsImage(Worksheet worksheet, int chartIndex) method.
- Save the image to a PNG file.
- C#
- VB.NET
using Spire.Xls; using System.Drawing; using System.Drawing.Imaging; namespace ConvertAExcelChartToImage { internal class Program { static void Main(string[] args) { //Initialize an instance of the Workbook class Workbook workbook = new Workbook(); //Load a sample Excel file workbook.LoadFromFile("Charts.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Save the first chart in the first worksheet as image Image image = workbook.SaveChartAsImage(sheet, 0); //Save the image to .png file image.Save(@"output\chart.png", ImageFormat.Png); } } }
Convert All Charts in an Excel Worksheet to Images in C# and VB.NET
To convert all charts in an Excel worksheet to images, you can use the Workbook.SaveChartAsImage(Worksheet worksheet) method. The following are the detailed steps:
- Initialize an instance of the Workbook class.
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet by its index through Workbook.Worksheets[int worksheetIndex] property.
- Save all charts in the worksheet as images using Workbook.SaveChartAsImage(Worksheet worksheet) method.
- Save the images to PNG files.
- C#
- VB.NET
using Spire.Xls; using System.Drawing; using System.Drawing.Imaging; namespace ConvertAllExcelChartsToImages { internal class Program { static void Main(string[] args) { //Initialize an instance of the Workbook class Workbook workbook = new Workbook(); //Load a sample Excel file workbook.LoadFromFile("Charts.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Save charts in the first worksheet as images Image[] imgs = workbook.SaveChartAsImage(sheet); //Save the images to png files for (int i = 0; i < imgs.Length; i++) { imgs[i].Save(string.Format(@"output\chart-{0}.png", i), ImageFormat.Png); } } } }
Convert a Chart Sheet to Image in Excel in C# and VB.NET
You can use the Workbook.SaveChartAsImage(ChartSheet chartSheet) method to convert a chart sheet in Excel to image. The following are the detailed steps:
- Initialize an instance of the Workbook class.
- Load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specific chart sheet by its index through Workbook.Chartsheets[int chartSheetIndex] property.
- Save the chart sheet as image using Workbook.SaveChartAsImage(ChartSheet chartSheet) method.
- Save the image to .png file.
- C#
- VB.NET
using Spire.Xls; using System.Drawing; using System.Drawing.Imaging; namespace ConvertExcelChartSheetToImage { internal class Program { static void Main(string[] args) { //Initialize an instance of the Workbook class Workbook workbook = new Workbook(); //Load a sample Excel file workbook.LoadFromFile("ChartSheet.xlsx"); //Get the first chart sheet ChartSheet chartSheet = workbook.Chartsheets[0]; //Save the first chart sheet as image Image image = workbook.SaveChartAsImage(chartSheet); //Save the image to .png file image.Save(@"output\chartSheet.png", ImageFormat.Png); } } }
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.
Convert Excel Sheet to a High-Resolution Image in C#, VB.NET
Sometimes, you may want to convert Excel sheet to a high-resolution image, especially when the Excel report contains graphs or pictures. This article will show you how to set image resolution when saving Excel sheet to JPG using Spire.XLS.
Step 1: Create a custom function that you can use to reset the image resolution.
private static Bitmap 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); Graphics g = Graphics.FromImage(bmp); g.DrawImage(mf, 0, 0); g.Dispose(); return bmp; }
Step 2: Create a workbook instance and load the sample Excel file.
Workbook workbook = new Workbook(); workbook.LoadFromFile("Chart.xlsx",ExcelVersion.Version2013);
Step 3: Get the worksheet you want to convert.
Worksheet worksheet = workbook.Worksheets[0];
Step 4: Convert the worksheet to EMF stream.
MemoryStream ms = new MemoryStream(); worksheet.ToEMFStream(ms, 1, 1, worksheet.LastRow, worksheet.LastColumn);
Step 5: Create an image from the EMF stream, and call ResetResolution to reset the resolution for the image.
Image image = Image.FromStream(ms); Bitmap images = ResetResolution(image as Metafile, 300);
Step 6: Save the image in JPG file format.
images.Save("Result.jpg", ImageFormat.Jpeg);
Output:
Full Code:
using Spire.Xls; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace Convert { class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); workbook.LoadFromFile("Chart.xlsx", ExcelVersion.Version2013); Worksheet worksheet = workbook.Worksheets[0]; using (MemoryStream ms = new MemoryStream()) { worksheet.ToEMFStream(ms, 1, 1, worksheet.LastRow, worksheet.LastColumn); Image image = Image.FromStream(ms); Bitmap images = ResetResolution(image as Metafile, 300); images.Save("Result.jpg", ImageFormat.Jpeg); } } private static Bitmap 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); Graphics g = Graphics.FromImage(bmp); g.DrawImage(mf, 0, 0); g.Dispose(); return bmp; } } }
Imports Spire.Xls Imports System.Drawing Imports System.Drawing.Imaging Imports System.IO Namespace Convert Class Program Private Shared Sub Main(args As String()) Dim workbook As New Workbook() workbook.LoadFromFile("Chart.xlsx", ExcelVersion.Version2013) Dim worksheet As Worksheet = workbook.Worksheets(0) Using ms As New MemoryStream() worksheet.ToEMFStream(ms, 1, 1, worksheet.LastRow, worksheet.LastColumn) Dim image__1 As Image = Image.FromStream(ms) Dim images As Bitmap = ResetResolution(TryCast(image__1, Metafile), 300) images.Save("Result.jpg", ImageFormat.Jpeg) End Using End Sub Private Shared Function ResetResolution(mf As Metafile, resolution As Single) As Bitmap Dim width As Integer = CInt(mf.Width * resolution / mf.HorizontalResolution) Dim height As Integer = CInt(mf.Height * resolution / mf.VerticalResolution) Dim bmp As New Bitmap(width, height) bmp.SetResolution(resolution, resolution) Dim g As Graphics = Graphics.FromImage(bmp) g.DrawImage(mf, 0, 0) g.Dispose() Return bmp End Function End Class End Namespace
C#/VB.NET: Convert CSV to PDF
CSV is one of the commonly used file formats for exchanging tabular data between applications. However, in some circumstances, CSV may not be the most appropriate file format. For instance, if you have a CSV report that needs to be sent to an important customer, the best way to ensure the file appears as-is on the customer's device is to convert it to PDF. This article will demonstrate how to convert CSV to PDF 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 CSV to PDF in C# and VB.NET
The following are the steps to convert a CSV file to PDF:
- Create an instance of Workbook class.
- Load the CSV file using Workbook.LoadFromFile(filePath, separator) method.
- Set the Workbook.ConverterSetting.SheetFitToPage property as true to ensure the worksheet is rendered to one PDF page.
- Get the first worksheet in the Workbook using Workbook.Worksheets[0] property.
- Loop through the columns in the worksheet and auto-fit the width of each column using Worksheet.AutoFitColumn() method.
- Save the worksheet to PDF using Worksheet.SaveToPdf() method.
- C#
- VB.NET
using Spire.Xls; namespace ConvertCsvToPdf { class Program { static void Main(string[] args) { //Create a Workbook instance Workbook wb = new Workbook(); //Load a CSV file wb.LoadFromFile("Sample.csv", ","); //Set SheetFitToPage property as true to ensure the worksheet is converted to 1 PDF page wb.ConverterSetting.SheetFitToPage = true; //Get the first worksheet Worksheet sheet = wb.Worksheets[0]; //Loop through the columns in the worksheet for (int i = 1; i < sheet.Columns.Length; i++) { //AutoFit columns sheet.AutoFitColumn(i); } //Save the worksheet to PDF sheet.SaveToPdf("toPDF.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 Excel to SVG
SVG, short for Scalable Vector Graphics, is a web-friendly vector image format. SVG has many advantages over other image formats. One of the most significant advantages is resolution independence, which means you can resize SVG images as needed without losing image quality. Sometimes, you may need to convert Excel files to SVG for web viewing. This article will demonstrate how to programmatically convert Excel to SVG 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 an Excel Worksheet to SVG using C# and VB.NET
Spire.XLS provides the Worksheet.SaveToSVGStream() method to convert an Excel worksheet to SVG. The detailed steps are as follows:
- Initialize an instance of the Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet by its index through Workbook.Worksheets[int] property.
- Initialize an instance of the FileStream class.
- Save the worksheet to SVG using Worksheet.ToSVGStream(Stream, int, int, int, int) method.
- C#
- VB.NET
using Spire.Xls; using System.IO; namespace ConvertWorksheetToSVG { class Program { static void Main(string[] args) { //Create an instance of Workbook class Workbook workbook = new Workbook(); //Load an Excel file workbook.LoadFromFile("Sample1.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Create an instance of FileStream class FileStream fs = new FileStream("E:\\Program Files\\WorksheetToSVG.svg", FileMode.Create); //Save the worksheet to SVG sheet.ToSVGStream(fs, 0, 0, 0, 0); fs.Flush(); fs.Close(); } } }
Convert an Excel Chart Sheet to SVG using C# and VB.NET
A chart sheet is a worksheet that only contains a chart. Spire.XLS allows you to convert a chart sheet to SVG by using the ChartSheet.ToSVGStream() method. The detailed steps are as follows:
- Initialize an instance of the Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific chart sheet by its index through Workbook.Chartsheets[int] property.
- Initialize an instance of the FileStream class.
- Save the chart sheet to SVG using ChartSheet.ToSVGStream(Stream) method.
- C#
- VB.NET
using Spire.Xls; using System.IO; namespace ConvertChartSheetToSVG { class Program { static void Main(string[] args) { //Create an instance of Workbook class Workbook workbook = new Workbook(); //Load an Excel file workbook.LoadFromFile("Sample2.xlsx"); //Get the first chart sheet ChartSheet chartSheet = workbook.Chartsheets[0]; //Create an instance of FileStream class FileStream fs = new FileStream("E:\\ProgramFiles\\ChartSheetToSVG.svg", FileMode.Create); //Save the chart sheet to SVG chartSheet.ToSVGStream(fs); fs.Flush(); fs.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.
Convert Excel to OpenDocument Spreadsheet (.ods) format in C#, VB.NET
Simple introduction about OpenDocument Spreadsheet format
The OpenDocument Spreadsheet format (commonly referred to as ODS format) is an XML-based file format for editable spreadsheet documents defined by OASIS. This free ISO-standardized format is now widely supported by a variety of free software applications, such as OpenOffice.org and the LibreOffice suite.
Using C#/VB.NET to convert Excel to .ods format via Spire.XLS
Spire.XLS, a powerful .NET excel component, enables developers to convert excel documents to ods format easily. This part will introduce you the solution of converting excel to .ods format in c# and vb.net.
Now, please view the following screenshot of the original excel document in .xlsx format:
Then refer to the following steps:
Step 1: Initialize a new instance of Workbook class and load the excel document from file.
Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx");
Step 2: Invoke Workbook.SaveToFile(string fileName, FileFormat fileFormat) method to save the excel document to ODS format.
workbook.SaveToFile("Result.ods",FileFormat.ODS);
Run the project, we will get the following result file in .ods format:
Full codes:
using Spire.Xls; namespace Convert_Excel_to_ODS { class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx"); workbook.SaveToFile("Result.ods",FileFormat.ODS); } } }
Imports Spire.Xls Namespace Convert_Excel_to_ODS Class Program Private Shared Sub Main(args As String()) Dim workbook As New Workbook() workbook.LoadFromFile("Sample.xlsx") workbook.SaveToFile("Result.ods", FileFormat.ODS) End Sub End Class End Namespace=
C#/VB.NET: Convert Excel Data to Word Tables with Formatting
If you're creating a written report on a company's monthly expenditures, you might need to include a spreadsheet to show the financial figures and make them easier to read. This article demonstrates how to convert Excel data into a Word table maintaining the formatting in C# and VB.NET using Spire.Office for .NET.
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
Export Excel Data to a Word Table with Formatting
Below are the steps to convert Excel data to a Word table and keep the formatting using Spire.Office for .NET.
- Create a Workbook object and load a sample Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets[index] property.
- Create a Document object, and add a section to it.
- Add a table using Section.AddTable() method.
- Detect the merged cells in the worksheet and merge the corresponding cells of the Word tale using the custom method MergeCells().
- Get value of a specific Excel cell through CellRange.Value property and add it to a cell of the Word table using TableCell.AddParagraph().AppendText() method.
- Copy the font style and cell style from Excel to the Word table using the custom method CopyStyle().
- Save the document to a Word file using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; using Spire.Xls; namespace ConvertExcelToWord { internal class Program { static void Main(string[] args) { //Load an Excel file Workbook workbook = new Workbook(); workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.xlsx"); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Create a Word document Document doc = new Document(); Section section = doc.AddSection(); section.PageSetup.Orientation = PageOrientation.Landscape; //Add a table Table table = section.AddTable(true); table.ResetCells(sheet.LastRow, sheet.LastColumn); //Merge cells MergeCells(sheet, table); for (int r = 1; r <= sheet.LastRow; r++) { //Set row Height table.Rows[r - 1].Height = (float)sheet.Rows[r - 1].RowHeight; for (int c = 1; c <= sheet.LastColumn; c++) { CellRange xCell = sheet.Range[r, c]; TableCell wCell = table.Rows[r - 1].Cells[c - 1]; //Export data from Excel to Word table TextRange textRange = wCell.AddParagraph().AppendText(xCell.NumberText); //Copy font and cell style from Excel to Word CopyStyle(textRange, xCell, wCell); } } //Save the document to a Word file doc.SaveToFile("ExportToWord.docx", Spire.Doc.FileFormat.Docx); } //Merge cells if any private static void MergeCells(Worksheet sheet, Table table) { if (sheet.HasMergedCells) { //Get merged cell ranges from Excel CellRange[] ranges = sheet.MergedCells; //Merge corresponding cells in Word table for (int i = 0; i < ranges.Length; i++) { int startRow = ranges[i].Row; int startColumn = ranges[i].Column; int rowCount = ranges[i].RowCount; int columnCount = ranges[i].ColumnCount; if (rowCount > 1 && columnCount > 1) { for (int j = startRow; j <= startRow + rowCount; j++) { table.ApplyHorizontalMerge(j - 1, startColumn - 1, startColumn - 1 + columnCount - 1); } table.ApplyVerticalMerge(startColumn - 1, startRow - 1, startRow - 1 + rowCount -1); } if (rowCount > 1 && columnCount == 1) { table.ApplyVerticalMerge(startColumn - 1, startRow - 1, startRow - 1 + rowCount -1); } if (columnCount > 1 && rowCount == 1) { table.ApplyHorizontalMerge(startRow - 1, startColumn - 1, startColumn - 1 + columnCount - 1); } } } } //Copy cell style of Excel to Word table private static void CopyStyle(TextRange wTextRange, CellRange xCell, TableCell wCell) { //Copy font style wTextRange.CharacterFormat.TextColor = xCell.Style.Font.Color; wTextRange.CharacterFormat.FontSize = (float)xCell.Style.Font.Size; wTextRange.CharacterFormat.FontName = xCell.Style.Font.FontName; wTextRange.CharacterFormat.Bold = xCell.Style.Font.IsBold; wTextRange.CharacterFormat.Italic = xCell.Style.Font.IsItalic; //Copy backcolor wCell.CellFormat.BackColor = xCell.Style.Color; //Copy horizontal alignment switch (xCell.HorizontalAlignment) { case HorizontalAlignType.Left: wTextRange.OwnerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Left; break; case HorizontalAlignType.Center: wTextRange.OwnerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center; break; case HorizontalAlignType.Right: wTextRange.OwnerParagraph.Format.HorizontalAlignment = HorizontalAlignment.Right; break; } //Copy vertical alignment switch (xCell.VerticalAlignment) { case VerticalAlignType.Bottom: wCell.CellFormat.VerticalAlignment = VerticalAlignment.Bottom; break; case VerticalAlignType.Center: wCell.CellFormat.VerticalAlignment = VerticalAlignment.Middle; break; case VerticalAlignType.Top: wCell.CellFormat.VerticalAlignment = VerticalAlignment.Top; break; } } } }
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.
Convert worksheet to PDF in C#, VB.NET
We have discussed before about converting workbook to PDF. However, in this section, we will show you a neat solution to convert the specific worksheet to PDF with C# and VB.NET in workbook. Apply Spire.Xls for .NET in your application and you can turn worksheet into PDF easily without changing the layout of worksheet.
In the following sections, we will demonstrate how to convert worksheet to PDF.
Step 1: Initialize a new instance of Workbook class and load the sample Excel file.
Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx");
Step 2: Get its first worksheet.
Worksheet sheet = workbook.Worksheets[0];
Step 3: Convert the selected worksheet to PDF and save to file.
sheet.SaveToPdf("toPDF.pdf");
Step 4: Launch the file.
System.Diagnostics.Process.Start("toPDF.pdf");
Effective screenshot:
Full codes:
using Spire.Xls; namespace Excel_Worksheet_to_PDF { class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); workbook.LoadFromFile("Sample.xlsx"); Worksheet sheet = workbook.Worksheets[0]; sheet.SaveToPdf("toPDF.pdf"); System.Diagnostics.Process.Start("toPDF.pdf"); } } }
Imports Spire.Xls Namespace Excel_Worksheet_to_PDF Class Program Private Shared Sub Main(args As String()) Dim workbook As New Workbook() workbook.LoadFromFile("Sample.xlsx") Dim sheet As Worksheet = workbook.Worksheets(0) sheet.SaveToPdf("toPDF.pdf") System.Diagnostics.Process.Start("toPDF.pdf") End Sub End Class End Namespace