Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 创建excel工作表并按活动存储数据_Android - Fatal编程技术网

Android 创建excel工作表并按活动存储数据

Android 创建excel工作表并按活动存储数据,android,Android,我想创建一个excel工作表,并想在该excel工作表中存储数据,例如,我想存储图像和文本消息。实际上,我正在开发一个android应用程序进行测量,我完成了测量部分,现在我想做报告。我在谷歌上搜索了很多次,但都没有找到任何信息。任何人都知道如何从android代码创建excel工作表并存储数据。我使用这个库进行excel导入和导出,支持ms2007 ,易于集成和使用 creating a file // check if available and not read only

我想创建一个excel工作表,并想在该excel工作表中存储数据,例如,我想存储图像和文本消息。实际上,我正在开发一个android应用程序进行测量,我完成了测量部分,现在我想做报告。我在谷歌上搜索了很多次,但都没有找到任何信息。任何人都知道如何从android代码创建excel工作表并存储数据。

我使用这个库进行excel导入和导出,支持ms2007

,易于集成和使用

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();
                }
android.permission.WRITE\u外部存储
android.permission.READ_外部存储
//读取excel文件
File File=新文件(context.getExternalFilesDir(null),文件名);
FileInputStream myInput=新的FileInputStream(文件);
//创建一个POIFSSystem对象
POIFSFileSystem myFileSystem=新的POIFSFileSystem(myInput);
//使用文件系统创建工作簿
HSSFWorkbook myWorkBook=新的HSSFWorkbook(myFileSystem);
//从工作簿中获取第一张工作表
HSSFSheet mySheet=myWorkBook.getSheetAt(0);
/**我们现在需要一些东西来遍历单元格**/
迭代器rowIter=mySheet.rowitter();
单元格bladeIDcell=mySheet.getRow(0).getCell(0);
单元格bladeNameCell=mySheet.getRow(0).getCell(0);
HSSFSheet mySheet2=myWorkBook.getSheetAt(1);
迭代器rowIter2=mySheet2.rowIter2();
while(rowIter2.hasNext()){
HSSFRow myRow=(HSSFRow)rowIter2.next();
迭代器cellIter=myRow.cellIterator();
ArrayList值fromcell=新的ArrayList();
while(cellIter.hasNext()){
HSSFCell myCell=(HSSFCell)cellIter.next();
w(“FileUtils”,“单元格值:”+myCell.toString());
值fromcell.add(myCell.toString());
//Toast.makeText(上下文,“单元格值:+myCell.toString(),Toast.LENGTH_SHORT).show();
}

我将此库用于excel导入和导出,支持ms2007

,易于集成和使用

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();
                }
android.permission.WRITE\u外部存储
android.permission.READ_外部存储
//读取excel文件
File File=新文件(context.getExternalFilesDir(null),文件名);
FileInputStream myInput=新的FileInputStream(文件);
//创建一个POIFSSystem对象
POIFSFileSystem myFileSystem=新的POIFSFileSystem(myInput);
//使用文件系统创建工作簿
HSSFWorkbook myWorkBook=新的HSSFWorkbook(myFileSystem);
//从工作簿中获取第一张工作表
HSSFSheet mySheet=myWorkBook.getSheetAt(0);
/**我们现在需要一些东西来遍历单元格**/
迭代器rowIter=mySheet.rowitter();
单元格bladeIDcell=mySheet.getRow(0).getCell(0);
单元格bladeNameCell=mySheet.getRow(0).getCell(0);
HSSFSheet mySheet2=myWorkBook.getSheetAt(1);
迭代器rowIter2=mySheet2.rowIter2();
while(rowIter2.hasNext()){
HSSFRow myRow=(HSSFRow)rowIter2.next();
迭代器cellIter=myRow.cellIterator();
ArrayList值fromcell=新的ArrayList();
while(cellIter.hasNext()){
HSSFCell myCell=(HSSFCell)cellIter.next();
w(“FileUtils”,“单元格值:”+myCell.toString());
值fromcell.add(myCell.toString());
//Toast.makeText(上下文,“单元格值:+myCell.toString(),Toast.LENGTH_SHORT).show();
}

尝试以下用于存储文本的代码:

        String filename = "abc.csv";
        String data = arrayListDGId.toString();  //arraylist contain your text
        data = data.replace("[", "").replace("]", "");              
        FileOutputStream fos;  
           try {  
               File dir = new File("/sdcard/FOLDERNAME/");
               boolean b = dir.mkdir();
                File myFile = new File(dir, filename);  
                myFile.createNewFile();  
                FileOutputStream fOut = new FileOutputStream(myFile);  
                OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);  
                for (String s : arrayListDGId) {
                    myOutWriter.append(s + "\n"); //stores data to newline in same column
                }

                myOutWriter.close();  
                fOut.close();  
           } catch (FileNotFoundException e) {e.printStackTrace();}  
           catch (IOException e) {e.printStackTrace();}  

请尝试以下用于存储文本的代码:

        String filename = "abc.csv";
        String data = arrayListDGId.toString();  //arraylist contain your text
        data = data.replace("[", "").replace("]", "");              
        FileOutputStream fos;  
           try {  
               File dir = new File("/sdcard/FOLDERNAME/");
               boolean b = dir.mkdir();
                File myFile = new File(dir, filename);  
                myFile.createNewFile();  
                FileOutputStream fOut = new FileOutputStream(myFile);  
                OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);  
                for (String s : arrayListDGId) {
                    myOutWriter.append(s + "\n"); //stores data to newline in same column
                }

                myOutWriter.close();  
                fOut.close();  
           } catch (FileNotFoundException e) {e.printStackTrace();}  
           catch (IOException e) {e.printStackTrace();}  


我也尝试使用这个lib,我也下载了2-3文件,但我无法使用这个文件。下载lib文件并将其添加到项目libs/文件夹文件将在Android/data/your app package Name中创建我也尝试使用这个lib,我也下载了2-3文件,但我无法使用这个文件。下载lib文件并添加它在项目中,libs/文件夹文件将以Android/data/你的应用程序包名创建。你考虑过创建CSV文件吗?没有jmrodrigg。你能告诉我与CSV文件相关的任何想法吗。如果你提供更多关于你试图存储的数据类型的信息,我会尽力提供帮助。我正在制作一个测量应用程序。我在哪里绘制一些图形(例如直线、圆、矩形等)完成测量后,我想生成一份报告。我想显示直线的长度、圆的面积等。我知道长度和面积,但如何在excel表中显示这些内容,以及如何在excel表中保存捕获的图像。我为报告提供了一个按钮。因此,如果您想附加图像,恐怕您无法使用一个CSV文件。我帮不上忙。你有没有考虑过创建CSV文件的想法?没有jmrodrigg。你能告诉我任何与CSV文件相关的想法吗。如果你提供更多关于你试图存储的数据类型的信息,我会尽力帮你。我正在制作一个测量应用程序。我正在做一些绘图(例如直线、圆、矩形等)完成测量后,我想生成一份报告。我想显示直线的长度、圆的面积等。我知道长度和面积,但如何在excel表中显示这些内容,以及如何在excel表中保存捕获的图像。我为报告提供了一个按钮。因此,如果您想附加图像,恐怕您无法使用一个CSV文件。我帮不上忙。你需要这两个权限:谢谢Rit,我也会尝试这一个。如果你得到正确的答案,那么你应该投票表决该答案。这样其他人可以引用该答案。这是stackoverflow概念。是的Rit,我正在跟踪所有答案。当我完成后,我肯定会投票表决该答案。你需要