page 121

This article demonstrates how to add Trendline to an Excel chart and read the equation of the Trendline using Spire.XLS for Java.

Add Trendline

import com.spire.xls.*;
import com.spire.xls.core.IChartTrendLine;

import java.awt.*;

public class AddTrendline {
    public static void main(String[] args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load the Excel file
        workbook.loadFromFile("test.xlsx");

        //Get the first chart in the first worksheet
        Chart chart = workbook.getWorksheets().get(0).getCharts().get(0);

        //Add a Trendline to the first series of the chart
        IChartTrendLine trendLine = chart.getSeries().get(0).getTrendLines().add(TrendLineType.Linear);

        //Set Trendline name
        trendLine.setName("Linear(Series1)");
        //Set line type and color
        trendLine.getBorder().setPattern(ChartLinePatternType.DashDot);
        trendLine.getBorder().setColor(Color.blue);
        //Set forward and backward value
        trendLine.setForward(0.5);
        trendLine.setBackward(0.5);
        //Set intercept value
        trendLine.setIntercept(5);

        //Display equation on chart
        trendLine.setDisplayEquation(true);
        //Display R-Squared value on chart
        trendLine.setDisplayRSquared(true);

        //Save the result file
        workbook.saveToFile("AddTrendline.xlsx", ExcelVersion.Version2013);
    }
}

Add Trendline to Chart and Read Trendline Equation in Excel in Java

Read Trendline equation

import com.spire.xls.Chart;
import com.spire.xls.Workbook;
import com.spire.xls.core.IChartTrendLine;

public class ReadEquationOfTrendline {
    public static void main(String[] args){
        //Create a Workbook instance
        Workbook workbook = new Workbook();
        //Load the Excel file
        workbook.loadFromFile("AddTrendline.xlsx");

        //Get the first chart in the first worksheet
        Chart chart = workbook.getWorksheets().get(0).getCharts().get(0);

        //Read the equation of the first series of the chart
        IChartTrendLine trendLine = chart.getSeries().get(0).getTrendLines().get(0);
        String equation = trendLine.getFormula();
        System.out.println("The equation is: " + equation);
    }
}

Add Trendline to Chart and Read Trendline Equation in Excel in Java

Spire.Doc for Java supports embedding an external file (Word, Excel, PowerPoint, PDF, picture, video, etc.) in Word documents as an OLE object. This article gives you an example of how to insert a PDF file into a Word document.

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.OleObjectType;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.DocOleObject;
import com.spire.doc.fields.DocPicture;

public class InsertOLE {

    public static void main(String[] args) {

        //Create a Document object and load a Word document
        Document doc = new Document();
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\source.docx");

        //Get the last section
        Section section = doc.getLastSection();

        //Add a paragraph
        Paragraph par = section.addParagraph();

        //Load an image which will be inserted to Word document representing the embedded file
        DocPicture pdfIcon = new DocPicture(doc);
        pdfIcon.loadImage("C:\\Users\\Administrator\\Desktop\\pdf-icon.jpg");

        //Insert a PDF file to the Word document as an OLE object
        par.appendOleObject("C:\\Users\\Administrator\\Desktop\\report.pdf", pdfIcon, OleObjectType.Adobe_Acrobat_Document);

        //Save to another file
        doc.saveToFile("EmbedDocument.docx", FileFormat.Docx_2013);
    }
}

Insert an External File into Word Documents in Java

When sending an Excel document to others for review, it is recommended to turn on the Track Changes to ensure that all changes made to the worksheet or workbook are recorded. For the altered cells in Excel, each one will be highlighted with a blue triangle in the upper left corner of the cell. You can then view the changes and decide whether to accept or reject them. This article will demonstrate how to programmatically accept or reject all tracked changes in an Excel workbook 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

Accept All Tracked Changes in a Workbook

To accept tracked changes in a workbook, you'll first need to determine whether the workbook has tracked changes using Workbook.HasTrackedChanges property. If yes, you can then accept all changes at once using Workbook.AcceptAllTrackedChanges() method. The following are the steps to accept all tracked changes in an Excel workbook.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook.LoadFromFile() method.
  • Determine if the workbook has tracked changes using Workbook.HasTrackedChanges property.
  • Accept all tracked changes in the workbook using Workbook.AcceptAllTrackedChanges() method.
  • Save the result document using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace AcceptTrackedChanges
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook workbook = new Workbook();

            //Load a sample Excel document
            workbook.LoadFromFile("Sample.xlsx");

            //Determine if the workbook has tracked changes
            if (workbook.HasTrackedChanges)
            {

                //Accept all tracked changes in the workbook
                workbook.AcceptAllTrackedChanges();
            }

            //Save the result document
            workbook.SaveToFile("AcceptChanges.xlsx", FileFormat.Version2013);
        }
    }
}

C#/VB.NET: Accept or Reject Tracked Changes in Excel

Reject All Tracked Changes in a Workbook

If the tracked changes have been proven to exist in a workbook, Spire.XLS for .NET also provides the Workbook.RejectAllTrackedChanges() method to reject all tracked changes at once. The detailed steps are as follows.

  • Create a Workbook object.
  • Load a sample Excel document using Workbook.LoadFromFile() method.
  • Determine if the workbook has tracked changes using Workbook.HasTrackedChanges property.
  • Reject all tracked changes in the workbook using Workbook.RejectAllTrackedChanges() method.
  • Save the result document using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace AcceptTrackedChanges
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook object
            Workbook workbook = new Workbook();

            //Load a sample Excel document
            workbook.LoadFromFile("Sample.xlsx");

            //Determine if the workbook has tracked changes
            if (workbook.HasTrackedChanges)
            {

                //Reject all tracked changes in the workbook
                workbook.RejectAllTrackedChanges();
            }

            //Save the result document
            workbook.SaveToFile("RejectChanges.xlsx", FileFormat.Version2013);
        }
    }
}

C#/VB.NET: Accept or Reject Tracked Changes in 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.

page 121