SoFunction
Updated on 2025-04-04

Android operation excel function instance code

Learn app read and write control of Excel

1. Interface design

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
  xmlns:tools="/tools"
  android:
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="">
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">
    <TableLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="#ffcccccc"
      android:layout_margin="1dp">
    <!-- line1 -->
    <TableRow
      android:layout_width="match_parent"
      android:layout_height="20dp"
      android:background="#ffcccccc"
      android:layout_margin="0.5dp">
      <TextView
        android:layout_width="0.0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Engineering Data Import"
        android: />
      <TextView
        android:text="Default Path"
        android:layout_width="0.0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android: />
    </TableRow>
      <!---->
      <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffcc99"
        android:layout_margin="0.5dp">
        <Button
          android:text="Import Data"
          android:layout_width="0.0dp"
          android:layout_height="match_parent"
          android:
          android:textSize="12dp"
          android:layout_weight="1"/>
        <Button
          android:text="Clean data"
          android:layout_width="0.0dp"
          android:layout_height="match_parent"
          android:
          android:textSize="12dp"
          android:layout_weight="1"/>
      </TableRow>
    </TableLayout>
    <.
      android:scrollbars="vertical"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:divider="#ffff0000"
      android:dividerHeight="10dp"
      android:/>
  </LinearLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
  xmlns:app="/apk/res-auto"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffcccccc"
    android:layout_margin="0dp">
    <TableRow
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="#ffffcc99"
      android:layout_margin="0.5dp">
      <TextView
        android:text="Ci"
        android:layout_width="0.0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textSize="12dp"
        android: />
      <TextView
        android:text="CellName:"
        android:layout_width="0.0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textSize="12dp"
        android: />
      <TextView
        android:text="Longtitude"
        android:layout_width="0.0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textSize="12dp"
        android: />
      <TextView
        android:text="Attitude:"
        android:layout_width="0.0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textSize="12dp"
        android: />
      <TextView
        android:text="Azimuth:"
        android:layout_width="0.0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:textSize="12dp"
        android: />
    </TableRow>
  </TableLayout>
</LinearLayout>

2. Code design

package ;
import ;
import .;
import ;
import ;
import ;
import ;
import .;
import .;
import .;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import jxl.*;
import ;
import ;
import ;
import static .;
public class MainActivity extends AppCompatActivity {
  public static final int IMPORT_FILE_SUCCESS = 0;
  public static final int IMPORT_FILE_NOT_EXISTS= 1;
  public static final int IMPORT_FILE_FAIL = 2;
  private static final String LOG_TAG = "NPLOG";
  private String npmasterDir = "/npmaster/";
  private String filepName = "";
  private String absFullPath = "";
  private static final int DEFAULT_SHEET = 0;
  public List&lt;EngineerCellInfo&gt; cfgCelllist = null;
  public EngineerCellInfo defaultItem;
  private EngineerRecycleViewAdapter myRecycleViewAdapter;
  private RecyclerView recyclerView;
  Toast toast;
  Button btClear,btImport;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    /* Generate default title */
    initData();
    absFullPath =().toString()+npmasterDir;
    createFolder(absFullPath);
    createEngineerFile(absFullPath+filepName);
    generateTestData();
    TextView tv = (TextView) findViewById();
    (absFullPath+filepName);
    recyclerView = (RecyclerView)findViewById();
    //The line layout is similar to listview    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    ();
    (layoutManager);
    myRecycleViewAdapter = new EngineerRecycleViewAdapter(,cfgCelllist);
    (myRecycleViewAdapter);
    (new DefaultItemAnimator());
    btClear = (Button)findViewById();
    btImport = (Button)findViewById();
    (new MyClickListener());
    (new MyClickListener());
  }
  class MyClickListener implements 
  {
    @Override
    public void onClick(View v)
    {
      switch(())
      {
        case :
          int result =readExcel(absFullPath+filepName);
          toast = (,(result),Toast.LENGTH_SHORT);
          ();
          ();
          break;
        case :
          toast = (,"clear",Toast.LENGTH_SHORT);
          ();
          break;
        default:
          break;
      }
    }
  }
  public void initData()
  {
    cfgCelllist = new ArrayList&lt;EngineerCellInfo&gt;();
    defaultItem = new EngineerCellInfo();
    [0] = "Ci";
    [1] = "Cellname";
    [2] = "Longtitude";
    [3] = "Attitude";
    [4] = "Azimuth";
    (defaultItem);
  }
  public void generateTestData()
  {
    EngineerCellInfo engineerCellInfo = new EngineerCellInfo();
    [0] = "134633";
    [1] = "xx community";
    [2] = "103.89866";
    [3] = "30";
    [4] = "60";
    (engineerCellInfo);
    writeToFile(absFullPath+filepName);
  }
  /* create folder if not exists*/
  public void createFolder(String strFolder)
  {
    File file = new File(strFolder);
    if (!())
    {
      if (())
      {
        return;
      }
    }
  }
  public void writeToFile(String cfgFileName)
  {
    File file = null;
    if (()&lt;=1)
    {
      return;
    }
    try {
      file = new File(cfgFileName);
      if(())
      {
        WritableWorkbook book = (file);
        WritableSheet sheet = (DEFAULT_SHEET);
        for (int i = 1; i &lt;();i++)
        {
          for (int j=0;j&lt;5;j++)
          {
            Label labelCi= new Label( j, i, (i).item[j]);
            (labelCi);
          }
        }
        ();
        ();
      }
    }
    catch(Exception e)
    {
      ();
    }
  }
  /* Create the default file of the working parameters, if the file does not exist, create */
  public void createEngineerFile(String cfgFileName)
  {
    File file = null;
    try {
      file = new File(cfgFileName);
      if(()) {
        WritableWorkbook book = (file);
        WritableSheet sheet = ("task", DEFAULT_SHEET);
        for (int i = 0; i &lt;();i++)
        {
          for (int j=0;j&lt;5;j++)
          {
            Label labelCi= new Label( j, i, (i).item[j]);
            (labelCi);
          }
        }
        ();
        ();
      }
    }
    catch(Exception e)
    {
      ();
    }
  }
  /* read the cell file from engineer file */
  public int readExcel(String cfgFileName)
  {
    TextView tv = (TextView) findViewById();
    try
    {
      File file = new File(cfgFileName);
      if (!())
      {
        return IMPORT_FILE_NOT_EXISTS;
      }
      InputStream inputStream = new FileInputStream(file);
      Workbook workbook = (inputStream);
      Sheet sheet = (0);
      /* ignore the first row*/
      for (int i = 1; i &lt; ();i++)
      {
        EngineerCellInfo engineerCellInfo = new EngineerCellInfo();
        for (int j = 0; j &lt; 5;j++)
        {
          Cell cell = (j,i);
          [j] = ();
        }
        (engineerCellInfo);
        //(());
      }
      ();
    }
    catch(Exception e)
    {
      (LOG_TAG,"file to find the file");
      return IMPORT_FILE_FAIL;
    }
    return IMPORT_FILE_SUCCESS;
  }
  public class EngineerCellInfo
  {
    public String item[]; //use array to store
    public EngineerCellInfo()
    {
      item = new String[5];
    }
    public String toString()
    {
      return item[0]+"-"+item[1]+"-"+item[2]+"-"+item[3]+"-"+item[4];
    }
  }
}
package ;
import ;
import .;
import ;
import ;
import ;
import ;
import ;
import ;
public class EngineerRecycleViewAdapter extends <>
{
  private List<> itemsData;
  private Context mContext;
  private LayoutInflater inflater;
  public EngineerRecycleViewAdapter(Context context, List<EngineerCellInfo> itemsData)
  {
     = itemsData;
    =context;
    inflater=(mContext);
  }
  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
  {
    View view = (, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
  }
  @Override
  public void onBindViewHolder(final MyViewHolder holder, final int position)
  {
    (((position).item[0]));
    (((position).item[1]));
    (((position).item[2]));
    (((position).item[3]));
    (((position).item[4]));
  }
  @Override
  public int getItemCount()
  {
    return ();
  }
  public static class MyViewHolder extends 
  {
    public TextView tvCId;
    public TextView tvCellName;
    public TextView tvlongtitude;
    public TextView tvAttitude;
    public TextView tvAzimuth;
    public MyViewHolder(View view)
    {
      super(view);
      tvCId = (TextView)();
      tvCellName = (TextView)();
      tvlongtitude = (TextView)();
      tvAttitude = (TextView) ();
      tvAzimuth = (TextView) ();
    }
  }
}

3. Used jxl development package and recyclerview

compile ':appcompat-v7:25.0.1'
compile ':recyclerview-v7:25.0.1'
testCompile 'junit:junit:4.12'
compile files('libs/jxl-2.')

The above is the example code for Android operation excel function introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message. The editor will reply to everyone in time!