Android Create and read Excel

Source: Internet
Author: User
<span id="Label3"></p><p><p><span lang="zh-CN"><span lang="en-US"> <span lang="zh-CN">There are two ways to manipulate Excel in the mainstream, one is through the <span lang="en-US">poi <span lang="zh-CN">package, and the other through the <span lang="en-US">JXL <span lang="zh-CN">package. Here I mainly explain through the <span lang="en-US">JXL <span lang="zh-CN">package to read and write <span lang="en-US">Excel<span lang="zh-CN">. </span></span></span></span></span></span></span></span></span></span></span></p></p><p><p><span lang="zh-CN">first, you need to import a <span lang="en-US">Jxl.jar <span lang="zh-CN">package. </span></span></span></p></p><p><p>: http://www.andykhan.com/jexcelapi/download.html</p></p><p><p><span lang="zh-CN">The downloaded file will contain <span lang="en-US">the JXL <span lang="zh-CN">related <span lang="en-US">APIs<span lang="zh-CN">. </span></span></span></span></span></p></p><p><p>Directly below the Code:</p></p><p lang="en-US"><p lang="en-US">Writeexcel.java</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 1 Import java.io.File; 2 3 Import java.io.IOException; 4 5 Import java.util.Locale; 6 7 8 9 Import Jxl. cellview; JXL Import. Workbook; Import Jxl. workbooksettings; Jxl.format.UnderlineStyle import; Jxl.write.Formula import; Import jxl.write.Label; Import jxl.write.Number; Jxl.write.WritableCellFormat import; Import jxl.write.WritableFont; Jxl.write.WritableSheet import; Jxl.write.WritableWorkbook import; Import jxl.write.WriteException; Import jxl.write.biff.RowsExceededException; The writeexcel of the public Class--------writablecellformat timesboldunderline; Ivate Writablecellformat times; inputfile, Private String; Setoutputfile public void (String Inputfile) {this.inputfile = inputfile; 52 53} Throws ioexception, writeexception {$-a file File= new File (inputfile); Workbooksettings wbsettings = new Workbooksettings (); Wbsettings.setlocale (new Locale ("en", "en")); Writableworkbook workbook = Workbook.createworkbook (file, wbsettings); Workbook.createsheet ("report", 0); Writablesheet ExcelSheet = Workbook.getsheet (0); Createlabel (excelsheet); Createcontent (excelsheet); The Workbook.write of Bayi (); Workbook.close (); (writablesheet Sheet) throws WriteException {createlabel ($) 94 Writablefont times10pt = new Writablefont (writablefont.times, 10); 98: Writablecellformat (times10pt), 101 102 103 Times.setwrap (true); 104 106 107 Writablefont Times10ptboldunderline = new Writablefont (writablefont.times, ten, writablefont.bold, false,108 109 underlinestyle.single); 111 timesboldunderline = new Writablecellformat (times10ptboldunde rline); 113 Timesboldunderline.setwrap (true); 117 118 119 Cellview CV = new Cellview (); 121 Cv.setformat (times), 122 123 Cv.setformat (timesboldunderline); 124 cv.setautosize (tru e); 126 127 129 131 addcaption (sheet, 0, 0, "Header 1"); 133 addcaption (sheet, 1, 0, "Th IS is another header "); 134 135 136 137 138 139}140 141 142 143 private void Createcontent (writablesheet Sheet) Throws writeexception,144 145 rowsexceededexception {146 147 148 149 for (int i = 1; I < 10 ; I++) {151 153 addnumber (sheet, 0, i, i + ten); 154 155 156 157 Addnumb Er (sheet, 1, i, i * i); 158 159}160 161 162 163 StringBuffer buf = new StringBuffer (); 164 165 buf . Append ("SUM (a2:a10)"); 166 167 Formula F = new Formula (0, ten, buf.tostring ()); 168 169 Sheet.addcell (f); 171 buf = new StringBuffer (); 172 173 buf.append ("SUM (b2:b10)"), 174 175 f = new Formula (1, buf.tostring ()); 176 177 Sheet.addcell (f); 178 179 181 for (int i = n; i < i++) {182 183 AddLabel (sheet, 0, i, "boring text" + i); 184 185 AddLabel (sheet, 1, i, "another text"); 186 187}188 189}190 191 19 2 193 private void addcaption (writablesheet sheet, int column, int row, String S) 194 195 throws Rowsexceed edexception, writeexception {196 197 label label;198 199 label = new label (column, row, s, Timesboldunderl ine); 201 Sheet.addcell (label) 202 203}204 205 206 207 private void Addnumber (writablesheet sheet, in T column, int row,208 209 integer integer) throws writeexception, rowsexceededexception {210 21 1 Number number;212 213 Number = new number (column, row, integer, times) 214 215 Sheet.addcell (number); 216 217}218 21 9 221 private void AddLabel (writablesheet sheet, int column, int row, String S) 222 223 throws Writeex ception, rowsexceededexception {224 225 label label;226 227 label = new label (column, row, s, times); 228 2 Sheet.addcell (label); 230 231}232 233 234 235}</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>Readexcel.java</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre> 1 Import java.io.File; 2 Import java.io.IOException; 3 4 Import Jxl. Cell; 5 Import Jxl. celltype; 6 Import Jxl. Sheet; 7 Import Jxl. Workbook; 8 Import jxl.read.biff.BiffException; 9 public class Readexcel {one private string inputfile;12 public void Setinputfile (String Inputfile) {14 This.inputfile = Inputfile;15}16-public void read () throws IOException {$ File Inputworkbook = n EW File (inputfile); Workbook w;20 Try {w = Workbook.getworkbook (inputworkbook); 22 Sheet Sheet = W.getsheet (0), for (int j = 0; J < sheet.getcolumns (), J + +) {26 for (int i = 0; i < sheet.getrows (); i++) {cell cell = Sheet.getcell (j, i); 28 Celltype type = Cell.gettype (); if (type = = Celltype.label) {30 System.out.println ("I got a label" + cell.getcontents());}33 if (type = = Celltype.number) {system.out. Println ("I got a number" + cell.getcontents ()); 37}38 39 }40}41} catch (biffexception e) {e.printstacktrace (); 43}44}45 46 }</pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p><span lang="zh-CN"><span lang="en-US"> <span lang="zh-CN">The code for</span> dispatching Writeexcel:</span></span></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1 writeexcel writeexcel=new writeexcel (); 2 3 writeexcel.setoutputfile ("/sdcard/test.xls"); 4 5 try {6 7 writeexcel.write (); 8 9 } catch (ioexception e) {ten e.printstacktrace (); 12 (writeexception e) { e.printstacktrace (); </pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><p><p></p></p><p><p><span lang="zh-CN"><span lang="en-US"> <span lang="zh-CN">The <span lang="en-US"> <span lang="zh-CN"> <span lang="en-US">test.xls <span lang="zh-CN">file</span> </span> is generated</span> in the Android virtual machine</span> after Writeexcel is</span> </span> executed</span></p></p><p><p></p></p><p><p></p></p><p><p><span lang="zh-CN"><span lang="en-US"> <span lang="zh-CN">The code for</span> dispatching Readexcel:</span></span></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1 readexcel readexcel=new Readexcel (); 2 3 readexcel.setinputfile ("/sdcard/test.xls"); 4 5 try {6 7 readexcel.read (); 8 9 } catch (ioexception e) {ten e.printstacktrace (); }14 15 </pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>well, Android's ability to read and write Excel is basically implemented.</p></p><p><p>Android Create and read Excel</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.