zoukankan      html  css  js  c++  java
  • 通过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);
    	}
    }
    


  • 相关阅读:
    大数据基础---Hbase搭建
    大数据基础---Hbase是什么?
    大数据基础---Hive的搭建
    hive_异常_01_ Terminal initialization failed; falling back to unsupported
    Exception in thread "main" java.lang.IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: ${system:java.io.tmpdir%7D/$%7Bsystem:user.name%7D
    Jupyter notebook 平台字体修改
    Git教程04——GitHub远端仓库内容同步本地仓库
    Git教程03——本地仓库内容同步到GitHub远程仓库
    Git教程02——利用Git GUI 连接 GitHub远程仓库
    Git教程01——Windows 安装 Git
  • 原文地址:https://www.cnblogs.com/jerry1999/p/3740151.html
Copyright © 2011-2022 走看看