├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── app ├── .gitignore ├── build.gradle ├── libs │ ├── aa-poi-3.10-min-0.1.5.jar │ └── aa-poi-ooxml-schemas-3.10-reduced-more-0.1.5.jar ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── readexcel │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── assets │ │ ├── contact.xls │ │ └── contact_new.xlsx │ ├── java │ │ └── com │ │ │ └── readexcel │ │ │ ├── ContactBean.java │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── raw │ │ ├── contact.xls │ │ └── contact_new.xlsx │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── readexcel │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "25.0.0" 6 | 7 | defaultConfig { 8 | applicationId "com.readexcel" 9 | minSdkVersion 14 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | multiDexEnabled true 14 | 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled true 19 | shrinkResources true 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | debug { 23 | debuggable true 24 | } 25 | } 26 | 27 | packagingOptions { 28 | exclude 'META-INF/DEPENDENCIES' 29 | exclude 'META-INF/NOTICE' 30 | exclude 'META-INF/NOTICE.txt' 31 | exclude 'META-INF/LICENSE' 32 | exclude 'META-INF/LICENSE.txt' 33 | } 34 | } 35 | 36 | dependencies { 37 | compile fileTree(dir: 'libs', include: ['*.jar']) 38 | testCompile 'junit:junit:4.12' 39 | // compile 'com.android.support:appcompat-v7:23.0.1' 40 | //compile 'com.android.support:design:23.0.1' 41 | compile 'com.android.support:multidex:1.0.1' 42 | 43 | /* def poiVersion='3.9' 44 | compile 'org.apache.poi:poi:'+poiVersion 45 | compile 'org.apache.poi:poi-ooxml:'+poiVersion 46 | compile 'org.apache.poi:poi-ooxml-schemas:'+poiVersion*/ 47 | } 48 | -------------------------------------------------------------------------------- /app/libs/aa-poi-3.10-min-0.1.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkoshti/ReadExcelFile/38f3a45227b79551abf0aee8aafb10ed3e46e928/app/libs/aa-poi-3.10-min-0.1.5.jar -------------------------------------------------------------------------------- /app/libs/aa-poi-ooxml-schemas-3.10-reduced-more-0.1.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkoshti/ReadExcelFile/38f3a45227b79551abf0aee8aafb10ed3e46e928/app/libs/aa-poi-ooxml-schemas-3.10-reduced-more-0.1.5.jar -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in F:\Android\Android_soft\adt-bundle-windows-x86_64-20140702\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/readexcel/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.readexcel; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /app/src/main/assets/contact.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkoshti/ReadExcelFile/38f3a45227b79551abf0aee8aafb10ed3e46e928/app/src/main/assets/contact.xls -------------------------------------------------------------------------------- /app/src/main/assets/contact_new.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rkoshti/ReadExcelFile/38f3a45227b79551abf0aee8aafb10ed3e46e928/app/src/main/assets/contact_new.xlsx -------------------------------------------------------------------------------- /app/src/main/java/com/readexcel/ContactBean.java: -------------------------------------------------------------------------------- 1 | package com.readexcel; 2 | 3 | 4 | public class ContactBean { 5 | 6 | private String Mobile; 7 | private String FirstName; 8 | 9 | public String getMobile() { 10 | return Mobile; 11 | } 12 | 13 | public void setMobile(String mobile) { 14 | Mobile = mobile; 15 | } 16 | 17 | public String getFirstName() { 18 | return FirstName; 19 | } 20 | 21 | public void setFirstName(String firstName) { 22 | FirstName = firstName; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/readexcel/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.readexcel; 2 | 3 | import android.app.Activity; 4 | import android.content.ContentProviderOperation; 5 | import android.os.Bundle; 6 | import android.provider.ContactsContract; 7 | import android.util.Log; 8 | import android.view.View; 9 | import android.widget.Button; 10 | import android.widget.Toast; 11 | 12 | import org.apache.poi.hssf.usermodel.HSSFCell; 13 | import org.apache.poi.hssf.usermodel.HSSFRow; 14 | import org.apache.poi.hssf.usermodel.HSSFSheet; 15 | import org.apache.poi.hssf.usermodel.HSSFWorkbook; 16 | import org.apache.poi.poifs.filesystem.POIFSFileSystem; 17 | import org.apache.poi.ss.usermodel.Cell; 18 | import org.apache.poi.ss.usermodel.Row; 19 | import org.apache.poi.xssf.usermodel.XSSFCell; 20 | import org.apache.poi.xssf.usermodel.XSSFSheet; 21 | import org.apache.poi.xssf.usermodel.XSSFWorkbook; 22 | 23 | import java.io.InputStream; 24 | import java.util.ArrayList; 25 | import java.util.Iterator; 26 | 27 | public class MainActivity extends Activity implements View.OnClickListener { 28 | 29 | private Button btnReadXLS; 30 | private Button btnReadXLSX; 31 | 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_main); 37 | //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 38 | 39 | //setSupportActionBar(toolbar); 40 | 41 | btnReadXLS = (Button) findViewById(R.id.btnReadXLS); 42 | btnReadXLSX = (Button) findViewById(R.id.btnReadXLSX); 43 | 44 | btnReadXLS.setOnClickListener(this); 45 | btnReadXLSX.setOnClickListener(this); 46 | 47 | 48 | } 49 | 50 | 51 | public void readXLSFileFromAssets() { 52 | 53 | 54 | try { 55 | // Creating Input Stream 56 | 57 | InputStream myInput; 58 | 59 | // Don't forget to Change to your assets folder excel sheet 60 | myInput = getAssets().open("contact.xls"); 61 | 62 | // Create a POIFSFileSystem object 63 | POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput); 64 | 65 | // Create a workbook using the File System 66 | HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem); 67 | 68 | // Get the first sheet from workbook 69 | HSSFSheet mySheet = myWorkBook.getSheetAt(0); 70 | 71 | /** We now need something to iterate through the cells. **/ 72 | Iterator rowIter = mySheet.rowIterator(); 73 | 74 | ArrayList contactList = new ArrayList<>(); 75 | 76 | while (rowIter.hasNext()) { 77 | HSSFRow myRow = (HSSFRow) rowIter.next(); 78 | Iterator cellIter = myRow.cellIterator(); 79 | 80 | ContactBean bean = new ContactBean(); 81 | 82 | while (cellIter.hasNext()) { 83 | 84 | HSSFCell myCell = (HSSFCell) cellIter.next(); 85 | if (myCell.getColumnIndex() == 0) { 86 | 87 | bean.setFirstName(myCell.toString()); 88 | } 89 | 90 | if (myCell.getColumnIndex() == 1) { 91 | 92 | bean.setMobile(myCell.toString()); 93 | } 94 | 95 | Log.e("FileUtils", "Cell Value: " + myCell.toString() + " Index :" + myCell.getColumnIndex()); 96 | 97 | } 98 | 99 | contactList.add(bean); 100 | } 101 | 102 | // contactList.size(); 103 | 104 | // AddNewContact(contactList); 105 | 106 | } catch (Exception e) { 107 | //e.printStackTrace(); 108 | } 109 | 110 | return; 111 | } 112 | 113 | public void readXLSXFileFromAssets() { 114 | 115 | try { 116 | // Creating Input Stream 117 | 118 | InputStream myInput; 119 | 120 | // Don't forget to Change to your assets folder excel sheet 121 | myInput = getAssets().open("contact_new.xlsx"); 122 | 123 | XSSFWorkbook workbook = new XSSFWorkbook(myInput); 124 | XSSFSheet mySheet = workbook.getSheetAt(0); 125 | 126 | 127 | /** We now need something to iterate through the cells. **/ 128 | Iterator rowIter = mySheet.rowIterator(); 129 | 130 | ArrayList contactList = new ArrayList<>(); 131 | 132 | while (rowIter.hasNext()) { 133 | Row myRow = (Row) rowIter.next(); 134 | Iterator cellIter = myRow.cellIterator(); 135 | 136 | ContactBean bean = new ContactBean(); 137 | 138 | while (cellIter.hasNext()) { 139 | 140 | XSSFCell myCell = (XSSFCell) cellIter.next(); 141 | if (myCell.getColumnIndex() == 0) { 142 | 143 | bean.setFirstName(myCell.toString()); 144 | } 145 | 146 | if (myCell.getColumnIndex() == 1) { 147 | 148 | bean.setMobile(myCell.toString()); 149 | } 150 | 151 | Log.e("FileUtils", "Cell Value: " + myCell.toString() + " Index :" + myCell.getColumnIndex()); 152 | 153 | } 154 | 155 | contactList.add(bean); 156 | } 157 | 158 | // contactList.size(); 159 | 160 | // AddNewContact(contactList); 161 | 162 | } catch (Exception e) { 163 | // e.printStackTrace(); 164 | } 165 | 166 | return; 167 | } 168 | 169 | private void AddNewContact(ArrayList contactList) { 170 | 171 | String DisplayName = contactList.get(5).getFirstName(); 172 | String MobileNumber = contactList.get(5).getMobile(); 173 | String HomeNumber = "1111"; 174 | String WorkNumber = "2222"; 175 | String emailID = "email@nomail.com"; 176 | String company = "bad"; 177 | String jobTitle = "abcd"; 178 | 179 | ArrayList ops = new ArrayList<>(); 180 | 181 | ops.add(ContentProviderOperation.newInsert( 182 | ContactsContract.RawContacts.CONTENT_URI) 183 | .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null) 184 | .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null) 185 | .build()); 186 | 187 | //------------------------------------------------------ Names 188 | if (DisplayName != null) { 189 | ops.add(ContentProviderOperation.newInsert( 190 | ContactsContract.Data.CONTENT_URI) 191 | .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 192 | .withValue(ContactsContract.Data.MIMETYPE, 193 | ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE) 194 | .withValue( 195 | ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, 196 | DisplayName).build()); 197 | } 198 | 199 | //------------------------------------------------------ Mobile Number 200 | if (MobileNumber != null) { 201 | ops.add(ContentProviderOperation. 202 | newInsert(ContactsContract.Data.CONTENT_URI) 203 | .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 204 | .withValue(ContactsContract.Data.MIMETYPE, 205 | ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 206 | .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, MobileNumber) 207 | .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 208 | ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) 209 | .build()); 210 | } 211 | 212 | //------------------------------------------------------ Home Numbers 213 | if (HomeNumber != null) { 214 | ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 215 | .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 216 | .withValue(ContactsContract.Data.MIMETYPE, 217 | ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 218 | .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, HomeNumber) 219 | .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 220 | ContactsContract.CommonDataKinds.Phone.TYPE_HOME) 221 | .build()); 222 | } 223 | 224 | //------------------------------------------------------ Work Numbers 225 | if (WorkNumber != null) { 226 | ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 227 | .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 228 | .withValue(ContactsContract.Data.MIMETYPE, 229 | ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) 230 | .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, WorkNumber) 231 | .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, 232 | ContactsContract.CommonDataKinds.Phone.TYPE_WORK) 233 | .build()); 234 | } 235 | 236 | //------------------------------------------------------ Email 237 | if (emailID != null) { 238 | ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 239 | .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 240 | .withValue(ContactsContract.Data.MIMETYPE, 241 | ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) 242 | .withValue(ContactsContract.CommonDataKinds.Email.DATA, emailID) 243 | .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK) 244 | .build()); 245 | } 246 | 247 | //------------------------------------------------------ Organization 248 | if (!company.equals("") && !jobTitle.equals("")) { 249 | ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI) 250 | .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0) 251 | .withValue(ContactsContract.Data.MIMETYPE, 252 | ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE) 253 | .withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, company) 254 | .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK) 255 | .withValue(ContactsContract.CommonDataKinds.Organization.TITLE, jobTitle) 256 | .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK) 257 | .build()); 258 | } 259 | 260 | // Asking the Contact provider to create a new contact 261 | try { 262 | getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 263 | } catch (Exception e) { 264 | e.printStackTrace(); 265 | Toast.makeText(MainActivity.this, "Exception: " + e.getMessage(), Toast.LENGTH_SHORT).show(); 266 | } 267 | } 268 | 269 | @Override 270 | public void onClick(View v) { 271 | 272 | switch (v.getId()) { 273 | 274 | case R.id.btnReadXLS: 275 | 276 | 277 | new Thread(new Runnable() { 278 | @Override 279 | public void run() { 280 | 281 | readXLSFileFromAssets(); 282 | } 283 | }).start(); 284 | 285 | 286 | break; 287 | 288 | case R.id.btnReadXLSX: 289 | 290 | 291 | new Thread(new Runnable() { 292 | @Override 293 | public void run() { 294 | 295 | readXLSXFileFromAssets(); 296 | } 297 | }).start(); 298 | 299 | 300 | break; 301 | } 302 | 303 | } 304 | 305 | } 306 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 17 | 18 |