How to create Microsoft Word Document docx File from your Android App
>> YOUR LINK HERE: ___ http://youtube.com/watch?v=2oT5heXHa94
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: [email protected] • Complete source code and other details can be found in the below link: • https://programmerworld.co/android/ho... • • However, the main Java code is copied below also for reference: • 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(); • } • } • } • • • -
#############################
