创建Excel工作表和存储由活动数据 [英] Create excel sheet and store the data by activity

查看:164
本文介绍了创建Excel工作表和存储由活动数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个Excel工作表,并希望将数据存储在Excel工作表,比如我想要存储的图像和文本消息。其实我开发一个测试Android应用程序,我完成了测量部分,现在我想要做的报告。我没有搜索到很多时间在谷歌,但没有找到任何信息。任何一个可以知道如何创建从Android code一个Excel工作表和存储数据。

I want to create a excel sheet and want to store the data in that excel sheet, for example I want to store the image and text message. Actually I am developing a android app for measurement, I completed the measurement part and now I want to do the reporting. I did search many time in Google but nothing find any information. Can any one knows that how to create a excel sheet from the android code and store the data.

推荐答案

我用这个lib中的Excel导入和导出,支持高达ms2007

i used this lib for excel import and export , support upto ms2007

http://poi.apache.org/download.html ,轻松集成并使用

creating a file 


// check if available and not read only 
    if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { 
        Toast.makeText(context, "No External Storage", Toast.LENGTH_SHORT).show();
        Log.w("FileUtils", "Storage not available or read only"); 
        return false; 
    } 

    boolean success = false; 

    //New Workbook
    Workbook 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);

    //New Sheet
    Sheet sheet1 = null;
    sheet1 = wb.createSheet("job");


    Row row = sheet1.createRow(0);

    c = row.createCell(0);
    c.setCellValue("OperatorId : ");
    c.setCellStyle(cs);

    c = row.createCell(1);
    c.setCellValue("OperatorName : " );
    c.setCellStyle(cs);

    c = row.createCell(2);
    c.setCellValue("Blade : " );
    c.setCellStyle(cs);
    c = row.createCell(3);
    c.setCellValue("ShiftType : " );
    c.setCellStyle(cs);

    c = row.createCell(4);
    c.setCellValue("Bom : ");
    c.setCellStyle(cs);

    c = row.createCell(5);
    c.setCellValue("Job Created at : ");
    c.setCellStyle(cs);

    c = row.createCell(6);
    c.setCellValue("Blade Number: " );
    c.setCellStyle(cs);


    sheet1.setColumnWidth(0, (15 * 500));
    sheet1.setColumnWidth(1, (15 * 500));
    sheet1.setColumnWidth(2, (15 * 500));
    sheet1.setColumnWidth(3, (15 * 500));
    sheet1.setColumnWidth(4, (15 * 500));
    sheet1.setColumnWidth(5, (15 * 500));
    sheet1.setColumnWidth(6, (15 * 500));


    Row row2 = sheet1.createRow(1);

    c = row2.createCell(0);
    c.setCellValue("JobId");
    c.setCellStyle(cs);

    c = row2.createCell(1);
    c.setCellValue("BladeName");
    c.setCellStyle(cs);

    c = row2.createCell(2);
    c.setCellValue("Start Time");
    c.setCellStyle(cs);

    c = row2.createCell(3);
    c.setCellValue("End Time");
    c.setCellStyle(cs);

    c = row2.createCell(4);
    c.setCellValue("Time Taken");
    c.setCellStyle(cs);

    c = row2.createCell(5);
    c.setCellValue("Ply Number");
    c.setCellStyle(cs);




    SimpleDateFormat sdf = new SimpleDateFormat("HHmm_ddMMyyyy");
    String currentDateandTime = sdf.format(new Date());
    File file = new File(context.getExternalFilesDir(null), historyID+"History_"+currentDateandTime+".xls"); 
    FileOutputStream os = null; 

    try { 
        os = new FileOutputStream(file);
        wb.write(os);
        Log.w("FileUtils", "Writing file" + file); 
        success = true; 
    } catch (IOException e) { 
        Log.w("FileUtils", "Error writing " + file, e); 
    } catch (Exception e) { 
        Log.w("FileUtils", "Failed to save file", e); 
    } finally { 
        try { 
            if (null != os) 
                os.close(); 
        } catch (Exception ex) { 
        } 
    } 

    return success; 
} 


 public static boolean isExternalStorageReadOnly() { 
    String extStorageState = Environment.getExternalStorageState(); 
    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) { 
        return true; 
    } 
    return false; 
} 

public static boolean isExternalStorageAvailable() { 
    String extStorageState = Environment.getExternalStorageState(); 
    if (Environment.MEDIA_MOUNTED.equals(extStorageState)) { 
        return true; 
    } 
    return false; 
} 

使用权限

 android.permission.WRITE_EXTERNAL_STORAGE
 android.permission.READ_EXTERNAL_STORAGE


// Reading a excel file
        File file = new File(context.getExternalFilesDir(null), filename); 
        FileInputStream myInput = new FileInputStream(file);

        // Create a POIFSFileSystem object 
        POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);

        // Create a workbook using the File System 
        HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);

        // Get the first sheet from workbook 
        HSSFSheet mySheet = myWorkBook.getSheetAt(0);

        /** We now need something to iterate through the cells.**/
        Iterator<Row> rowIter = mySheet.rowIterator();
        Cell bladeIDcell = mySheet.getRow(0).getCell(0);
        Cell bladeNameCell = mySheet.getRow(0).getCell(0);


        HSSFSheet mySheet2 = myWorkBook.getSheetAt(1);
            Iterator<Row> rowIter2 = mySheet2.rowIterator();

            while(rowIter2.hasNext()){
                HSSFRow myRow = (HSSFRow) rowIter2.next();
                Iterator<Cell> cellIter = myRow.cellIterator();
                ArrayList<String> Valuesfromcell = new ArrayList<String>();
                while(cellIter.hasNext()){
                    HSSFCell myCell = (HSSFCell) cellIter.next();
                    Log.w("FileUtils", "Cell Value: " +  myCell.toString());
                    Valuesfromcell.add(myCell.toString());
                    //Toast.makeText(context, "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show();
                }

这篇关于创建Excel工作表和存储由活动数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆