[英]How to read 2 columns from excel file (xlsx) and write it to resource properties as key=value pair
I am trying to generate messages_[en/da/bg].properties file from the given set for excel (xlsx) files. 我正在尝试从excel(xlsx)文件的给定集合中生成messages_ [en / da / bg] .properties文件。 Each excel sheet is provided with two columns as key-value pair. 每个excel工作表都有两列作为键值对。 I have to read each file and write it to generate messages_en.properties file for i18n. 我必须读取每个文件并将其写入以为i18n生成messages_en.properties文件。
If I have excel sheets with filenames like locales_ENG.xlsx
, locales_DAN.xlsx
& locales_BGR.xlsx
then it should generate files as: messages_en.properties
messages_da.properties
messages_bg.properties
如果我有具有诸如locales_ENG.xlsx
, locales_DAN.xlsx
和locales_BGR.xlsx
类的文件名的excel工作表,则它应生成以下文件: messages_en.properties
messages_da.properties
messages_bg.properties
I would suggest exporting the Excel sheets as CSV (character separated value) and then, using a normal text editor and regular expression to search and replace in the given format. 我建议将Excel工作表导出为CSV(字符分隔值),然后使用常规文本编辑器和正则表达式以给定格式进行搜索和替换。 This is a manual effort but done quickly. 这是手动操作,但操作很快。
If you prefer writing code you can use Apache POI ( https://poi.apache.org/)to read the Excel files. 如果您喜欢编写代码,则可以使用Apache POI( https://poi.apache.org/)来读取Excel文件。
A good example is posted in this article: https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ 本文中发布了一个很好的示例: https : //www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/
I tried solving the problem by adding the Apache POI as a dependency in order to read excel file. 我尝试通过添加Apache POI作为依赖项来解决问题,以便读取excel文件。
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
and for writing the key-value pair I have used java Properties class. 为了编写键值对,我使用了Java Properties类。
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();
}
}
} }
In order to maintain the order of the key-value in the properties file, I have created custom properties class ie LinkedProperties which extends Properties class. 为了保持属性文件中键值的顺序,我创建了自定义属性类,即LinkedProperties,它扩展了Properties类。
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();
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.