GVKun编程网logo

如何从XLS(Excel)文件中读取数据[Java,Android](读取excel文件 java)

10

这篇文章主要围绕如何从XLS和Excel文件中读取数据[Java,Android]展开,旨在为您提供一份详细的参考资料。我们将全面介绍如何从XLS的优缺点,解答Excel文件中读取数据[Java,An

这篇文章主要围绕如何从XLSExcel文件中读取数据[Java,Android]展开,旨在为您提供一份详细的参考资料。我们将全面介绍如何从XLS的优缺点,解答Excel文件中读取数据[Java,Android]的相关问题,同时也会为您带来android – 从Arduino UNO R3套件中读取数据、android – 从文本文件中读取数据并将其显示在textview上、android – 如何从.db.crypt文件中读取数据?、android – 如何从本地json文件中获取数据?的实用方法。

本文目录一览:

如何从XLS(Excel)文件中读取数据[Java,Android](读取excel文件 java)

如何从XLS(Excel)文件中读取数据[Java,Android](读取excel文件 java)

我已经搜索了stackoverflow,但是没有找到明确的答案。如何将数据从XLS文件的特定行和列读取到我的Android应用程序?如何读取XLS文件?我不想将其转换为CSV,因为在尝试将其转换时出现错误。

也许我可以使用此http://www.andykhan.com/jexcelapi/tutorial.html#reading,但我什至不知道如何将其导入到我的项目中。请帮忙。

答案1

小编典典

嗨,您只需要包括一个外部jxl
jar即可,您可以通过同一教程来帮助您理解读取excel文件的过程..供您参考,我正在粘贴一些参考资料。读取第一张excel表格并创建结果集的代码。

    public List<String> read(String key) throws IOException  {    List<String> resultSet = new ArrayList<String>();    File inputWorkbook = new File(inputFile);    if(inputWorkbook.exists()){        Workbook w;        try {            w = Workbook.getWorkbook(inputWorkbook);            // Get the first sheet            Sheet sheet = w.getSheet(0);            // Loop over column and lines            for (int j = 0; j < sheet.getRows(); j++) {                Cell cell = sheet.getCell(0, j);                if(cell.getContents().equalsIgnoreCase(key)){                    for (int i = 0; i < sheet.getColumns(); i++) {                        Cell cel = sheet.getCell(i, j);                        resultSet.add(cel.getContents());                    }                }                continue;            }        } catch (BiffException e) {            e.printStackTrace();        } catch (Exception e) {            e.printStackTrace();        }    }    else    {        resultSet.add("File not found..!");    }    if(resultSet.size()==0){        resultSet.add("Data not found..!");    }    return resultSet;}

android – 从Arduino UNO R3套件中读取数据

android – 从Arduino UNO R3套件中读取数据

我正在尝试读取我已经存储在Arduino工具包中的数据,我正在使用physicaloid库来实现这一目标.我使用Arduino自己提供的B型USB电缆并使用tera Term将它连接到我的电脑,测试了套件(读数据).在键盘上按“@”后,数据开始传输(具体到我们的实现).

但当我连接我的Android平板电脑并通过physicaloid使用测试项目来打开设备并开始通信时,每次我点击“打开”它会显示一个Toast说它无法打开.我每次提示时都允许访问USB设备.这是我为读取数据而创建的示例程序:

if(mPhysicaloid.open()){

        Toast.makeText(getBaseContext(),"communicating",Toast.LENGTH_SHORT).show();
        String signalToStart = new String("@");
        byte[] bufToWrite = signalToStart.getBytes();
        mPhysicaloid.write(bufToWrite,bufToWrite.length);

        byte[] buf = new byte[255];
        mPhysicaloid.read(buf);
        String data = new String(buf);
        tvResult.setText(data);
        mPhysicaloid.close();

    }
    else 
        Toast.makeText(getBaseContext(),"no communication with device",Toast.LENGTH_LONG).show();

现在这就是我想知道的来自Arduino USB线的数据:它是采用RS232格式的Android设备无法理解(我不知道,我可能会在这里通过询问这些数据犯错误格式)或者是否适合Android设备理解的USB数据格式?请帮忙,我整天搜索过这个.我该怎么做才能打开设备并进行通信?

解决方法

我终于明白了从串口USB设备读取数据的想法.所以我想我会分享它:

首先,连接所有USB设备(如果不止一个)并获得合适的接口并搜索要与之通信的端点.在初始化USB设备时,请确保考虑您真正想要与之通信的USB设备.您可以通过考虑产品ID和供应商ID来做到这一点.
做上述代码..

private boolean searchEndPoint() {

    usbInterface = null;//class level variables,declare these.
    endpointOut = null;
    endpointIn = null;

    Log.d("USB","Searching device and endpoints...");

    if (device == null) {
        usbDevices = usbManager.getDeviceList();
        Iterator<UsbDevice> deviceIterator = usbDevices.values().iterator();

        while (deviceIterator.hasNext()) {
            UsbDevice tempDevice = deviceIterator.next();

            /**Search device for targetvendorID(class level variables[vendorId = SOME_NUMBER and productId=SOME_NUMBER] which u can find) and targetProductID.*/
            if (tempDevice .getvendorId() == vendorId) {
                if (tempDevice .getProductId() == productId) {
                    device = tempDevice ;
                }                                                                                                                                                                                                                                                                                                                                                                
            }
        }
    }

    if (device == null){ 
        Log.d("USB","The device with specified vendorId and ProductId not found");
        return false;
    }

    else
        Log.d("USB","device found");

    /**Search for UsbInterface with Endpoint of USB_ENDPOINT_XFER_BULK,*and direction USB_DIR_OUT and USB_DIR_IN
     */
    try{
        for (int i = 0; i < device.getInterfaceCount(); i++) {
            UsbInterface usbif = device.getInterface(i);

            UsbEndpoint tOut = null;
            UsbEndpoint tIn = null;

            int tEndpointCnt = usbif.getEndpointCount();
            if (tEndpointCnt >= 2) {
                for (int j = 0; j < tEndpointCnt; j++) {
                    if (usbif.getEndpoint(j).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                        if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_OUT) {
                            tOut = usbif.getEndpoint(j);
                        } else if (usbif.getEndpoint(j).getDirection() == UsbConstants.USB_DIR_IN) {
                            tIn = usbif.getEndpoint(j);
                        }
                    }
                }

                if (tOut != null && tIn != null) {
                    /** This interface have both USB_DIR_OUT
                     * And USB_DIR_IN of USB_ENDPOINT_XFER_BULK
                     */
                    usbInterface = usbif;
                    endpointOut = tOut;
                    endpointIn = tIn;
                }
            }

        }

        if (usbInterface == null) {
            Log.d("USB","No suitable interface found!");
            return false;
        } else {
            Log.d("USB","Suitable interface found!");
            return true;
        }



    }catch(Exception ex){

        ex.printstacktrace();
        return false;
    }
}

现在,您可以使用设备,USB接口和端点进行通信.现在是时候在Android设备和USB设备之间建立连接了.
下面是这个代码(并检查连接是否正常并进行通信):

private boolean checkUsbCOMM() {

    /**Value for setting request,on the USB connection.*/
    final int RQSID_SET_CONTROL_LINE_STATE = 0x22;

    boolean success = false;

    Log.d("USB","Checking USB Device for communication: ");
    try{

        Boolean permitToRead = SUSBS_usbManager.hasPermission(SUSBS_device);

        if (permitToRead) {
             //class level variable(connection,usbManager : declare it)
            connection = usbManager.openDevice(device);

            if (connection != null) {
                connection.claimInterface(usbInterface,true);

                int usbResult;

                usbResult = connection.controlTransfer(0x21,//requestType
                        RQSID_SET_CONTROL_LINE_STATE,//SET_CONTROL_LINE_STATE(request)
                        0,//value
                        0,//index
                        null,//buffer
                        0,//length
                        500);                                       //timeout = 500ms


                Log.i("USB","controlTransfer(SET_CONTROL_LINE_STATE)[must be 0 or greater than 0]: "+usbResult);

                if(usbResult >= 0)
                    success = true;
                else 
                    success = false;

            }

        }

        else {
            /**If permission is not there then ask for permission*/
            usbManager.requestPermission(device,mPermissionIntent);
            Log.d("USB","Requesting Permission to access USB Device: ");

        }

        return success;

    }catch(Exception ex){

        ex.printstacktrace();
        return false;
    }

}

瞧,USB设备现在能够进行通信.所以让我们使用一个单独的线程来阅读:

if(device!=null){
Thread readerThread = new Thread(){

                public void run(){

                    int usbResult = -1000;
                    int totalBytes = 0;

                    StringBuffer sb = new StringBuffer();
                    String usbReadResult=null;
                    byte[] bytesIn ;

                    try {

                        while(true){
                            /**Reading data until there is no more data to receive from USB device.*/
                            bytesIn = new byte[endpointIn.getMaxPacketSize()];
                            usbResult = connection.bulkTransfer(endpointIn,bytesIn,bytesIn.length,500);

                            /**The data read during each bulk transfer is logged*/
                            Log.i("USB","data-length/read: "+usbResult);

                            /**The USB result is negative when there is failure in reading or
                             *  when there is no more data to be read[That is : 
                             *  The USB device stops transmitting data]*/
                            if(usbResult < 0){
                                Log.d("USB","Breaking out from while,usb result is -1");
                                break;

                            }

                            /**Total bytes read from the USB device*/
                            totalBytes = totalBytes+usbResult;
                            Log.i("USB","TotalBytes read: "+totalBytes);

                            for(byte b: bytesIn){

                                if(b == 0 )
                                    break;
                                else{
                                    sb.append((char) b);
                                }


                            }

                        }

                        /**Converting byte data into characters*/
                        usbReadResult = new String(sb);
                        Log.d("USB","The result: "+usbReadResult);
                        //usbResult holds the data read.

                    } catch (Exception ex) {

                        ex.printstacktrace();
                    }


                }

            };

            /**Starting thread to read data from USB.*/
            SUSBS_readerThread.start();
            SUSBS_readerThread.join();


        }

要获得权限,请确保添加PendingIntent以及向清单添加权限.

AndroidManifest:< uses-feature android:name =“android.hardware.usb.host”/>

的PendingIntent:

private PendingIntent mPermissionIntent;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
mPermissionIntent = PendingIntent.getbroadcast(MainActivity.this,new Intent(ACTION_USB_PERMISSION),0);

    /**Setting up the broadcast receiver to request a permission to allow the APP to access the USB device*/
    IntentFilter filterPermission = new IntentFilter(ACTION_USB_PERMISSION);
    registerReceiver(mUsbReceiver,filterPermission);

android – 从文本文件中读取数据并将其显示在textview上

android – 从文本文件中读取数据并将其显示在textview上

我试图从我的原始文件夹中的文本文件“temp.txt”中读取数据,并在单击按钮“按钮”时在文本视图“text”上显示文件的内容,但我的应用程序崩溃时,我很有可能以错误的方式做这件事,因为我是android和java编程的新手.我在这里粘贴代码,任何帮助将不胜感激

案例R.id.b:

        InputStream is = getResources().openRawResource(R.raw.temp);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        try {
            string = br.readLine();
            while(string != null){
                st = string;
            }
            text.setText(st);

        } catch (IOException e) {
            // Todo Auto-generated catch block
            e.printstacktrace();
        }
        break;

“st”和“string”都是字符串变量.如果有人能指出另一种简单的方法来做同样的事情,我会很高兴的.

最佳答案
更改为以下内容:

InputStream is = getResources().openRawResource(R.raw.temp);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line; 
String entireFile = "";
try {
    while((line = br.readLine()) != null) { // <--------- place readLine() inside loop
        entireFile += (line + "\n"); // <---------- add each line to entireFile
    }
} catch (IOException e) {
    // Todo Auto-generated catch block
    e.printstacktrace();
}
text.setText(entireFile); // <------- assign entireFile to TextView
break;

android – 如何从.db.crypt文件中读取数据?

android – 如何从.db.crypt文件中读取数据?

我想从.db.crypt文件中读取数据作为字符串是否有任何lib或方法来解密此文件中的数据?

如果是,那么请指点我或提供任何样品.

解决方法

我认为.db.crypt只是某人定义的后缀.例如,我可以创建一个文本文件并将其命名为abc.db.crypt.所以你只要知道后缀就不能做任何事情.

但有时后缀是找到解决方案的线索.我想这个文件是一个先加密的数据库文件.所以你需要做的是找到加密方法(可能是DES或者作者刚才定义的算法),将文件解密为数据库文件(xxx.db),然后使用sqlite3从中获取数据.

android – 如何从本地json文件中获取数据?

android – 如何从本地json文件中获取数据?

我在本地创建了一个json文件.我可以在数据> data> com.example.storage> files> filename.json中查看.使用这个,我想在本地获取所有文本值&下载所有图像并将其保存在图像URL中的SD卡中,该URL位于json文件中.在离线模式下我自己可以加载json文件中的所有文本值以及SD卡中的图像.

如果这个想法有效,请告诉我.如果没有请建议其他方式来做到这一点.

任务是使用我想要获取数据的本地JSON文件.怎么做?(我为每个数据使用单独的ID).

示例JSON文件是.

{"ImageData":[{"ImageId":"12","ImageName":"Img1","ImageDesc":"Img1 Img1Img1 ","ImageUrl":"img_url/pro1.jpg"},{"ImageId":"13","ImageName":"Img13","ImageDesc":"Img13 Img13 Img13Img13 ","ImageUrl":"img_url/pro2.jpg"},{"ImageId":"14","ImageName":"Img14","ImageDesc":"Img14 Img14 Img14 Img14 Img14 ","ImageUrl":"img_url/pro3.jpg.jpg"},]}

解决方法:

对的,这是可能的.下次提供一些代码,以便我们有一些工作,没有太多人可以做到解决这个问题.

为了帮助您入门,请查看有关JSON的本教程:
http://www.vogella.com/tutorials/AndroidJSON/article.html

这个问题将帮助您在本地阅读文件:
How can I read a text file in Android?

当您将文件作为字符串获取时,您可以创建JSONObject并解析URL并检索图像.

今天关于如何从XLSExcel文件中读取数据[Java,Android]的分享就到这里,希望大家有所收获,若想了解更多关于android – 从Arduino UNO R3套件中读取数据、android – 从文本文件中读取数据并将其显示在textview上、android – 如何从.db.crypt文件中读取数据?、android – 如何从本地json文件中获取数据?等相关知识,可以在本站进行查询。

本文标签: