How to insert / put image in an excel file from your Android App?

This video shows the steps to insert images in your excel file. It uses apache implementation and uses HSSFSheet to insert the image at a particular position (anchor) in the sheet.

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.insertimageinexcel;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class MainActivity extends AppCompatActivity {

private EditText editText;
private ImageView imageView;

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.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE},
PackageManager.PERMISSION_GRANTED);

editText = findViewById(R.id.editText);
imageView = findViewById(R.id.imageView);

filePath = new File(getExternalFilesDir(null), "ExcelFile.xlsx");
}

public void buttonInsertImage(View view){
try {
String stringImageFilePath = Environment.getExternalStorageDirectory().getPath() +
"/Download/"+editText.getText().toString()+".jpeg";

Bitmap bitmap = BitmapFactory.decodeFile(stringImageFilePath);
imageView.setImageBitmap(bitmap);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 0, byteArrayOutputStream);
byte[] bytesImage = byteArrayOutputStream.toByteArray();
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
int intPictureIndex = hssfWorkbook.addPicture(bytesImage, Workbook.PICTURE_TYPE_JPEG);
CreationHelper creationHelper = hssfWorkbook.getCreationHelper();

ClientAnchor clientAnchor = creationHelper.createClientAnchor();
clientAnchor.setCol1(1);
clientAnchor.setRow1(1);
clientAnchor.setCol2(5);
clientAnchor.setRow2(10);

HSSFSheet hssfSheet = hssfWorkbook.createSheet("Programmer World");
Drawing drawing = hssfSheet.createDrawingPatriarch();
drawing.createPicture(clientAnchor, intPictureIndex);
hssfSheet.createRow(1).createCell(1);
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
hssfWorkbook.write(fileOutputStream);

if (fileOutputStream!=null){
fileOutputStream.flush();
fileOutputStream.close();
}
hssfWorkbook.close();
}
catch (Exception e){
}
}
}

plugins {
id 'com.android.application'
}

android {
compileSdk 31

defaultConfig {
applicationId "com.programmerworld.insertimageinexcel"
minSdk 31
targetSdk 31
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.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation 'org.apache.poi:poi:5.1.0'

}
<?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/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="108dp"
android:layout_marginTop="28dp"
android:onClick="buttonInsertImage"
android:text="@string/insert_image"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="74dp"
android:layout_marginTop="74dp"
android:ems="10"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="SpeakableTextPresentCheck"
android:autofillHints="" />

<ImageView
android:id="@+id/imageView"
android:layout_width="263dp"
android:layout_height="290dp"
android:layout_marginStart="57dp"
android:layout_marginTop="70dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button8"
app:srcCompat="@drawable/ic_launcher_background"
android:contentDescription="@string/todo" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.programmerworld.insertimageinexcel">

<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.InsertImageInExcel">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Leave a Reply

%d bloggers like this: