• 通过Excel来集中管理资源文件


    

    在支持双语或多语种项目中,常常需要编辑多个文件来添加资源项,感觉比较繁琐,所以想做一个可以集中管理资源文件的工具。借助Excel,使用Excel来记录,并且通过Excel可以进行分页分模块来规划资源项的存放。

    资源excel样例:

    资源标识 中文 英文
    product.id 产品ID Product ID
    product.name 产品名称 Product Name


    解析excel文件,生成资源文件的工具下载地址:

    http://download.csdn.net/detail/u014569459/7186353


    代码(基于jxl):

    package cn.jerry.mouse.property_tools;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    
    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    
    public class ExcelProperties {
    
    	public void exportToFile(String excelFileName,int keyColumn,int valueColumn,String exportFileName) throws Exception {
    		File excelFile = new File(excelFileName);
    		File exportFile = new File(exportFileName);
    		BufferedWriter bw = new BufferedWriter(new FileWriter(exportFile));
    		
    		Workbook workbook = Workbook.getWorkbook(excelFile);
    		Sheet[] sheet = workbook.getSheets();
    		Cell[] keyCell;
    		Cell[] valueCell;
    		String key;
    		String value;
    		
    		for(int sheetIndex=0;sheetIndex<sheet.length;sheetIndex++)
    		{
    			keyCell = sheet[sheetIndex].getColumn(keyColumn);
    			valueCell = sheet[sheetIndex].getColumn(valueColumn);
    			
    			for(int rowIndex=1;rowIndex<keyCell.length;rowIndex++) //第一行作为标题栏,忽略掉
    			{
    				key = keyCell[rowIndex].getContents();
    				value = valueCell[rowIndex].getContents();
    				if(key.trim()!="")
    					bw.write(key+"="+value+"
    ");
    			}
    		}
    		bw.close();
    		workbook.close();
    	}
    	private boolean isFileValid(String excelFileName) throws Exception
    	{
    		try {
    			File excelFile = new File(excelFileName);
    			Workbook workbook = Workbook.getWorkbook(excelFile);
    			workbook.getSheets();
    			workbook.close();
    		} catch (BiffException e) {
    			throw new Exception("不支持此文件格式,仅支持Excel 2003");
    		}
    		return true; 
    	}
    	public static void main(String[] args) throws Exception
    	{
    		ExcelProperties excelUtil = new ExcelProperties();
    		String excelFilePath = "D:\res.xls";
    		String exportCNFilePath = "D:\res_zh_CN.properties";
    		String exportENFilePath = "D:\res_en_US.properties";
    		String exportDefaultFilePath = "D:\res.properties";
    		int keyColumn = 0;
    		int valueColumn;
    		
    		if(args.length==4)
    		{
    			excelFilePath = args[0];
    			exportCNFilePath = args[1];
    			exportENFilePath = args[2];
    			exportDefaultFilePath = args[3];
    		}
    		else if(args.length!=0)
    		{
    			System.out.println("Usage: java -jar ExcelProps.jar excelFilePath exportCNFilePath exportENFilePath excelFilePath");
    			return;
    		}
    		
    		excelUtil.isFileValid(excelFilePath);
    		
    		System.out.println("Begin to exprort from excelFile: "+excelFilePath);
    		
    		valueColumn = 1;
    		excelUtil.exportToFile(excelFilePath, keyColumn, valueColumn, exportCNFilePath);
    		System.out.println("Config file in Chinese exported: "+exportCNFilePath);
    		
    		valueColumn = 2;
    		excelUtil.exportToFile(excelFilePath, keyColumn, valueColumn, exportENFilePath);
    		System.out.println("Config file in English exported: "+exportENFilePath);
    		
    		valueColumn = 1;
    		excelUtil.exportToFile(excelFilePath, keyColumn, valueColumn, exportDefaultFilePath);
    		System.out.println("Config file in default language exported: "+exportDefaultFilePath);
    	}
    }
    


  • 相关阅读:
    Spring标签@Aspect-实现面向方向编程(@Aspect的多数据源自动加载)——SKY
    easyUI参数传递Long型时,前台解析出错的问题——SKY
    javax.servlet.ServletException: Could not resolve view with name‘ XXXX’in servlet with name 'spring'的解决方案-----SKY
    Netty实现java多线程Post请求解析(Map参数类型)—SKY
    java并发编程基础---Sky
    创建100个1k的随机文件到FSxL
    RAID磁盘阵列与LVM逻辑卷管理
    The beginners’ guide to farming Chia Coin on Windows.
    有赞移动Crash平台建设
    Comparisons Serverless
  • 原文地址:https://www.cnblogs.com/jerry1999/p/3740151.html
Copyright © 2020-2023  润新知