简体   繁体   中英

Writing to excel file with apache poi in android project

I have problem whit getting my project to work i will paste the java files and error log hopefully someone can give me a hint. The app crash when button R.id.bskickaTidSc3 in TidSc3.java is clicked.

error log

06-08 12:45:49.365: E/dalvikvm(1243): Could not find class 'org.apache.poi.hssf.usermodel.HSSFWorkbook', referenced from method com.example.spapp_beta.TidsedelExcel.SetExcelVecka
06-08 12:45:49.365: W/dalvikvm(1243): VFY: unable to resolve new-instance 67 (Lorg/apache/poi/hssf/usermodel/HSSFWorkbook;) in Lcom/example/spapp_beta/TidsedelExcel;
06-08 12:45:49.365: D/dalvikvm(1243): VFY: replacing opcode 0x22 at 0x0000
06-08 12:45:49.365: D/dalvikvm(1243): DexOpt: unable to opt direct call 0x0087 at 0x09 in Lcom/example/spapp_beta/TidsedelExcel;.SetExcelVecka
06-08 12:45:49.375: D/AndroidRuntime(1243): Shutting down VM
06-08 12:45:49.375: W/dalvikvm(1243): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
06-08 12:45:49.387: E/AndroidRuntime(1243): FATAL EXCEPTION: main
06-08 12:45:49.387: E/AndroidRuntime(1243): java.lang.NoClassDefFoundError: org.apache.poi.hssf.usermodel.HSSFWorkbook
06-08 12:45:49.387: E/AndroidRuntime(1243):     at com.example.spapp_beta.TidsedelExcel.SetExcelVecka(TidsedelExcel.java:17)
06-08 12:45:49.387: E/AndroidRuntime(1243):     at com.example.spapp_beta.TidSc3.onClick(TidSc3.java:96)
06-08 12:45:49.387: E/AndroidRuntime(1243):     at android.view.View.performClick(View.java:4204)
06-08 12:45:49.387: E/AndroidRuntime(1243):     at android.view.View$PerformClick.run(View.java:17355)
06-08 12:45:49.387: E/AndroidRuntime(1243):     at android.os.Handler.handleCallback(Handler.java:725)
06-08 12:45:49.387: E/AndroidRuntime(1243):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-08 12:45:49.387: E/AndroidRuntime(1243):     at android.os.Looper.loop(Looper.java:137)
06-08 12:45:49.387: E/AndroidRuntime(1243):     at android.app.ActivityThread.main(ActivityThread.java:5041)
06-08 12:45:49.387: E/AndroidRuntime(1243):     at java.lang.reflect.Method.invokeNative(Native Method)
06-08 12:45:49.387: E/AndroidRuntime(1243):     at java.lang.reflect.Method.invoke(Method.java:511)
06-08 12:45:49.387: E/AndroidRuntime(1243):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-08 12:45:49.387: E/AndroidRuntime(1243):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-08 12:45:49.387: E/AndroidRuntime(1243):     at dalvik.system.NativeStart.main(Native Method)
06-08 12:46:37.675: E/Trace(1261): error opening trace file: No such file or directory (2)
06-08 12:46:38.065: D/gralloc_goldfish(1261): Emulator without GPU emulation detected.

TidSc3.java

package com.example.spapp_beta;

import java.io.FileNotFoundException;
import java.io.IOException;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class TidSc3 extends Activity implements OnClickListener {

    Button skicka, visa;
    TextView namn, vecka, ar, arbplts, man,tis,ons,tors,fre,lor,son,oI,oII,restid,km,trakt;
    EditText v,ovrigt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tid_sc3);
        ovrigt = (EditText) findViewById(R.id.eTovrigt);
        v = (EditText) findViewById(R.id.eTtidSc3vecka);
        namn = (TextView) findViewById(R.id.tVsamNamn);
        vecka = (TextView) findViewById(R.id.tVsamVecka);
        ar = (TextView) findViewById(R.id.tVsamAr);
        arbplts = (TextView) findViewById(R.id.tVsamArbplts);
        man = (TextView) findViewById(R.id.tVsamMan);
        tis = (TextView) findViewById(R.id.tVsamTis);
        ons = (TextView) findViewById(R.id.tVsamOns);
        tors = (TextView) findViewById(R.id.tVsamTors);
        fre = (TextView) findViewById(R.id.tVsamFre);
        lor = (TextView) findViewById(R.id.tVsamLor);
        son = (TextView) findViewById(R.id.tVsamSon);
        oI = (TextView) findViewById(R.id.tVsamOI);
        oII = (TextView) findViewById(R.id.tVsamOII);
        restid = (TextView) findViewById(R.id.tVsamRestid);
        km = (TextView) findViewById(R.id.tVsamKm);
        trakt = (TextView) findViewById(R.id.tVsamTrakt);

        visa = (Button) findViewById(R.id.bvisa);
        skicka = (Button) findViewById(R.id.bskickaTidSc3);
        skicka.setOnClickListener(this);
        visa.setOnClickListener(this);
    }   
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                switch (arg0.getId()){

                case R.id.bvisa:
                    String s = v.getText().toString();
                    long l = Long.parseLong(s);
                    String[] veckaA = new String[16];
                    DbTidsedel2013 get = new DbTidsedel2013(TidSc3.this);
                    get.open();
                    veckaA = get.VeckaArray(l);
                    get.close();

                    vecka.setText(veckaA[0]);
                    ar.setText(veckaA[1]);
                    namn.setText(veckaA[2]);
                    arbplts.setText(veckaA[3] + "\n");
                    man.setText(veckaA[4]);
                    tis.setText(veckaA[5]);
                    ons.setText(veckaA[6]);
                    tors.setText(veckaA[7]);
                    fre.setText(veckaA[8]);
                    lor.setText(veckaA[9]);
                    son.setText(veckaA[10]);
                    restid.setText(veckaA[11]);
                    km.setText(veckaA[12]);
                    oI.setText(veckaA[13]);
                    oII.setText(veckaA[14]);
                    trakt.setText(veckaA[15]);
                    break;

                case R.id.bskickaTidSc3:
                    String s1 = v.getText().toString();
                    long l1 = Long.parseLong(s1);
                    String[] veckaA1 = new String[16];
                    DbTidsedel2013 get1 = new DbTidsedel2013(TidSc3.this);
                    get1.open();
                    veckaA1 = get1.VeckaArray(l1);
                    get1.close();

                    TidsedelExcel tidEx = new TidsedelExcel();
                    try {
                        tidEx.SetExcelVecka(veckaA1);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }


                    String ov = ovrigt.getText().toString();
                    Intent intent = new Intent(Intent.ACTION_SENDTO); 
                    intent.setType("text/plain");
                    intent.putExtra(Intent.EXTRA_SUBJECT, "Tid vecka " + veckaA1[0]);
                    intent.putExtra(Intent.EXTRA_TEXT, ov);
                    intent.setData(Uri.parse("mailto:")); 
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
                    startActivity(intent);

                }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.tid_sc3, menu);
        return true;
    }

}

TidsedelExcel.java

package com.example.spapp_beta;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class TidsedelExcel {

    public void SetExcelVecka (String[] vecka) throws FileNotFoundException, IOException{

        Workbook workbook = new HSSFWorkbook(new FileInputStream("/assets/TsO.xls"));
        Sheet sheet = workbook.getSheetAt(0);

        Cell cellvecka1 = sheet.getRow(1).getCell(14);
        cellvecka1.setCellValue(vecka[0]);

        Cell cellvecka2 = sheet.getRow(7).getCell(0);
        cellvecka2.setCellValue(vecka[0]);

        Cell cellAr = sheet.getRow(1).getCell(10);
        cellAr.setCellValue(vecka[1]);

        Cell cellNamn = sheet.getRow(3).getCell(0);
        cellNamn.setCellValue(vecka[2]);

        Cell cellArbplts = sheet.getRow(3).getCell(10);
        cellArbplts.setCellValue(vecka[3]);

        Cell cellMan = sheet.getRow(7).getCell(3);
        int man = Integer.parseInt(vecka[4]);   
        cellMan.setCellValue(man);

        Cell cellTis = sheet.getRow(7).getCell(4);
        int tis = Integer.parseInt(vecka[5]);
        cellTis.setCellValue(tis);

        Cell cellOns = sheet.getRow(7).getCell(5);
        int ons = Integer.parseInt(vecka[6]);
        cellOns.setCellValue(ons);

        Cell cellTors = sheet.getRow(7).getCell(6);
        int tors = Integer.parseInt(vecka[7]);
        cellTors.setCellValue(tors);

        Cell cellFre = sheet.getRow(7).getCell(7);
        int fre = Integer.parseInt(vecka[8]);
        cellFre.setCellValue(fre);

        Cell cellLor = sheet.getRow(7).getCell(8);
        int lor = Integer.parseInt(vecka[9]);
        cellLor.setCellValue(lor);

        Cell cellSon = sheet.getRow(7).getCell(9);
        int son = Integer.parseInt(vecka[10]);
        cellSon.setCellValue(son);

        Cell cellRestid = sheet.getRow(7).getCell(10);
        int restid = Integer.parseInt(vecka[11]);
        cellRestid.setCellValue(restid);

        Cell cellMil = sheet.getRow(7).getCell(16);
        int mil = Integer.parseInt(vecka[12]);
        cellMil.setCellValue(mil);

        Cell cellOI = sheet.getRow(7).getCell(13);
        int oI = Integer.parseInt(vecka[13]);
        cellOI.setCellValue(oI);

        Cell cellOII = sheet.getRow(7).getCell(14);
        int oII = Integer.parseInt(vecka[14]);
        cellOII.setCellValue(oII);

        Cell cellTrakt = sheet.getRow(7).getCell(15);
        int trakt = Integer.parseInt(vecka[15]);
        cellTrakt.setCellValue(trakt);

        // Write the output to a file
        FileOutputStream fileOut = new FileOutputStream("/assets/Tidsedel_V_" + vecka[0] + "_" + vecka[1] + ".xls");
        workbook.write(fileOut);
        fileOut.close();

    }

}

Thanks to anyone that is kind to help out

The error says

06-08 12:45:49.387: E/AndroidRuntime(1243): java.lang.NoClassDefFoundError: org.apache.poi.hssf.usermodel.HSSFWorkbook

Go to Project properties > Java Build Path > Order and Export tab and select the library you have used in your project..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM