page 104

Java: Convert Word to EPUB

2021-11-26 06:18:43 Written by Koohji

EPUB (short for electronic publication) is a popular file format for e-books. EPUB files can be read on numerous e-readers and most smartphones, tablets and computers. In some circumstances, you might need to convert your Word document to EPUB file format to make it readable on various devices. This article will show you how to achieve this task programmatically in Java using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>14.1.3</version>
    </dependency>
</dependencies>

Convert Word to EPUB

The following are the steps to convert a Word document to EPUB file format:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Save the Word document to EPUB using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class ConvertWordToEpub {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load a Word document
        doc.loadFromFile("Sample.docx");

        //Save the Word document to EPUB format
        doc.saveToFile("ToEpub.epub", FileFormat.E_Pub);
    }
}

Java: Convert Word to EPUB

Convert Word to EPUB with a Cover Image

The following are the steps to convert a Word document to EPUB with a cover image:

  • Create a Document instance.
  • Load a Word document using Document.loadFromFile() method.
  • Create a DocPicture instance.
  • Load an image using DocPicture.loadImage() method.
  • Save the Word document to EPUB with cover image using Document.saveToEpub(String, DocPicture) method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.fields.DocPicture;

public class ConvertWordToEpubWithCoverImage {
    public static void main(String[] args){
        //Create a Document instance
        Document doc = new Document();
        //Load a Word document
        doc.loadFromFile("Sample.docx");

        //Create a DocPicture instance
        DocPicture picture = new DocPicture(doc);
        //Load an image
        picture.loadImage("Cover.png");

        //Save the Word document to EPUB with cover image
        doc.saveToEpub("ToEpubWithCoverImage.epub", picture);
    }
}

Java: Convert Word to EPUB

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.

As its name suggests, a Comma Separated Values (CSV) file is a plain text file containing only numbers and letters, usually separated by commas. It can be used for exchanging data between applications. This article demonstrates how to convert Excel to CSV and convert CSV to Excel using Spire.XLS for Java.

Install Spire.XLS for Java

First of all, you're required to add the Spire.Xls.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.xls</artifactId>
        <version>15.12.15</version>
    </dependency>
</dependencies>

Convert Excel to CSV

Spire.XLS for Java supports converting Excel to CSV with only several lines of codes. To get started, follow these steps.

  • Create a Workbook instance.
  • Load a sample Excel document using Workbook.loadFromFile() method.
  • Get a specific worksheet of the document using Workbook.getWorksheets().get() method.
  • Save the worksheet to CSV using Worksheet.saveToFile() method.
  • Java
import com.spire.xls.*;
import java.nio.charset.Charset;

public class ExcelToCSV {
    public static void main(String[] args) {

        //Create a workbook
        Workbook workbook = new Workbook();

        //Load a sample excel file
        workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\sample.xlsx");

        //Calculate formulas if any 
        workbook.calculateAllValue();

        //Get the first sheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Save the document to CSV
        sheet.saveToFile("output/ToCSV_out.csv", ",", Charset.forName("UTF-8"));
    }
}

Java: Convert Excel to CSV and Vice Versa

Convert CSV to Excel

The following are detailed steps to convert CSV to Excel.

  • Create a Workbook instance and load a sample CSV file using Workbook.loadFromFile() method.
  • Get a specific worksheet using Workbook.getWorksheets().get() method.
  • Specify the cell range using Worksheet.getCellRange() method and ignore errors when setting numbers in the cells as text using CellRange.setIgnoreErrorOptions (java.util.EnumSet ignoreErrorOptions) method.
  • Automatically adjust the height of rows and width of columns using methods provided by CellRange class.
  • Save the document to an XLSX file using Workbook.saveToFile() method.
  • Java
import com.spire.xls.*;
import java.util.EnumSet;

public class CSVToExcel {
    public static void main(String[] args) {
        //Create a workbook
        Workbook workbook = new Workbook();
        //Load a sample CSV file
        workbook.loadFromFile("C:\\Users\\Test1\\Desktop\\test.csv", ",", 1, 1);

        //Get the first worksheet
        Worksheet sheet = workbook.getWorksheets().get(0);

        //Specify the cell range and ignore errors when setting numbers in the cells as text
        sheet.getCellRange("A1:D6").setIgnoreErrorOptions(EnumSet.of(IgnoreErrorType.NumberAsText));

        //Automatically adjust the height of the rows and width of the columns
        sheet.getAllocatedRange().autoFitColumns();
        sheet.getAllocatedRange().autoFitRows();

        //Save the document to an XLSX file
        workbook.saveToFile("output/CSVToExcel_out.xlsx", ExcelVersion.Version2013);
    }
}

Java: Convert Excel to CSV and Vice Versa

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.

Java: Preform Mail Merge with a Region

2021-11-25 08:55:12 Written by Koohji

When you run mail merge with a region, all merge fields within the region are repeated for each record in the data source. This is useful when you want to dynamically add rows to a Word table. In this article, you will learn how to perform mail merge with a region using Spire.Doc for Java.

Install Spire.Doc for Java

First of all, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>14.1.3</version>
    </dependency>
</dependencies>

Create a Template

To create a mail merge region, you need to specify the start point and the end point of the region. For example, the following Word template contains the region "Country" which is marked by «TableStart:Country» and «TableEnd:Country». Mail Merge will repeat that region for each record in the data source.

Java: Preform Mail Merge with a Region

The following is the sample XML file that will be used as the data source.

  • Package Manager
<?xml version="1.0" encoding="UTF-8"?>
<Data>
	<Country>
		<Capital>Buenos Aires</Capital>
		<Name>Argentina</Name>
		<Continent>South America</Continent>
		<Area>2777815</Area>
		<Population>32300003</Population>
	</Country>
	<Country>
		<Capital>La Paz</Capital>
		<Name>Bolivia</Name>
		<Continent>South America</Continent>
		<Area>1098575</Area>
		<Population>7300000</Population>
	</Country>
	<Country>
		<Capital>Brasilia</Capital>
		<Name>Brazil</Name>
		<Continent>South America</Continent>
		<Area>8511196</Area>
		<Population>150400000</Population>
	</Country>	
	<Country>
		<Capital>Buenos Aires</Capital>
		<Name>Argentina</Name>
		<Continent>South America</Continent>
		<Area>2777815</Area>
		<Population>32300003</Population>
	</Country>
	<Country>
		<Capital>La Paz</Capital>
		<Name>Bolivia</Name>
		<Continent>South America</Continent>
		<Area>1098575</Area>
		<Population>7300000</Population>
	</Country>
</Data>

Preform Mail Merge with a Region

The following are the steps to preform mail merge with a region.

  • Create a Document object.
  • Load the Word template file using Document.loadFromFile() method.
  • Execute mail merge with a region using Document.getMailMerge().executeWidthRegion() method.
  • Save the changes to another file using Document.saveToFile() method.
  • Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;

public class MailMergeWithRegions {

    public static void main(String[] args) throws Exception {

        //Create a Document object
        Document doc = new Document();

        //Load the Word template file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\MailMergeTemplate.docx");

        //Execute mail merge with a region
        doc.getMailMerge().executeWidthRegion("C:\\Users\\Administrator\\Desktop\\Data.xml");

        //Save the changes to another file
        doc.saveToFile("output/MailMergeWithRegions.docx", FileFormat.Docx_2013);
    }
}

Java: Preform Mail Merge with a Region

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 104