数据
一共17种语言的星期和月份词
代码
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class FileUtil {
public static void main(String[] args) {
readExcel(new String[]{"NO", "EN", "BG", "CZ", "DE", "ES", "FI", "FR", "HU", "IT", "LT", "NL", "PL", "PT", "RO", "RU", "SL", "TR"});
}
/**
* 读取excel (xls和xlsx)
*
* @return
*/
public static List<Map<String, String>> readExcel(String columns[]) {
File file = new File("/work/02Paccar/date.xlsx");
String filePath = file.getAbsolutePath();
Sheet sheet = null;
Row row = null;
Row rowHeader = null;
List<Map<String, String>> list = null;
String cellData = null;
Workbook wb = null;
if (filePath == null) {
return null;
}
String extString = filePath.substring(filePath.lastIndexOf("."));
InputStream is = null;
try {
is = new FileInputStream(filePath);
if (".xls".equals(extString)) {
wb = new HSSFWorkbook(is);
} else if (".xlsx".equals(extString)) {
wb = new XSSFWorkbook(is);
} else {
wb = null;
}
if (wb != null) {
// 用来存放表中数据
list = new ArrayList<Map<String, String>>();
// 获取第一个sheet
sheet = wb.getSheetAt(0);
// 获取第一行
rowHeader = sheet.getRow(0);
row = sheet.getRow(0);
// 获取最大行数
int rownum = sheet.getPhysicalNumberOfRows();
// 获取最大列数
int colnum = row.getPhysicalNumberOfCells();
String data[][] = new String[rownum][colnum];
for (int i = 0; i < rownum; i++) {
row = sheet.getRow(i);
for (int j = 0; j < colnum; j++) {
data[i][j] = row.getCell(j).getStringCellValue();
}
}
for (int i = 1; i < colnum; i++) {
System.out.println(data[0][i]);
for (int j = 1; j < rownum; j++) {
System.out.println("<string name=\"" + data[j][0] + "\">" + data[j][i] + "</string>");
}
System.out.println();
System.out.println();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
}
结果
jar包下载:https://github.com/andruhon/android5xlsx
参考文章:https://blog.csdn.net/u014743890/article/details/91045033