xml,Excel文件的读取
发布时间:2020-12-16 05:38:28 所属栏目:百科 来源:网络整理
导读:*/ public HotArea readDoc (InputStream inputStream) { Document doc = null ; HotArea root = new HotArea(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { builder = factory.newDocumentBu
*/
public HotArea readDoc(InputStream inputStream) {
Document doc = null;
HotArea root = new HotArea();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
doc = builder.parse(inputStream);
NodeList nodeList = doc.getChildNodes();
if (nodeList != null && nodeList.getLength() == 1) {
Node node = nodeList.item(0);
root = readToPlace(null,node);
}
} catch (Exception e) {
LogUtils.e(TAG,e.getMessage());
}
return root;
}
public HotArea readToPlace(HotArea placeNode,Node node) {
HotArea hNode = placeNode;
if (hNode == null) {
hNode = new HotArea();
}
Element element = (Element) node;
NamedNodeMap attrs = element.getAttributes();
int len = attrs.getLength();
for (int i = 0; i < len; ++i) {
Node n = attrs.item(i);
String name = n.getNodeName();
String value = n.getNodeValue();
if ("areaId".equals(name)) {
hNode.setAreaId(value);
} else if ("areaTitle".equals(name)) {
hNode.setAreaTitle(value);
} else if ("pts".equals(name)) {
hNode.setPts(value,",");
} else if ("desc".equals(name)) {
hNode.setDesc(value);
}
}
NodeList nodeList = element.getChildNodes();
int jLen = nodeList.getLength();
for (int j = 0; j < jLen; ++j) {
Node n = nodeList.item(j);
if (n instanceof Element) {
HotArea h = new HotArea();
hNode.getAreas().add(h);
readToPlace(h,n);
}
}
return hNode;
}
参考连接这里写链接内容 Excel表的读取 jxl生成一个Excel表 {
Workbook oldWwb = Workbook.getWorkbook(excelFile);
wwb = Workbook.createWorkbook(excelFile,oldWwb);
WritableSheet ws = wwb.getSheet(0);
// 当前行数
int row = ws.getRows();
Label lab1 = new Label(0,row,args[0] + "");
Label lab2 = new Label(1,args[1] + "");
Label lab3 = new Label(2,args[2] + "");
Label lab4 = new Label(3,args[3] + "");
ws.addCell(lab1);
ws.addCell(lab2);
ws.addCell(lab3);
ws.addCell(lab4);
// 从内存中写入文件中,只能刷一次.
wwb.write();
wwb.close();
Toast.makeText(activity,"保存成功",Toast.LENGTH_SHORT).show();
}
读取Excel表的数据 /** * 获取 excel 表格中的数据,不能在主线程中调用 * * @param xlsName excel 表格的名称 * @param index 第几张表格中的数据 */
private ArrayList<CountryModel> getXlsData(String xlsName,int index) {
ArrayList<CountryModel> countryList = new ArrayList<CountryModel>();
AssetManager assetManager = getAssets();
try {
Workbook workbook = Workbook.getWorkbook(assetManager.open(xlsName));
Sheet sheet = workbook.getSheet(index);
int sheetNum = workbook.getNumberOfSheets();
int sheetRows = sheet.getRows();
int sheetColumns = sheet.getColumns();
Log.d(TAG,"the num of sheets is " + sheetNum);
Log.d(TAG,"the name of sheet is " + sheet.getName());
Log.d(TAG,"total rows is 行=" + sheetRows);
Log.d(TAG,"total cols is 列=" + sheetColumns);
for (int i = 0; i < sheetRows; i++) {
CountryModel countryModel = new CountryModel();
countryModel.setChinaName(sheet.getCell(0,i).getContents());
countryModel.setEnglishName(sheet.getCell(1,i).getContents());
countryModel.setAreaNumber(sheet.getCell(2,i).getContents());
countryList.add(countryModel);
}
workbook.close();
} catch (Exception e) {
Log.e(TAG,"read error=" + e,e);
}
return countryList;
}
//在异步方法中 调用
private class ExcelDataLoader extends AsyncTask<String,Void,ArrayList<CountryModel>> {
@Override
protected void onPreExecute() {
progressDialog.setMessage("加载中,请稍后......");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
@Override
protected ArrayList<CountryModel> doInBackground(String... params) {
return getXlsData(params[0],0);
}
@Override
protected void onPostExecute(ArrayList<CountryModel> countryModels) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
if(countryModels != null && countryModels.size()>0){
//存在数据
sortByName(countryModels);
setupData(countryModels);
}else {
//加载失败
}
}
}
读取的地址 裁剪照片*/
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri,"image/*");
// crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
intent.putExtra("crop","true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX",1);
intent.putExtra("aspectY",1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX",300);
intent.putExtra("outputY",300);
intent.putExtra("return-data",true);
startActivityForResult(intent,REQUESTCODE_CUTTING);
}
文字识别 ocr(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |