以下代码出错

时间:2014-04-10 10:38:22

标签: android

获取以下包裹的错误:

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Cell;

可以帮我解决以下代码以解决错误。

public class AndroidReadExcelActivity extends Activity implements OnClickListener{
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            View writeButton = findViewById(R.id.write);
            writeButton.setOnClickListener(this);
            View readButton = findViewById(R.id.read);
            readButton.setOnClickListener(this);
            View writeExcelButton = findViewById(R.id.writeExcel);
            writeExcelButton.setOnClickListener(this);
            View readExcelButton = findViewById(R.id.readExcel);
            readExcelButton.setOnClickListener(this);

        }

        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.write:
                saveFile(this,"myFile.txt");
                break;
            case R.id.read:
                readFile(this,"myFile.txt");
                break;
            case R.id.writeExcel:
                saveExcelFile(this,"myExcel.xls");
                break;
            case R.id.readExcel:
                readExcelFile(this,"myExcel.xls");
                break;   
            }
        }

        private static boolean saveFile(Context context, String fileName) { 

            // check if available and not read only 
            if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { 
                Log.w("FileUtils", "Storage not available or read only"); 
                return false; 
            } 

            // Create a path where we will place our List of objects on external storage 
            File file = new File(context.getExternalFilesDir(null), fileName); 
            PrintStream p = null; // declare a print stream object
            boolean success = false; 

            try { 
                OutputStream os = new FileOutputStream(file); 
                // Connect print stream to the output stream
                p = new PrintStream(os);
                p.println("This is a TEST");
                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 != p) 
                        p.close(); 
                } catch (Exception ex) { 
                } 
            } 

            return success; 
        } 

        private static void readFile(Context context, String filename) { 

            if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) 
            { 
                Log.w("FileUtils", "Storage not available or read only"); 
                return; 
            } 

            FileInputStream fis = null;

            try
            { 
                File file = new File(context.getExternalFilesDir(null), filename); 
                fis = new FileInputStream(file); 
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fis);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                //Read File Line By Line
                while ((strLine = br.readLine()) != null) {
                    Log.w("FileUtils", "File data: " + strLine);
                    Toast.makeText(context, "File Data: " + strLine , Toast.LENGTH_SHORT).show();
                }
                in.close();
            } 
            catch (Exception ex) { 
                Log.e("FileUtils", "failed to load file", ex); 
            } 
            finally { 
                try {if (null != fis) fis.close();} catch (IOException ex) {} 
            } 

            return;
        } 

        private static boolean saveExcelFile(Context context, String fileName) { 

            // check if available and not read only 
            if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) { 
                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("myOrder");

            // Generate column headings
            Row row = sheet1.createRow(0);

            c = row.createCell(0);
            c.setCellValue("Item Number");
            c.setCellStyle(cs);

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

            c = row.createCell(2);
            c.setCellValue("Price");
            c.setCellStyle(cs);

            sheet1.setColumnWidth(0, (15 * 500));
            sheet1.setColumnWidth(1, (15 * 500));
            sheet1.setColumnWidth(2, (15 * 500));

            // Create a path where we will place our List of objects on external storage 
            File file = new File(context.getExternalFilesDir(null), fileName); 
            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; 
        } 

        private static void readExcelFile(Context context, String filename) { 

            if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) 
            { 
                Log.w("FileUtils", "Storage not available or read only"); 
                return; 
            } 

            try{
                // Creating Input Stream 
                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();

                while(rowIter.hasNext()){
                    HSSFRow myRow = (HSSFRow) rowIter.next();
                    Iterator<Cell> cellIter = myRow.cellIterator();
                    while(cellIter.hasNext()){
                        HSSFCell myCell = (HSSFCell) cellIter.next();
                        Log.w("FileUtils", "Cell Value: " +  myCell.toString());
                        Toast.makeText(context, "cell Value: " + myCell.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
            }catch (Exception e){e.printStackTrace(); }

            return;
        } 

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

2 个答案:

答案 0 :(得分:0)

您需要导入 Apache POI库

可以找到使用此库的教程here

浏览官方网站了解最新的library版本,并查看此answer

Jar下载URL点击HTTP下的任何镜像链接。

答案 1 :(得分:0)

公共类AndroidReadExcelActivity

以您的档案JAVA命名,例如mainActivity