This video shows the steps to develop a method to create the Word Document File in your Android App.
This uses the apache poi library for the same.
It takes the content of the file as an input from the user from the EditText (Plain text) widget.
It then creates the document file in the Files directory of that App. It uses the getExternalFilesDir API to create the respective file in the App’s data directory.
I hope you like this video. For any questions, suggestions or appreciation please contact us at: https://programmerworld.co/contact/ or email at: programmerworld1990@gmail.com
Source code:
package com.programmerworld.worddocapp;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private EditText editTextInput;
private File filePath = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE},
PackageManager.PERMISSION_GRANTED);
editTextInput = findViewById(R.id.editTextTextPersonName);
filePath = new File(getExternalFilesDir(null), "Test.docx");
try {
if (!filePath.exists()){
filePath.createNewFile();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void buttonCreate(View view){
try {
XWPFDocument xwpfDocument = new XWPFDocument();
XWPFParagraph xwpfParagraph = xwpfDocument.createParagraph();
XWPFRun xwpfRun = xwpfParagraph.createRun();
xwpfRun.setText(editTextInput.getText().toString());
xwpfRun.setFontSize(24);
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
xwpfDocument.write(fileOutputStream);
if (fileOutputStream!=null){
fileOutputStream.flush();
fileOutputStream.close();
}
xwpfDocument.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.programmerworld.worddocapp">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.WordDocApp">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="105dp"
android:layout_marginTop="99dp"
android:onClick="buttonCreate"
android:text="@string/create_doc_file"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="91dp"
android:layout_marginTop="54dp"
android:ems="10"
android:hint="@string/enter_the_content_here"
android:inputType="textPersonName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
android:autofillHints="" />
</androidx.constraintlayout.widget.ConstraintLayout>
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.programmerworld.worddocapp"
minSdkVersion 26
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'org.apache.poi:poi-ooxml:4.1.2'
implementation 'javax.xml.stream:stax-api:1.0'
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Screenshots:
I don’t have enough words to thank you!!!
Your words and comments will suffice. ?
Thanks for your comments.
Cheers
Programmer World
–
How can i write spanned texts to file?
To write the spannable texts in a word file, one use different options on XWPFRun object to set the attributes of the texts. For example in the below it shows how one can set font size, bold/ italics texts, underline text and different colors, etc.
xwpfRun.setText(“This is ProgrammerWorld”);
xwpfRun.setFontSize(24);
xwpfRun.setBold(true);
xwpfRun.setUnderline(UnderlinePatterns.DASH);
xwpfRun.setUnderlineColor(String.valueOf(Color.RED));
xwpfRun.setColor(String.valueOf(Color.GREEN));
xwpfRun.setItalic(true);
I hope this information is useful.
Cheers
ProgrammerWorld
–