title: Summing up values in one column programmatically from android to excel
body: <p>When I try to summing up the values in one such column it gives me 0 rather than summing it up.</p>
  
  <p>Then I open in phone the value of sum shows 0.</p>
  
  <p>If i open it in PC then it will show the original sum value done by Microsoft Excel by itself. And then in Mobile the sum will be shown 0 and in PC the sum will be shown as 280 (which is required..!!). So need solution when I open excel sheet in phone it should be perfect.</p>
  
  <p>Here is the code,</p>
  
  <pre><code>package com.example.excelsheet;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.util.Date;
  
  import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  import org.apache.poi.hssf.usermodel.HSSFSheet;
  import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
  import org.apache.poi.hssf.util.HSSFColor;
  import org.apache.poi.ss.usermodel.Cell;
  import org.apache.poi.ss.usermodel.CellStyle;
  import org.apache.poi.ss.usermodel.Row;
  
  import android.os.Bundle;
  import android.os.Environment;
  import android.support.v7.app.ActionBarActivity;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.Toast;
  
  public class MainActivity extends ActionBarActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      HSSFWorkbook wb = new HSSFWorkbook();
  
      // Cell c = null;
  
      // Cell style for header row
      CellStyle cs = wb.createCellStyle();
      cs.setFillForegroundColor(HSSFColor.LIME.index);
      cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  
      HSSFSheet firstSheet = wb.createSheet("MY FIRST SHEET");
      // HSSFSheet secondSheet = workbook.createSheet("MY SECOND SHEET");
  
      // HSSFRow row0 = firstSheet.createRow(0);
      // HSSFCell cell01 = row0.createCell(0);
      // cell.setCellValue(new HSSFRichTextString("MY FIRST SHEET"));
  
      // HSSFRow rowB = secondSheet.createRow(0);
      // HSSFCell cellB = rowB.createCell(0);
      // cellB.setCellValue(new HSSFRichTextString("MY SECOND SHEET"));
  
      // cell = row.createCell(0);
  
      int idxr = 0;
      int idyc = 0;
  
      Cell c = null;
  
      Row row = firstSheet.createRow(idxr);
      c = row.createCell(idyc);
      c.setCellValue("RUTURAJ Raval");
      c.setCellStyle(cs);
  
  
      idxr = idxr + 2;
      idyc = 0;
  
      row = firstSheet.createRow(idxr);
      c = row.createCell(idyc);
      c.setCellValue("Grand Total Sheet from receipt list");
      c.setCellStyle(cs);
  
      idxr = idxr + 2;
      idyc = 0;
  
      row = firstSheet.createRow(idxr);
      c = row.createCell(idyc);
      c.setCellValue("Total is:");
      c.setCellStyle(cs);
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue("280");
      c.setCellStyle(cs);
  
      idyc++;
  
      idxr = idxr + 2;
      idyc = 0;
  
      row = firstSheet.createRow(idxr);
      c = row.createCell(idyc);
      c.setCellValue("Sr no.");
      c.setCellStyle(cs);
      firstSheet.setColumnWidth(idyc, (10 * 500));
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue("Receipt Name");
      c.setCellStyle(cs);
      firstSheet.setColumnWidth(idyc, (10 * 500));
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue("Receipt Total");
      c.setCellStyle(cs);
      firstSheet.setColumnWidth(idyc, (10 * 500));
  
      idyc++;
  
      idxr = idxr + 1;
      idyc = 0;
  
      int firstRow = idxr + 1;
  
      row = firstSheet.createRow(idxr);
      c = row.createCell(idyc);
      c.setCellValue("1");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue("Maheshwari");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue(Integer.parseInt("10"));
  
      idyc++;
  
      idxr = idxr + 1;
      idyc = 0;
  
      row = firstSheet.createRow(idxr);
      c = row.createCell(idyc);
      c.setCellValue("2");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue("Arihant");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue(Integer.parseInt("20"));
  
      idyc++;
  
      idxr = idxr + 1;
      idyc = 0;
  
      row = firstSheet.createRow(idxr);
      c = row.createCell(idyc);
      c.setCellValue("3");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue("Hakim Chichi Sons");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue(Integer.parseInt("30"));
  
      idyc++;
  
      idxr = idxr + 1;
      idyc = 0;
  
      row = firstSheet.createRow(idxr);
      c = row.createCell(idyc);
      c.setCellValue("4");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue("D-Mart");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue(Integer.parseInt("40"));
  
      idyc++;
  
      idxr = idxr + 1;
      idyc = 0;
  
      row = firstSheet.createRow(idxr);
      c = row.createCell(idyc);
      c.setCellValue("5");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue("Dhiraj Sons");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue(Integer.parseInt("50"));
  
      idyc++;
  
      idxr = idxr + 1;
      idyc = 0;
  
      row = firstSheet.createRow(idxr);
      c = row.createCell(idyc);
      c.setCellValue("6");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue("Big Bazar");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue(Integer.parseInt("60"));
  
      idyc++;
  
      idxr = idxr + 1;
      idyc = 0;
  
      int lastRow = idxr + 1;
  
      row = firstSheet.createRow(idxr);
      c = row.createCell(idyc);
      c.setCellValue("7");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue("Atul Bakery");
  
      idyc++;
  
      c = row.createCell(idyc);
      c.setCellValue(Integer.parseInt("70"));
  
      idyc++;
  
      idxr = idxr + 1;
      idyc = 0;
      idyc = idyc + 1;
  
      row = firstSheet.createRow(idxr);
      c = row.createCell(idyc);
      c.setCellValue("Total:");
      c.setCellStyle(cs);
  
      idyc++;
  
      // Dim rExternalTotal As Range , dExternalTotal as Double
      //
      // @SuppressWarnings("rawtypes")
      // Set rExternalTotal = Range(rReportData.Offset(0, 0),
      // rReportData.Offset(261, 0).End(xlUp))
      // dExternalTotal = Application.WorksheetFunction.Sum(rExternalTotal)
      //
      // rExternalTotal = Application.WorksheetFunction.Sum(columns("A:A"));
  
      String myFormula = "SUM(" + getColumnName(idyc) + firstRow + ":"
              + getColumnName(idyc) + lastRow + ")";
  
      c = row.createCell(idyc);
      c.setCellType(Cell.CELL_TYPE_FORMULA);
      c.setCellFormula(myFormula);
  //      c.setCellValue("280 value");
      c.setCellStyle(cs);
  
  //      firstSheet.groupRow(firstRow-1,lastRow-1);
  //        firstSheet.setRowGroupCollapsed(firstRow-1, true);
  
  
  
      FileOutputStream fos = null;
      try {
  
          String path = Environment.getExternalStorageDirectory()
                  .getAbsolutePath() + "/ExcelSheet/";
          File file_path;
  
          file_path = new File(path);
  
          if (!file_path.exists())
              file_path.mkdir();
  
          String fileName = "Excel_" + new Date().getTime() + ".xls";
          File file = new File(path, fileName);
          fos = new FileOutputStream(file);
  
          wb.write(fos);
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          if (fos != null) {
              try {
                  fos.flush();
                  fos.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          Toast.makeText(MainActivity.this, "File Generated",
                  Toast.LENGTH_LONG).show();
  
      }
  
  }
  
  private String getColumnName(int columnNumber) {
  
      String columnName = "";
      int dividend = columnNumber + 1;
      int modulus;
  
      while (dividend &gt; 0) {
          modulus = (dividend - 1) % 26;
          columnName = (char) (65 + modulus) + columnName;
          dividend = (int) ((dividend - modulus) / 26);
      }
  
      return columnName;
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_settings) {
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  }
  </code></pre>
  
  <p>Now my question is, what changes do I need to put?</p>
  
  <p>Where am I getting wrong??</p>
  
  <p>Please let me know..</p>
  
  <p>Thank You..</p>