如何从 excel 文件 (xlsx) 中读取 2 列并将其作为键=值对写入资源属性
How to read 2 columns from excel file (xlsx) and write it to resource properties as key=value pair
我正在尝试从 excel (xlsx) 文件的给定集生成 messages_[en/da/bg].properties 文件。每个 excel sheet 都有两列作为键值对。我必须读取每个文件并将其写入以生成用于 i18n 的 messages_en.properties 文件。
如果我有 excel sheet 文件名如 locales_ENG.xlsx
、locales_DAN.xlsx
和 locales_BGR.xlsx
那么它应该生成如下文件:
messages_en.properties
messages_da.properties
messages_bg.properties
我建议将 Excel 工作表导出为 CSV(字符分隔值),然后使用普通文本编辑器和正则表达式以给定格式搜索和替换。
这是一项手动工作,但很快就完成了。
如果您更喜欢编写代码,可以使用 Apache POI(https://poi.apache.org/)to 阅读 Excel 文件。
这篇文章中发布了一个很好的例子:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/
我尝试通过添加 Apache POI 作为依赖项来解决问题,以便读取 excel 文件。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
为了编写键值对,我使用了 java 属性 class。
public class App {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
public static void main(String[] args) throws IOException {
final App app = new App();
for (final Locale locale : Locale.values()) {
app.readExcelFile("locales_" + locale + ".xlsx");
app.writeToPropertiesFile("messages_" + locale.name().substring(0, 2).toLowerCase() + ".properties");
}
}
public void writeToPropertiesFile(String propertiesPath) throws IOException {
final LinkedProperties props = new LinkedProperties();
map.entrySet().forEach(entry -> props.setProperty(entry.getKey(), entry.getValue()));
props.store(new FileOutputStream(new File(propertiesPath)), null);
}
public void readExcelFile(String fileName) throws IOException {
Workbook workbook = null;
XSSFCell cell1 = null;
XSSFCell cell2 = null;
try {
workbook = WorkbookFactory.create(new File(fileName));
final XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(0);
final Iterator<Row> rowIterator = sheet.rowIterator();
// skip first row which is header
rowIterator.next();
while (rowIterator.hasNext()) {
final XSSFRow row = (XSSFRow) rowIterator.next();
final Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell1 = (XSSFCell) cellIterator.next();
final String key = cell1.getRichStringCellValue().toString();
if (key == "")
break;
cell2 = (XSSFCell) cellIterator.next();
final String value = cell2.getRichStringCellValue().toString();
map.put(key, value);
}
}
}
catch (final Exception e) {
} finally {
if (workbook != null)
workbook.close();
}
}
}
为了保持属性文件中键值的顺序,我创建了自定义属性 class 即扩展属性 class.
的 LinkedProperties
public class LinkedProperties extends Properties {
/**
*
*/
private static final long serialVersionUID = 1L;
private final HashSet<Object> keys = new LinkedHashSet<>();
public LinkedProperties() {
// Nothing is done here
}
public Iterable<Object> orderedKeys() {
return Collections.list(keys());
}
@Override
public Enumeration<Object> keys() {
return Collections.<Object>enumeration(keys);
}
@Override
public Object put(Object key, Object value) {
keys.add(key);
return super.put(key, value);
}
@Override
public synchronized boolean equals(Object o) {
return super.equals(o);
}
@Override
public synchronized int hashCode() {
return super.hashCode();
}
}
我正在尝试从 excel (xlsx) 文件的给定集生成 messages_[en/da/bg].properties 文件。每个 excel sheet 都有两列作为键值对。我必须读取每个文件并将其写入以生成用于 i18n 的 messages_en.properties 文件。
如果我有 excel sheet 文件名如 locales_ENG.xlsx
、locales_DAN.xlsx
和 locales_BGR.xlsx
那么它应该生成如下文件:
messages_en.properties
messages_da.properties
messages_bg.properties
我建议将 Excel 工作表导出为 CSV(字符分隔值),然后使用普通文本编辑器和正则表达式以给定格式搜索和替换。 这是一项手动工作,但很快就完成了。
如果您更喜欢编写代码,可以使用 Apache POI(https://poi.apache.org/)to 阅读 Excel 文件。
这篇文章中发布了一个很好的例子:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/
我尝试通过添加 Apache POI 作为依赖项来解决问题,以便读取 excel 文件。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
为了编写键值对,我使用了 java 属性 class。
public class App {
LinkedHashMap<String, String> map = new LinkedHashMap<>();
public static void main(String[] args) throws IOException {
final App app = new App();
for (final Locale locale : Locale.values()) {
app.readExcelFile("locales_" + locale + ".xlsx");
app.writeToPropertiesFile("messages_" + locale.name().substring(0, 2).toLowerCase() + ".properties");
}
}
public void writeToPropertiesFile(String propertiesPath) throws IOException {
final LinkedProperties props = new LinkedProperties();
map.entrySet().forEach(entry -> props.setProperty(entry.getKey(), entry.getValue()));
props.store(new FileOutputStream(new File(propertiesPath)), null);
}
public void readExcelFile(String fileName) throws IOException {
Workbook workbook = null;
XSSFCell cell1 = null;
XSSFCell cell2 = null;
try {
workbook = WorkbookFactory.create(new File(fileName));
final XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(0);
final Iterator<Row> rowIterator = sheet.rowIterator();
// skip first row which is header
rowIterator.next();
while (rowIterator.hasNext()) {
final XSSFRow row = (XSSFRow) rowIterator.next();
final Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell1 = (XSSFCell) cellIterator.next();
final String key = cell1.getRichStringCellValue().toString();
if (key == "")
break;
cell2 = (XSSFCell) cellIterator.next();
final String value = cell2.getRichStringCellValue().toString();
map.put(key, value);
}
}
}
catch (final Exception e) {
} finally {
if (workbook != null)
workbook.close();
}
}
}
为了保持属性文件中键值的顺序,我创建了自定义属性 class 即扩展属性 class.
的 LinkedPropertiespublic class LinkedProperties extends Properties {
/**
*
*/
private static final long serialVersionUID = 1L;
private final HashSet<Object> keys = new LinkedHashSet<>();
public LinkedProperties() {
// Nothing is done here
}
public Iterable<Object> orderedKeys() {
return Collections.list(keys());
}
@Override
public Enumeration<Object> keys() {
return Collections.<Object>enumeration(keys);
}
@Override
public Object put(Object key, Object value) {
keys.add(key);
return super.put(key, value);
}
@Override
public synchronized boolean equals(Object o) {
return super.equals(o);
}
@Override
public synchronized int hashCode() {
return super.hashCode();
}
}