Using jxl to read data in excel in java

package bboss;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
/ **
 *
 * @author llh
 *
 */
public class ExcelReadSOAP {
    /**
     * Use jxl to read data in excel
     *
     * @param args
     * @throws IOException
     * @throws BiffException
     */
    public static String ExcelReadSOAP(String code) throws IOException, BiffException {
        int lie = 0;
        String return = null ;
        System.out.println( "Project path" + System.getProperty("user.dir" ));
        File file = new File(System.getProperty("user.dir") + "\\case\\编码大全.xls");
        System.out.println( "File path: " + file + "\n" + "File absolute path: " + file.getAbsolutePath());
         // Read xls file stream 
        InputStream is = new FileInputStream(file.getAbsolutePath( ));
         // Use jxl 
        Workbook rwb = Workbook.getWorkbook(is);
         // Get the number of sheets in the current excel 
        Sheet[] sheets = rwb.getSheets();
         // Get the number of sheets 
        int pages = sheets.length;
        System.out.println("表数:" + pages);
        for (int i = 0; i < pages; i++) {
            Sheet sheet = sheets[i];
             // how many columns 
            int cols = sheet.getColumns();
            System.out.println( "has" + cols + "columns" );
             // how many rows 
            int rows = sheet.getRows();
            System.out.println( "has" + rows + "rows" );
             // column loop 
            for ( int j = 0; j < cols; j++ ) {
                 // row loop 
                for ( int k = 0 ; k <rows ; k++ ){
                     // Positioning coordinates, starting from (0, 0) 
                    Cell excelRows = sheet.getCell(j, k);
                     // Get the coordinate value 
                    String e = excelRows.getContents();
                     // If the corresponding transaction code is found, Save the column value 
                    if (code.equals(e)){
                        lie = j;
                    }
                }
            }
            // Take out the header information of the corresponding transaction code and return return 
            = sheet.getCell(lie,0 ).getContents();
             if (retur== null ){
                System.out.println( "------------The corresponding business was not found in the corresponding agreement template -----------" );
            }
        }
        return retur;

    }

}

 

In the project, we often use excel to save some data, so in the project, reading the content in excel has also become a technology that we must often use, make a record here, in order to facilitate your viewing at any time, In order to help everyone who needs this method to learn from.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324712255&siteId=291194637