一、使用jxl 读取excel文件
1.build.gradle的dependencies中加入
implementation 'net.sourceforge.jexcelapi:jxl:2.6.12'
2.将xls文件复制到assets文件夹
3.读取文件内容:
public static List<ContentBean> readExcelToAssets(Context context) {
List<ContentBean> contentBeans = new ArrayList<>();
try {
InputStream is = context.getAssets().open("App.xls");
Workbook book = Workbook.getWorkbook(is);
book.getNumberOfSheets();
Sheet sheet = book.getSheet(0);
int Rows = sheet.getRows();
for (int i = 1; i < Rows; ++i) {
String key = (sheet.getCell(0, i)).getContents();
List<String> values = new ArrayList<>();
String value1 = (sheet.getCell(1, i)).getContents();
values.add(value1);
String value2 = (sheet.getCell(2, i)).getContents();
values.add(value2);
String value3 = (sheet.getCell(3, i)).getContents();
values.add(value3);
String value4 = (sheet.getCell(4, i)).getContents();
values.add(value4);
String value5 = (sheet.getCell(5, i)).getContents();
values.add(value5);
contentBeans.add(new ContentBean(key, values));
Log.e("FileUtil", "第" + i + "行数据=\n" + key + "," + value1 + "," + value2 + "," + value3 + "," + value4 + "," + value5);
}
book.close();
} catch (Exception e) {
Log.e("FileUtil", "e" + e);
}
return contentBeans;
}
二、使用XmlPullParser读取xml文件
1.将xml文件复制到assets文件夹
2.读取文件内容:文章来源:https://www.toymoban.com/news/detail-641769.html
/**
* 获取XML中所有的指令信息
* xml文件内容:
* <resources>
* <string name="app_name">APP</string>
* </resources>
*/
public static List<LanguageBean> parseDateSource(Context context) {
List<LanguageBean> languageBeans = new ArrayList<>();
try {
InputStream inputStream = context.getAssets().open("strings.xml");
XmlPullParser parse = Xml.newPullParser();
parse.setInput(inputStream, "utf-8");
int event = parse.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:
Log.e("FileUtil", "parseDateSource 初始化指令集合");
break;
case XmlPullParser.START_TAG:
String keys = parse.getName();
// <string name="app_name">APP</string>
// 读取string标签下的值,key = app_name,value = APP
if (keys.equals("string")) {
String key = parse.getAttributeValue(null, "name");
String value = parse.nextText();
Log.e("FileUtil", "START_TAG " + key + "," + value);
languageBeans.add(new LanguageBean(key, value));
} else {
Log.e("FileUtil", "START_TAG keys " + keys);
}
break;
case XmlPullParser.END_TAG:
Log.e("FileUtil", "END_TAG");
break;
default:
break;
}
event = parse.next();// 进入到下一个元素并触发相应事件
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return languageBeans;
}
完整的FileUtil工具类:文章来源地址https://www.toymoban.com/news/detail-641769.html
package com.file.util.file;
import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.util.Xml;
import androidx.annotation.RequiresApi;
import com.file.util.MainActivity;
import com.file.util.bean.ContentBean;
import com.file.util.bean.LanguageBean;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import jxl.Sheet;
import jxl.Workbook;
/**
* @Author: Jin
* @Description:
* @CreateDate: 2022/11/17 16:34
*/
public class FileUtil {
public static void readExcel(Context context) {
String logFilePath = Environment.getExternalStorageDirectory() + File.separator + "Visitor";
File file = new File(logFilePath, "test.xls");
Log.e("FileUtil", "file=" + file.getAbsolutePath());
try {
InputStream is = new FileInputStream(file);
Workbook book = Workbook.getWorkbook(is);
book.getNumberOfSheets();
Sheet sheet = book.getSheet(0);
int Rows = sheet.getRows();
for (int i = 1; i < Rows; ++i) {
String name = (sheet.getCell(0, i)).getContents();
String department = (sheet.getCell(1, i)).getContents();
String company = (sheet.getCell(2, i)).getContents();
String phone = (sheet.getCell(3, i)).getContents();
Log.e("FileUtil", "第" + i + "行数据=" + name + "," + department + "," + company + "," + phone);
}
book.close();
} catch (Exception e) {
Log.e("FileUtil", "e" + e);
}
}
public static List<ContentBean> readExcelToAssets(Context context) {
List<ContentBean> contentBeans = new ArrayList<>();
try {
InputStream is = context.getAssets().open("App.xls");
Workbook book = Workbook.getWorkbook(is);
book.getNumberOfSheets();
Sheet sheet = book.getSheet(0);
int Rows = sheet.getRows();
for (int i = 1; i < Rows; ++i) {
String key = (sheet.getCell(0, i)).getContents();
List<String> values = new ArrayList<>();
String value1 = (sheet.getCell(1, i)).getContents();
values.add(value1);
String value2 = (sheet.getCell(2, i)).getContents();
values.add(value2);
String value3 = (sheet.getCell(3, i)).getContents();
values.add(value3);
String value4 = (sheet.getCell(4, i)).getContents();
values.add(value4);
String value5 = (sheet.getCell(5, i)).getContents();
values.add(value5);
contentBeans.add(new ContentBean(key, values));
Log.e("FileUtil", "第" + i + "行数据=\n" + key + "," + value1 + "," + value2 + "," + value3 + "," + value4 + "," + value5);
}
book.close();
} catch (Exception e) {
Log.e("FileUtil", "e" + e);
}
return contentBeans;
}
/**
* 获取XML中所有的指令信息
* xml文件内容:
* <resources>
* <string name="app_name">APP</string>
* </resources>
*/
public static List<LanguageBean> parseDateSource(Context context) {
List<LanguageBean> languageBeans = new ArrayList<>();
try {
InputStream inputStream = context.getAssets().open("strings.xml");
XmlPullParser parse = Xml.newPullParser();
parse.setInput(inputStream, "utf-8");
int event = parse.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
switch (event) {
case XmlPullParser.START_DOCUMENT:
Log.e("FileUtil", "parseDateSource 初始化指令集合");
break;
case XmlPullParser.START_TAG:
String keys = parse.getName();
// <string name="app_name">APP</string>
// 读取string标签下的值,key = app_name,value = APP
if (keys.equals("string")) {
String key = parse.getAttributeValue(null, "name");
String value = parse.nextText();
Log.e("FileUtil", "START_TAG " + key + "," + value);
languageBeans.add(new LanguageBean(key, value));
} else {
Log.e("FileUtil", "START_TAG keys " + keys);
}
break;
case XmlPullParser.END_TAG:
Log.e("FileUtil", "END_TAG");
break;
default:
break;
}
event = parse.next();// 进入到下一个元素并触发相应事件
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return languageBeans;
}
public static List<String> makeContent(Context context, int type) {
List<String> contents = new ArrayList<>();
List<LanguageBean> languageBeans = FileUtil.parseDateSource(context);
List<ContentBean> contentBeans = FileUtil.readExcelToAssets(context);
// 寻找语言
for (LanguageBean languageBean : languageBeans) {
for (ContentBean contentBean : contentBeans) {
if (TextUtils.equals(languageBean.getValue(), contentBean.getKey())) {
if (contentBean.getValues().size() > type) {
languageBean.setValue(contentBean.getValues().get(type));
}
break;
}
}
}
// 组装结果
for (LanguageBean languageBean : languageBeans) {
String content = "<string name=\"" + languageBean.getKey() + "\">" + languageBean.getValue() + "</string>";
Log.i("FileUtil", "makeContent " + content);
contents.add(content);
}
return contents;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static void saveXML(Context context, int type) {
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS + "/RedKey/language/");
if (!file.exists()) {
file.mkdirs();
}
BufferedWriter out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file.getAbsoluteFile() + "/" + "language_" + getLanguageName(type) + ".txt", true)));
List<String> contents = makeContent(context, type);
for (String content : contents) {
out.write(content + "\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static String getLanguageName(int type) {
String languageName = "";
switch (type) {
case 0:
languageName = "en";
break;
case 1:
languageName = "ru";
break;
case 2:
languageName = "de";
break;
case 3:
languageName = "fr";
break;
case 4:
languageName = "pl";
break;
}
return languageName;
}
}
到了这里,关于Android 读取excel(支持 xls)和 xml的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!