Android applies the method of reading Excel files

  • 2020-11-20 06:14:02
  • OfStack

This article illustrates how Android can be used to read Excel files. To share for your reference, the details are as follows:

ReadExcel. java file:


public class ReadExcel extends Activity { 
 /** Called when the activity is first created. */ 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
  // createExcel(); 
  // readExcel(); 
  writeExcel("mnt/sdcard/test.xls"); 
 } 
 public void readExcel() { 
  try { 
   /** 
    *  Follow-up consideration , Such as Excel Inside the image and other data types read  
    **/ 
   InputStream is = new FileInputStream("mnt/sdcard/test.xls"); 
   Workbook book = Workbook 
     .getWorkbook(new File("mnt/sdcard/test.xls")); 
   book.getNumberOfSheets(); 
   //  For the first 1 Three worksheet objects  
   Sheet sheet = book.getSheet(0); 
   int Rows = sheet.getRows(); 
   int Cols = sheet.getColumns(); 
   System.out.println(" Name of the current worksheet :" + sheet.getName()); 
   System.out.println(" Total number of rows :" + Rows); 
   System.out.println(" The total number of columns :" + Cols); 
   for (int i = 0; i < Cols; ++i) { 
    for (int j = 0; j < Rows; ++j) { 
     // getCell(Col,Row) Gets the value of the cell  
     System.out 
       .print((sheet.getCell(i, j)).getContents() + "\t"); 
    } 
    System.out.print("\n"); 
   } 
   //  Get the first 1 The column first 1 Row cell  
   Cell cell1 = sheet.getCell(0, 0); 
   String result = cell1.getContents(); 
   System.out.println(result); 
   book.close(); 
  } catch (Exception e) { 
   System.out.println(e); 
  } 
 } 
 public void createExcel() { 
  try { 
   //  Create or open Excel file  
   WritableWorkbook book = Workbook.createWorkbook(new File( 
     "mnt/sdcard/test.xls")); 
   //  The generation name is "control" 1 Page "worksheet , parameter 0 That means this is number one 1 page  
   WritableSheet sheet1 = book.createSheet(" The first 1 page ", 0); 
   WritableSheet sheet2 = book.createSheet(" The first 3 page ", 2); 
   //  in Label Object in its constructor , The cell position is number one 1 The column first 1 line (0,0) And the cell content is test 
   Label label = new Label(0, 0, "test"); 
   //  Adds the defined cells to the worksheet  
   sheet1.addCell(label); 
   /* 
    *  generate 1 A number of cells . You must use the Number The full package path , Otherwise there is a grammatical ambiguity  
    */ 
   jxl.write.Number number = new jxl.write.Number(1, 0, 555.12541); 
   sheet2.addCell(number); 
   //  Write data and close the file  
   book.write(); 
   book.close(); 
  } catch (Exception e) { 
   System.out.println(e); 
  } 
 } 
 /** 
  * jxl Do not provide modification to existing tables , Here by 1 Here's a little way to do it , Not suitable for large data updates !  This is updated by overwriting the original file . 
  * 
  * @param filePath 
  */ 
 public void updateExcel(String filePath) { 
  try { 
   Workbook rwb = Workbook.getWorkbook(new File(filePath)); 
   WritableWorkbook wwb = Workbook.createWorkbook(new File( 
     "d:/new.xls"), rwb);// copy 
   WritableSheet ws = wwb.getSheet(0); 
   WritableCell wc = ws.getWritableCell(0, 0); 
   //  Determine the type of cell , Make the corresponding transformation  
   Label label = (Label) wc; 
   label.setString("The value has been modified"); 
   wwb.write(); 
   wwb.close(); 
   rwb.close(); 
  } catch (Exception e) { 
   e.printStackTrace(); 
  } 
 } 
 public static void writeExcel(String filePath) { 
  try { 
   //  Create a workbook  
   WritableWorkbook wwb = Workbook.createWorkbook(new File(filePath)); 
   //  Create worksheets  
   WritableSheet ws = wwb.createSheet("Sheet1", 0); 
   //  Add label text  
   // Random rnd = new Random((new Date()).getTime()); 
   // int forNumber = rnd.nextInt(100); 
   // Label label = new Label(0, 0, "test"); 
   // for (int i = 0; i < 3; i++) { 
   // ws.addCell(label); 
   // ws.addCell(new jxl.write.Number(rnd.nextInt(50), rnd 
   // .nextInt(50), rnd.nextInt(1000))); 
   // } 
   //  Add images ( Note here jxl Temporary support only png Formatted picture ) 
   // 0,1 Represent the x,y 2,5 The number of cells representing the width and height  
   ws.addImage(new WritableImage(5, 5, 2, 5, new File( 
     "mnt/sdcard/nb.png"))); 
   wwb.write(); 
   wwb.close(); 
  } catch (Exception e) { 
   System.out.println(e.toString()); 
  }
 }
}

jxl.7z click here to download.

I hope this article has been helpful in Android programming.


Related articles: