Conversion
Upload
Click here to browse files.Convert to
- Demo
- Java
- C# source
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.core.spreadsheet.HTMLOptions;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ConvertDemo {
public void convertDemo(String filePath, String convertTo, String resultFileName) throws IOException {
Workbook workbook = new Workbook();
workbook.loadFromFile(filePath);
ConvertFormat(workbook,convertTo,resultFileName);
}
private void ConvertFormat(Workbook workbook, String convertTo, String resultFileName) throws IOException {
switch (convertTo){
case "PDF":
workbook.getConverterSetting().setSheetFitToPage(true);
workbook.saveToFile(resultFileName + ".pdf", FileFormat.PDF);
break;
case "IMAGE":
BufferedImage[] images = (BufferedImage[]) new Image[workbook.getWorksheets().size()];
for (int i = 0; i < workbook.getWorksheets().size();i++){
images[i] = workbook.saveAsImage(i,300,300);
}
if (images != null && images.length > 0){
if (images.length == 1){
ImageIO.write(images[0],".PNG", new File(resultFileName+".png"));
}
}else {
for (int j = 0; j < images.length;j++){
String fileName = String.format("image-{0}.png",j);
ImageIO.write(images[j],".PNG",new File(fileName));
}
}
break;
case "HTML":
for (int i = 0; i < workbook.getWorksheets().size(); i++) {
HTMLOptions options = new HTMLOptions();
options.setImageEmbedded(true);
String htmlPath = String.format(resultFileName+"-{0}.html",i++);
workbook.getWorksheets().get(i).saveToHtml(htmlPath,options);
}
break;
case "TIFF":
workbook.saveToTiff(resultFileName+".tiff");
break;
case "XPS":
workbook.saveToFile(resultFileName+".xps",FileFormat.XPS);
break;
}
}
}
Prevent Page Breaks in Word Tables in C#
All too often, we start a table near the bottom of a page. Microsoft Word automatically places a page break in a table if a single row or several rows are too long to fit between the top and bottom margins of the page. So we'd rather the same row can be placed on one page or have the entire table on the next page instead of being broken over two pages. In this article, I'll introduce you two ways to avoid page breaks in Word tables via Spire.Doc.
Assume that we have a Word table like this (Row 2 splits to different pages), we may want to optimize the layout by the following two methods.

Method 1: Keep the entire table on the same page.
Step 1: Create a new Word document and load the test file.
Document doc = new Document("Test.docx");
Step 2: Get the table from Word document.
Table table = doc.Sections[0].Tables[0] as Table;
Step 3: Change the paragraph setting to keep them together.
foreach (TableRow row in table.Rows)
{
foreach (TableCell cell in row.Cells)
{
foreach (Paragraph p in cell.Paragraphs)
{
p.Format.KeepFollow = true;
}
}
}
Step 4: Save and launch the file.
doc.SaveToFile("Result.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("Result.docx");
Output:

Use this sentence to replace Step 3, you'll get the layout as below:

Full C# Code:
Method 1
using Spire.Doc;
using Spire.Doc.Documents;
namespace PreventPageBreak
{
class Program
{
static void Main(string[] args)
{
Document doc = new Document("Test.docx");
Table table = doc.Sections[0].Tables[0] as Table;
foreach (TableRow row in table.Rows)
{
foreach (TableCell cell in row.Cells)
{
foreach (Paragraph p in cell.Paragraphs)
{
p.Format.KeepFollow = true;
}
}
}
doc.SaveToFile("Result.docx", FileFormat.Docx2010);
System.Diagnostics.Process.Start("Result.docx");
}
}
}
Remove borderline of textbox in Excel chart in C#, VB.NET
There is an article in the tutorials which demonstrates how to insert textbox with contents in Excel. Sometime back, a user of Spire.XLS wanted to know if it is possible to remove the borderline of the textbox that has been inserted in Excel chart. Yes, of course. This article focuses on delivering a solution to this issue.
In the following section, we're going to create two textboxes in the same chart, one textbox is built with borderline, the other one without. Then we can learn how to remove borderline using Spire.XLS by comparison.
Code snippet for remove borderline of textbox:
Step 1: Create a new instance of workbook.
Workbook workbook = new Workbook(); workbook.Version=ExcelVersion.Version2010;
Step 2: Create a new worksheet named "Remove Borderline" and add a chart to the worksheet.
Worksheet sheet = workbook.Worksheets[0]; sheet.Name = "Remove Borderline"; Chart chart = sheet.Charts.Add();
Step 3: Create textbox1 in the chart and input text information.
XlsTextBoxShape textbox1 = chart.TextBoxes.AddTextBox(50, 50, 100, 500) as XlsTextBoxShape;
Step 4: Create textbox2 in the chart, input text information and remove borderline.
XlsTextBoxShape textbox = chart.TextBoxes.AddTextBox(500, 50, 100, 500) as XlsTextBoxShape; textbox.Text = "The solution without borderline"; textbox.Line.Weight = 0;
Step 5: Save and launch the file.
workbook.SaveToFile("Sample.xlsx", ExcelVersion.Version2010);
Process.Start("Sample.xlsx");
Result:

Full code:
using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.Shapes;
using System.Diagnostics;
namespace RemoveBorderlineofTextbox
{
class Program
{
static void Main(string[] args)
{
// Create a new workbook
Workbook workbook = new Workbook();
workbook.Version = ExcelVersion.Version2010;
// Get the first worksheet and rename it
Worksheet sheet = workbook.Worksheets[0];
sheet.Name = "Remove Borderline";
// Add a chart to the worksheet
Chart chart = sheet.Charts.Add();
// Add the first text box (with visible border)
XlsTextBoxShape textbox1 = chart.TextBoxes.AddTextBox(50, 50, 100, 500) as XlsTextBoxShape ;
textbox1.Text = "The original with borderline";
// Add the second text box (border will be hidden)
XlsTextBoxShape textbox2 = chart.TextBoxes.AddTextBox(500, 50, 100, 500) as XlsTextBoxShape;
textbox2.Text = "The solution without borderline";
// Hide the border by setting line weight to 0
textbox2.Line.Weight = 0;
// Save the result file
workbook.SaveToFile("Sample.xlsx", ExcelVersion.Version2010);
Process.Start("Sample.xlsx");
}
}
}
Imports Spire.Xls
Imports Spire.Xls.Core.Spreadsheet.Shapes
Imports System.Diagnostics
Namespace RemoveBorderlineofTextbox
Class Program
Private Shared Sub Main(args As String())
'Create a new workbook
Dim workbook As New Workbook()
workbook.Version = ExcelVersion.Version2010
' Get the first worksheet And rename it
Dim sheet As Worksheet = workbook.Worksheets(0)
sheet.Name = "Remove Borderline"
' Add a chart to the worksheet
Dim chart As Chart = sheet.Charts.Add()
' Add the first text box (with visible border)
Dim textbox1 As XlsTextBoxShape = TryCast(chart.TextBoxes.AddTextBox(50, 50, 100, 500), XlsTextBoxShape)
textbox1.Text = "The original with borderline"
'Add the second text box (border will be hidden)
Dim textbox2 As XlsTextBoxShape = TryCast(chart.TextBoxes.AddTextBox(500, 50, 100, 500), XlsTextBoxShape)
textbox2.Text = "The solution without borderline"
'Hide the border by setting line weight to 0
textbox2.Line.Weight = 0
'Save the result file
workbook.SaveToFile("Sample.xlsx", ExcelVersion.Version2010)
Process.Start("Sample.xlsx")
End Class
End Namespace
How to Expand/Collapse the rows in an existing Pivot Table in C#
Pivot table displays the data in sort, count total or give the average of the data stored in one table or spreadsheet. So it gives readers clear information of the data's trends and patterns rather than a large amount of similar data. Sometimes, there are so many rows in one pivot table and we may need to expand or collapse them to make the pivot table more clearly.
By using Spire.XLS for .NET, developers can create pivot table. This article will show you how to expand and collapse the rows in an existing Pivot table in C#.
Firstly, make sure that Spire.XLS for .NET (version7.5.5 or above) has been installed on your machine. And then, adds Spire.XLS.dll as reference in the downloaded Bin folder thought the below path: "..\Spire.XLS\Bin\NET4.0\ Spire.XLS.dll".
//Create a new excel document
Workbook book = new Workbook();
//load an excel document with Pivot table from the file
book.LoadFromFile("test.xlsx");
//Find the Pivot Table sheet
Worksheet sheet = book.Worksheets["Pivot Table"];
//Get the data in Pivot Table
Spire.Xls.Core.Spreadsheet.PivotTables.XlsPivotTable pivotTable = sheet.PivotTables[0] as Spire.Xls.Core.Spreadsheet.PivotTables.XlsPivotTable;
//Calculate Data
pivotTable.CalculateData();
//Collapse the rows
(pivotTable.PivotFields["Vendor No"] as Spire.Xls.Core.Spreadsheet.PivotTables.XlsPivotField).HideItemDetail("1501", true);
//Expand the rows
(pivotTable.PivotFields["Vendor No"] as Spire.Xls.Core.Spreadsheet.PivotTables.XlsPivotField).HideItemDetail("1502", false);
//Save the document to file
book.SaveToFile("result.xlsx", ExcelVersion.Version2007);
Effective screenshots:
Collapse the rows in Pivot table in C#

Expand the rows in Pivot table in C#

Full codes:
using Spire.Xls;
namespace HighlightValues
{
class Program
{
static void Main(string[] args)
{
Workbook book = new Workbook();
book.LoadFromFile("test.xlsx");
Worksheet sheet = book.Worksheets["Pivot Table"];
Spire.Xls.Core.Spreadsheet.PivotTables.XlsPivotTable pivotTable = sheet.PivotTables[0] as Spire.Xls.Core.Spreadsheet.PivotTables.XlsPivotTable;
pivotTable.CalculateData();
(pivotTable.PivotFields["Vendor No"] as Spire.Xls.Core.Spreadsheet.PivotTables.XlsPivotField).HideItemDetail("1501", true);
(pivotTable.PivotFields["Vendor No"] as Spire.Xls.Core.Spreadsheet.PivotTables.XlsPivotField).HideItemDetail("1502", true);
book.SaveToFile("result_1.xlsx", ExcelVersion.Version2007);
(pivotTable.PivotFields["Vendor No"] as Spire.Xls.Core.Spreadsheet.PivotTables.XlsPivotField).HideItemDetail("1502", false);
book.SaveToFile("result_2.xlsx", ExcelVersion.Version2007);
}
}
}
Remove Table from PowerPoint document
Spire.Presentation is a powerful and easy-to-use .NET component, especially designed for developers. Using Spire.Presentation you can generate, modify, convert, render, and print documents without installing Microsoft PowerPoint on your machine. There is a document in our website introducing you how to insert table. And in this document, I will introduce you how to remove tables within a PPT document.
Step 1: Create Presentation instance and load file.
Presentation presentation = new Presentation();
presentation.LoadFromFile("sample.ppt");
Step 2: Get the tables within the PPT document.
List<IShape> shape_tems = new List<IShape>();
foreach (IShape shape in presentation.Slides[0].Shapes)
{
if (shape is ITable)
{
//add new table to table list
shape_tems.Add(shape);
}
}
Step 3: Remove all tables.
foreach (IShape shape in shape_tems)
{
presentation.Slides[0].Shapes.Remove(shape);
}
Step 4: Save the document.
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
Download and install Spire.Presentation for .NET and refer to below code to remove tables within PPT document.
Screenshots:
Before:

After:

Full Code:
//create Presentation instance and load file
Presentation presentation = new Presentation();
presentation.LoadFromFile("sample.ppt");
//get the tables in PowerPoint document
List<IShape> shape_tems = new List<IShape>();
foreach (IShape shape in presentation.Slides[0].Shapes)
{
if (shape is ITable)
{
//add new table to table list
shape_tems.Add(shape);
}
}
//remove all tables
foreach (IShape shape in shape_tems)
{
presentation.Slides[0].Shapes.Remove(shape);
}
//save the document
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("result.pptx");
'create Presentation instance and load file
Dim presentation As New Presentation()
presentation.LoadFromFile("sample.ppt")
'get the tables in PowerPoint document
Dim shape_tems As New List(Of IShape)()
For Each shape As IShape In presentation.Slides(0).Shapes
If TypeOf shape Is ITable Then
'add new table to table list
shape_tems.Add(shape)
End If
Next
'remove all tables
For Each shape As IShape In shape_tems
presentation.Slides(0).Shapes.Remove(shape)
Next
'save the document
presentation.SaveToFile("result.pptx", FileFormat.Pptx2010)
System.Diagnostics.Process.Start("result.pptx")
If you couldn't successfully use Spire.Presentation, please refer Spire.Presentation Quick Start which can guide you quickly use Spire.Presentation.
MailMerge
| Agreement Start Date: | |
| Agreement End Date: | |
| Agreement Extension Date: | |
| Documentation Start Date: | |
| Documentation End Date: | |
|
downloads
|
|
- Demo
- Java
- C# source
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
public class MailMargeDemo {
public void mailMerge(String docFile, String resultFilePath) throws Exception {
Document doc = new Document();
doc.loadFromFile(docFile);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar=Calendar.getInstance();
ArrayList dateList=new ArrayList<>();
int []offset={-5,5,6,-2,2};
for(int data:offset){
calendar.setTime(new Date());
calendar.add(Calendar.YEAR,data);
dateList.add(format.format(calendar.getTime()));
}
String[] values =new String[dateList.size()];
dateList.toArray(values);
String []fields={
"SubGrantPAStartDateValue",
"SubGrantPAEndDateValue",
"SubGrantPAExtensionDateValue",
"SubGrantPSStartDateValue",
"SubGrantPSEndDateValue"
};
doc.getMailMerge().execute(fields,values);
doc.saveToFile(resultFilePath, FileFormat.Docx);
}
}
Table
Data
| Name | Capital | Continent | Area | Population | Flag |
| Argentina | Buenos Aires | South America | 2777815 | 32300003 | |
| Bolivia | La Paz | South America | 1098575 | 7300000 | |
| Brazil | Brasilia | South America | 8511196 | 150400000 | |
| Canada | Ottawa | North America | 9976147 | 26500000 | |
| Chile | Santiago | South America | 756943 | 13200000 | |
| Colombia | Bagota | South America | 1138907 | 33000000 | |
| Cuba | Havana | North America | 114524 | 10600000 | |
| Ecuador | Quito | South America | 455502 | 10600000 | |
| El Salvador | San Salvador | North America | 20865 | 5300000 | |
| Guyana | Georgetown | South America | 214969 | 800000 |
Option
- Demo
- Java
- C# source
FindAndHighlight
Upload
Click here to browse files.Convert to
- Demo
- Java
- C# source
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;
import java.awt.*;
public class FindHighlightDemo {
public void findHeighlight(String docFile, String findText, String resultFilePath){
Document doc = new Document();
doc.loadFromFile(docFile);
TextSelection[] textSelections = doc.findAllString(findText, false, true);
if(textSelections!=null){
for (TextSelection selection : textSelections) {
selection.getAsOneRange().getCharacterFormat().setHighlightColor(Color.YELLOW);
}
}
doc.saveToFile(resultFilePath, FileFormat.Docx);
}
}
Conversion
Upload
Click here to browse files.Convert to
- Demo
- Java
- C# source