以编程方式将一列中的值从android总结为excel

时间:2015-04-11 13:11:19

标签: android excel sum excel-formula apache-poi

当我尝试总结一个这样的列中的值时,它给出了0而不是总结它。

然后我在电话中打开sum的值显示为0。

如果我在PC中打开它,它将显示Microsoft Excel自己完成的原始总和值。然后在Mobile中,总和将显示为0,在PC中,总和将显示为280(这是必需的!!)。所以当我在手机中打开excel表时需要解决方案它应该是完美的。

这是代码,

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 > 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);
}
}

现在我的问题是,我需要做出哪些改变?

我哪里出错?

请告诉我..

谢谢..

1 个答案:

答案 0 :(得分:0)

可以确认您的代码创建了一个带有总和公式的Excel工作表,该公式在PC或Windows Phone上汇总确定(总计:280),但在使用Polaris查看器的Android设备上没有。

使用Planmaker应用在不同的Android设备上尝试使用它。它显示确定。此外,Sheet To Go from Documents To Go应用程序将显示280.

那么我们会责怪Polaris观众吗? -