android excel、txt读写
android中读写excel 需要用到jxl.jar
public class ExcelUtil { /** * getPath * @param context * @param uri * @return */ public static String getPath(Context context, Uri uri) { if ("content".equalsIgnoreCase(uri.getScheme())) { String[] projection = { "_data" }; Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, projection,null, null, null); int column_index = cursor.getColumnIndexOrThrow("_data"); if (cursor.moveToFirst()) { return cursor.getString(column_index); } } catch (Exception e) { // Eat it } } else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } /** * read xls * @param path */ public static List<WorkEntity> readExcelFile(String path) { List<WorkEntity> lists = new ArrayList<WorkEntity>(); try { Workbook book = Workbook.getWorkbook(new File(path)); Sheet sheet = book.getSheet(0); int Rows = sheet.getRows(); int Cols = sheet.getColumns(); for (int i = 0; i < Rows; ++i) { String str = ""; for (int j = 0; j < Cols; ++j) { str += (sheet.getCell(j, i)).getContents()+","; } //去除回车 str = str.replace("\n", ""); String[] ss = str.split(","); if (ss.length >= 2){ //只拿前2列,第一列标签号,第二列扩展号 WorkEntity work = new WorkEntity(); work.biaoqian = ss[0]; work.kuozhan = ss[1]; //未满3位的扩展号前面添零 if(work.kuozhan.length() == 1){ work.kuozhan = "00" + work.kuozhan; }else if(work.kuozhan.length() == 2){ work.kuozhan = "0" + work.kuozhan; } lists.add(work); } } book.close(); } catch (Exception e) { System.out.println(e); } return lists; } /** * read xlsx 这个方法不稳定,会顺序错乱,漏读 * @param path * @return */ public static List<WorkEntity> readXLSX(String path) { List<WorkEntity> lists = new ArrayList<WorkEntity>(); String str = ""; String v = null; boolean flat = false; List<String> ls = new ArrayList<String>(); try { ZipFile xlsxFile = new ZipFile(new File(path)); ZipEntry sharedStringXML = xlsxFile.getEntry("xl/sharedStrings.xml"); if(sharedStringXML != null){ InputStream inputStream = xlsxFile.getInputStream(sharedStringXML); XmlPullParser xmlParser = Xml.newPullParser(); xmlParser.setInput(inputStream, "utf-8"); int evtType = xmlParser.getEventType(); while (evtType != XmlPullParser.END_DOCUMENT) { switch (evtType) { case XmlPullParser.START_TAG: String tag = xmlParser.getName(); //sst|si|t if (tag.equalsIgnoreCase("t")) { ls.add(xmlParser.nextText()); } break; case XmlPullParser.END_TAG: break; default: break; } evtType = xmlParser.next(); } } ZipEntry sheetXML = xlsxFile.getEntry("xl/worksheets/sheet1.xml"); InputStream inputStreamsheet = xlsxFile.getInputStream(sheetXML); XmlPullParser xmlParsersheet = Xml.newPullParser(); xmlParsersheet.setInput(inputStreamsheet, "utf-8"); int evtTypesheet = xmlParsersheet.getEventType(); while (evtTypesheet != XmlPullParser.END_DOCUMENT) { switch (evtTypesheet) { case XmlPullParser.START_TAG: String tag = xmlParsersheet.getName(); if (tag.equalsIgnoreCase("row")) { } else if (tag.equalsIgnoreCase("c")) { String t = xmlParsersheet.getAttributeValue(null, "t"); if (t != null && t.equals("s")) { flat = true; } else { flat = false; } } else if (tag.equalsIgnoreCase("v")) { v = xmlParsersheet.nextText(); if (v != null) { if (flat) { str += ls.get(Integer.parseInt(v)) + ","; } else { str += v + ","; } } } break; case XmlPullParser.END_TAG: if (xmlParsersheet.getName().equalsIgnoreCase("row") && v != null) { // System.out.println("@@@@@@@@@@@@@"+str); String s = str.substring(0,str.lastIndexOf(",")); int index = s.indexOf(","); String kzh = s.substring(index+1); if(kzh.length() == 1){ kzh = "00" + kzh; }else if(kzh.length() == 2){ kzh = "0" + kzh; } WorkEntity work = new WorkEntity(); work.biaoqian = s.substring(0,index); work.kuozhan = kzh; lists.add(work); str = ""; } break; } evtTypesheet = xmlParsersheet.next(); } System.out.println(str); } catch (ZipException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } if (str == null) { str = "解析文件失败"; } return lists; } public static void removeExcel(String fileName){ String path = Environment.getExternalStorageDirectory()+"/tva2020"; File filePath=new File(path+"/"+fileName); if (filePath.exists()){ filePath.delete(); } } public static void toWriteExcel(String fileName,List<Map<Integer, String>> datas){ String path = Environment.getExternalStorageDirectory()+"/tva2020"; File filePath=new File(path); if(!filePath.exists() && !filePath.isDirectory()){ filePath.mkdir(); } try { WritableWorkbook wwb = Workbook.createWorkbook(new File(path+"/"+fileName)); WritableSheet ws = wwb.createSheet("sheet1", 0); for(int i = 0; i < datas.size(); i++){ Map<Integer, String> map = datas.get(i); for (Map.Entry<Integer, String> entry : map.entrySet()) { Label lab = new Label(entry.getKey(), i, entry.getValue()); ws.addCell(lab); } } wwb.write(); wwb.close(); } catch (IOException e) { e.printStackTrace(); } catch (RowsExceededException e) { e.printStackTrace(); } catch (WriteException e) { e.printStackTrace(); } } }
读txt
//读txt文件 public static String readFile(String str_filepath) {// 转码 File file = new File(str_filepath); BufferedReader reader; String text = ""; try { // FileReader f_reader = new FileReader(file); // BufferedReader reader = new BufferedReader(f_reader); FileInputStream fis = new FileInputStream(file); BufferedInputStream in = new BufferedInputStream(fis); in.mark(4); byte[] first3bytes = new byte[3]; in.read(first3bytes);//找到文档的前三个字节并自动判断文档类型。 in.reset(); if (first3bytes[0] == (byte) 0xEF && first3bytes[1] == (byte) 0xBB && first3bytes[2] == (byte) 0xBF) {// utf-8 reader = new BufferedReader(new InputStreamReader(in, "utf-8")); } else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFE) { reader = new BufferedReader( new InputStreamReader(in, "unicode")); } else if (first3bytes[0] == (byte) 0xFE && first3bytes[1] == (byte) 0xFF) { reader = new BufferedReader(new InputStreamReader(in, "utf-16be")); } else if (first3bytes[0] == (byte) 0xFF && first3bytes[1] == (byte) 0xFF) { reader = new BufferedReader(new InputStreamReader(in, "utf-16le")); } else { reader = new BufferedReader(new InputStreamReader(in, "GBK")); } String str = reader.readLine(); while (str != null) { text = text + str + "/n"; str = reader.readLine(); } reader.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return text; }
浙公网安备 33010602011771号