将多个多语资源properties文件整理到同一个excel里,一一对应起来
要求
比如:
第一个文件 message_zh.properties 里有
aaa=123
bbb=456
第二个文件 message_en_US.properties 里有
aaa=onetwothree
bbb=fourfivesix
合并到一个excel里只有三列
aaa 123 onetwothree
bbb 456 fourfivesix
实现
import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Map; import java.util.Properties; /** * @author jiash * @since 2023/4/18 */ public class PropertiesExcelTest { public static void main(String[] args) throws Exception { InputStream inputStream = new FileInputStream("E:\\test\\message_zh.properties"); Properties properties1 = new Properties(); properties1.load(inputStream); InputStream inputStream2 = new FileInputStream("E:\\test\\message_en_US.properties"); Properties properties2 = new Properties(); properties2.load(inputStream2); String excelPath = "E:\\test\\aaaaa.xls"; // 创建Excel的工作书册 Workbook,对应到一个excel文档 HSSFWorkbook wb = new HSSFWorkbook(); // 创建Excel的工作sheet,对应到一个excel文档的tab HSSFSheet sheet = wb.createSheet("sheet1"); int i = 1; for (Map.Entry<Object, Object> entry : properties1.entrySet()) { String key = entry.getKey().toString(); HSSFRow row = sheet.createRow(i); HSSFCell cell = row.createCell(1); cell.setCellValue(key); cell = row.createCell(2); cell.setCellValue(properties1.get(key).toString()); cell = row.createCell(3); cell.setCellValue(properties2.get(key).toString()); i++; } try (FileOutputStream os = new FileOutputStream(excelPath)) { wb.write(os); } catch (Exception e) { } } }
原创文章,欢迎转载,转载请注明出处!
把每一件简单的事情做好,就是不简单;把每一件平凡的事情做好,就是不平凡!相信自己,创造奇迹~~