diff --git a/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/SqlRowSetDataSet.java b/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/SqlRowSetDataSet.java index 19a77df9fe8b1385f7a5302439d63c4e6ffece93..7a68a6a2fda45a71e22f0e32eb54ad189c6ef4ab 100644 --- a/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/SqlRowSetDataSet.java +++ b/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/SqlRowSetDataSet.java @@ -4,6 +4,7 @@ import org.springframework.jdbc.support.rowset.SqlRowSet; import org.springframework.jdbc.support.rowset.SqlRowSetMetaData; import org.tinygroup.tinyscript.dataset.AbstractDataSet; import org.tinygroup.tinyscript.dataset.Field; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 包装Spring记录集原生类型 @@ -86,7 +87,7 @@ public class SqlRowSetDataSet extends AbstractDataSet{ if (absolute(row)) { return getData(col); } - throw new Exception("不存在" + row + "行的数据!"); + throw new Exception(ResourceBundleUtil.getResourceMessage("database", "database.data.noexists", row)); } public void setData(int row, int col, T data) throws Exception { diff --git a/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/function/ExecuteSqlFunction.java b/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/function/ExecuteSqlFunction.java index e8409fc8599751dbfbe02695bc9e881a66b17e3c..d033626b734b78968cef8230284d733c742a3f4e 100644 --- a/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/function/ExecuteSqlFunction.java +++ b/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/function/ExecuteSqlFunction.java @@ -9,6 +9,7 @@ import org.tinygroup.tinyscript.ScriptContext; import org.tinygroup.tinyscript.ScriptException; import org.tinygroup.tinyscript.ScriptSegment; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * 扩展数据源操作sql,自动释放Connection @@ -29,7 +30,7 @@ public class ExecuteSqlFunction extends AbstractScriptFunction { Object... parameters) throws ScriptException { try{ if (parameters == null || parameters.length == 0) { - throw new ScriptException("execute函数的参数为空!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); }else if(parameters.length == 2 && parameters[0] != null && parameters[1] != null){ String sql = (String) parameters[1]; if(parameters[0] instanceof DataSource){ @@ -37,15 +38,15 @@ public class ExecuteSqlFunction extends AbstractScriptFunction { }else if(parameters[0] instanceof Connection){ return executeConnection((Connection)parameters[0],sql); }else{ - throw new ScriptException("execute函数的参数格式不正确:未知类型"+parameters[0].getClass().getName()); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } }else { - throw new ScriptException("execute函数的参数格式不正确!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } }catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("execute函数执行发生异常:", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/function/QuerySqlFunction.java b/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/function/QuerySqlFunction.java index 3958d0ad6fc1097335d9d4ba451ecf5685395cd2..ab0a5250e53d231a9f7137f8943dcc7290148231 100644 --- a/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/function/QuerySqlFunction.java +++ b/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/function/QuerySqlFunction.java @@ -13,6 +13,7 @@ import org.tinygroup.tinyscript.database.ResultSetDataSet; import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.impl.VariableDataSet; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; public class QuerySqlFunction extends AbstractScriptFunction { @@ -28,7 +29,7 @@ public class QuerySqlFunction extends AbstractScriptFunction { Object... parameters) throws ScriptException { try{ if (parameters == null || parameters.length == 0) { - throw new ScriptException("query函数的参数为空!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); }else if(parameters.length == 2 && parameters[0] != null && parameters[1] != null){ String sql = (String) parameters[1]; if(parameters[0] instanceof DataSource){ @@ -36,15 +37,15 @@ public class QuerySqlFunction extends AbstractScriptFunction { }else if(parameters[0] instanceof Connection){ return queryConnection((Connection)parameters[0],sql); }else{ - throw new ScriptException("query函数的参数格式不正确:未知类型"+parameters[0].getClass().getName()); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } }else { - throw new ScriptException("query函数的参数格式不正确!"); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } }catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException("query函数执行发生异常:", e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } diff --git a/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/function/WriteDBFunction.java b/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/function/WriteDBFunction.java index edd2e3e0edced9673d840b3202e6650844b19e81..1a51ce1422b3688a16d5781966aefe7e412099f5 100644 --- a/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/function/WriteDBFunction.java +++ b/org.tinygroup.tinyscript.database/src/main/java/org/tinygroup/tinyscript/database/function/WriteDBFunction.java @@ -18,171 +18,179 @@ import org.tinygroup.tinyscript.dataset.AbstractDataSet; import org.tinygroup.tinyscript.dataset.DataSet; import org.tinygroup.tinyscript.dataset.Field; import org.tinygroup.tinyscript.function.AbstractScriptFunction; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; import org.tinygroup.tinyscript.interpret.ScriptContextUtil; import org.tinygroup.tinyscript.interpret.ScriptUtil; /** * 序表通过sql写入数据库 + * * @author yancheng11334 * */ public class WriteDBFunction extends AbstractScriptFunction { private static final int MAX_RECORDS = 200; - + public String getNames() { return "writeDB"; } - + public String getBindingTypes() { return DataSet.class.getName(); } - public Object execute(ScriptSegment segment, ScriptContext context, - Object... parameters) throws ScriptException { - try{ + public Object execute(ScriptSegment segment, ScriptContext context, Object... parameters) throws ScriptException { + try { if (parameters == null || parameters.length == 0) { - throw new ScriptException(String.format("%s函数的参数为空!", getNames())); - }else if(checkParameters(parameters, 2)){ + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.empty", getNames())); + } else if (checkParameters(parameters, 2)) { AbstractDataSet ds = (AbstractDataSet) parameters[0]; String table = (String) parameters[1]; - return insertDataSet(ds,table,null,context); - }else if(checkParameters(parameters, 3)){ + return insertDataSet(ds, table, null, context); + } else if (checkParameters(parameters, 3)) { AbstractDataSet ds = (AbstractDataSet) parameters[0]; String table = (String) parameters[1]; - DataSource dataSource =(DataSource) parameters[2]; - return insertDataSet(ds,table,dataSource,context); - }else { - throw new ScriptException(String.format("%s函数的参数格式不正确!", getNames())); + DataSource dataSource = (DataSource) parameters[2]; + return insertDataSet(ds, table, dataSource, context); + } else { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); } - }catch (ScriptException e) { + } catch (ScriptException e) { throw e; } catch (Exception e) { - throw new ScriptException(String.format("%s函数执行发生异常:", getNames()), e); + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); } } - + /** * 批量插入序表数据 + * * @param ds * @param table * @param dataSource * @return * @throws ScriptException */ - private Object insertDataSet(AbstractDataSet ds,String table,DataSource dataSource,ScriptContext context) throws Exception{ - DataSource newDataSource = dataSource==null?(DataSource)ScriptUtil.getVariableValue(context, ScriptContextUtil.getCustomBeanName(context)):dataSource; + private Object insertDataSet(AbstractDataSet ds, String table, DataSource dataSource, ScriptContext context) + throws Exception { + DataSource newDataSource = dataSource == null + ? (DataSource) ScriptUtil.getVariableValue(context, ScriptContextUtil.getCustomBeanName(context)) + : dataSource; Connection conn = null; PreparedStatement ps = null; String insertSql = null; - try{ + try { conn = newDataSource.getConnection(); - ResultSetMetaData rsmd = getTableMetaData(table,conn); - Map maps = createMap(ds,rsmd); - insertSql = createSQL(ds,table); - + ResultSetMetaData rsmd = getTableMetaData(table, conn); + Map maps = createMap(ds, rsmd); + insertSql = createSQL(ds, table); + conn.setAutoCommit(false); ps = conn.prepareStatement(insertSql); - - int count=0; + + int count = 0; int[] result = new int[ds.getRows()]; - int i=0,j=0; - try{ - for(i=0;i createMap(DataSet ds,ResultSetMetaData rsmd) throws Exception{ - Map maps = new HashMap(); //定义物理表的字段名和列的映射关系 - Map result = new HashMap(); //定义字段列和物理表列的映射关系 - for(int i=1;i<=rsmd.getColumnCount();i++){ - maps.put(rsmd.getColumnName(i).toUpperCase(), i); //转大写存储 + + private Map createMap(DataSet ds, ResultSetMetaData rsmd) throws Exception { + Map maps = new HashMap(); // 定义物理表的字段名和列的映射关系 + Map result = new HashMap(); // 定义字段列和物理表列的映射关系 + for (int i = 1; i <= rsmd.getColumnCount(); i++) { + maps.put(rsmd.getColumnName(i).toUpperCase(), i); // 转大写存储 } List fields = ds.getFields(); - for(int i=0;i fields = ds.getFields(); StringBuilder sb = new StringBuilder(); StringBuilder values = new StringBuilder(); values.append(" values("); sb.append("insert into ").append(table); sb.append(" ("); - for(int i=0;i list = new ArrayList(); + for (int i = 1; i <= dataSet.getRows(); i++) { + list.add(new DataSetBean(dataSet, i)); + } + return list; + } else { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.parameter.error", getNames())); + } + + } catch (ScriptException e) { + throw e; + } catch (Exception e) { + throw new ScriptException(ResourceBundleUtil.getDefaultMessage("function.run.error", getNames()), e); + } + } + +} \ No newline at end of file diff --git a/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/DataSetBean.java b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/DataSetBean.java new file mode 100644 index 0000000000000000000000000000000000000000..dbfbb10f1a659c673ba1fda333f435133e7ecd8c --- /dev/null +++ b/org.tinygroup.tinyscript.dataset/src/main/java/org/tinygroup/tinyscript/dataset/impl/DataSetBean.java @@ -0,0 +1,259 @@ +package org.tinygroup.tinyscript.dataset.impl; + +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.tinygroup.tinyscript.ScriptException; +import org.tinygroup.tinyscript.dataset.DataSet; +import org.tinygroup.tinyscript.dataset.Field; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; + +public class DataSetBean implements Map { + + private Map map; + + private String type; + private List fields; + private int rowId = -1; + + public DataSetBean(Map map) { + this(map, false); + } + + public DataSetBean(Map map, boolean order) { + if (order) { + map = new LinkedHashMap(); + } else { + map = new HashMap(); + } + + fields = new ArrayList(); + for (String key : map.keySet()) { + fields.add(new Field(key, key, map.get(key).getClass().getSimpleName())); + } + + putAll(map); + } + + public DataSetBean(DataSet dataSet, int rowId) { + this(dataSet, rowId, false); + } + + public DataSetBean(DataSet dataSet, int rowId, boolean order) { + if (order) { + map = new LinkedHashMap(); + } else { + map = new HashMap(); + } + this.fields = dataSet.getFields(); + this.rowId = rowId; + this.type = dataSet.getClass().getSimpleName(); + for (int i = 1; i <= fields.size(); i++) { + try { + put(fields.get(i - 1).getName(), dataSet.getData(rowId, i)); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + /** + * 将DataSetBean转为实体Object,class需要实现无参的构造方法 + * + * @param clazz + * @return + * @throws ScriptException + */ + public T convertToObject(Class clazz) throws ScriptException { + T t = null; + try { + Constructor constructor = clazz.getDeclaredConstructor(); + constructor.setAccessible(true); + t = constructor.newInstance(); + } catch (Exception e) { + throw new ScriptException( + ResourceBundleUtil.getResourceMessage("dataset", "dataset.creatbean.error", clazz.getName()), e); + } + + for (String key : map.keySet()) { + try { + java.lang.reflect.Field field = clazz.getDeclaredField(key); + field.setAccessible(true); + field.set(t, map.get(key)); + } catch (NoSuchFieldException e) { + // 如果没找到对应的field,则跳过 + } catch (IllegalAccessException e) { + } + + } + return t; + } + + @SuppressWarnings("unchecked") + public T getObject(Object key, Class clazz) { + Object obj = map.get(key); + if (obj.getClass() == clazz) { + return (T) obj; + } + + return castToBaseType(obj, clazz); + } + + @Override + public int size() { + return map.size(); + } + + @Override + public boolean isEmpty() { + return map.size() == 0; + } + + @Override + public boolean containsKey(Object key) { + return map.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return map.containsValue(value); + } + + @Override + public Object get(Object key) { + return map.get(key); + } + + @Override + public Object put(String key, Object value) { + return map.put(key, value); + } + + @Override + public Object remove(Object key) { + return map.remove(key); + } + + @Override + public void putAll(Map m) { + map.putAll(m); + } + + @Override + public void clear() { + rowId = -1; + type = null; + fields.clear(); + map.clear(); + } + + @Override + public Set keySet() { + return map.keySet(); + } + + @Override + public Collection values() { + return map.values(); + } + + @Override + public Set> entrySet() { + return map.entrySet(); + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public List getFields() { + return fields; + } + + public void setFields(List fields) { + this.fields = fields; + } + + public int getRowId() { + return rowId; + } + + public void setRowId(int rowId) { + this.rowId = rowId; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("{"); + for (String key : map.keySet()) { + builder.append(key).append(":").append(map.get(key).toString()).append(" "); + } + builder.append("}"); + return builder.toString(); + } + + @SuppressWarnings("unchecked") + private T castToBaseType(Object obj, Class clazz) { + if (clazz == int.class || clazz == Integer.class) { + try { + return (T) Integer.valueOf(obj.toString()); + } catch (Exception e) { + return null; + } + } + if (clazz == long.class || clazz == Long.class) { + try { + return (T) Long.valueOf(obj.toString()); + } catch (Exception e) { + return null; + } + } + if (clazz == short.class || clazz == Short.class) { + try { + return (T) Short.valueOf(obj.toString()); + } catch (Exception e) { + return null; + } + } + if (clazz == double.class || clazz == Double.class) { + try { + return (T) Double.valueOf(obj.toString()); + } catch (Exception e) { + return null; + } + } + if (clazz == float.class || clazz == Float.class) { + try { + return (T) Float.valueOf(obj.toString()); + } catch (Exception e) { + return null; + } + } + if (clazz == boolean.class || clazz == Boolean.class) { + try { + return (T) Boolean.valueOf(obj.toString()); + } catch (Exception e) { + return null; + } + } + + if (clazz == String.class) { + return (T) obj.toString(); + } + + return null; + + } + +} diff --git a/org.tinygroup.tinyscript.dataset/src/main/resources/dataset.beans.xml b/org.tinygroup.tinyscript.dataset/src/main/resources/dataset.beans.xml index 1f546ec94f2f740cb7da76b1bb4cd71466c658e1..8a60f6627366d4314488a3d57b453fbb99982f7a 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/resources/dataset.beans.xml +++ b/org.tinygroup.tinyscript.dataset/src/main/resources/dataset.beans.xml @@ -166,5 +166,9 @@ + + + diff --git a/org.tinygroup.tinyscript.dataset/src/main/resources/dataset.properties b/org.tinygroup.tinyscript.dataset/src/main/resources/dataset.properties index 4e152c70f7bf79464cd1981205ccc0455f2ed6d4..66b682f99f645162d203d85c1dc535bb7cf83ad9 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/resources/dataset.properties +++ b/org.tinygroup.tinyscript.dataset/src/main/resources/dataset.properties @@ -32,4 +32,5 @@ dataset.expression.error=[%s] An exception occurred in the resolution rule:[%s] dataset.sort.error=An exception occurred in sorted by rule:[%s] dataset.operate.nosupport=DataSet does not support operations:[%s] dataset.clone.error=[%s] clone failed: +dataset.creatbean.error=Create bean failed:[%s] diff --git a/org.tinygroup.tinyscript.dataset/src/main/resources/dataset_zh_CN.properties b/org.tinygroup.tinyscript.dataset/src/main/resources/dataset_zh_CN.properties index 1275a373ac347807d61867d54af1d1dcac29b0a6..aedc1e07a2b887caa28efa3d822ffed87f456975 100644 --- a/org.tinygroup.tinyscript.dataset/src/main/resources/dataset_zh_CN.properties +++ b/org.tinygroup.tinyscript.dataset/src/main/resources/dataset_zh_CN.properties @@ -32,4 +32,5 @@ dataset.expression.error=\u89e3\u6790[%s]\u89c4\u5219[%s]\u5931\u8d25! dataset.sort.error=\u5e8f\u5217\u6309\u89c4\u5219[%s]\u6392\u5e8f\u53d1\u751f\u5f02\u5e38\u003a dataset.operate.nosupport=\u6570\u636e\u96c6\u4e0d\u652f\u6301[%s]\u64cd\u4f5c dataset.clone.error=[%s]\u514b\u9686\u5931\u8d25\u003a +dataset.creatbean.error=\u521b\u5efa\u0062\u0065\u0061\u006e\u5931\u8d25\uff1a[%s] diff --git a/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/excel/SheetDataSet.java b/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/excel/SheetDataSet.java index 3ff8433c649612bc1aa618b5d35c596ad3db8f6b..cd8cadc20a7ab18c9c9cc97878905d368866b768 100644 --- a/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/excel/SheetDataSet.java +++ b/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/excel/SheetDataSet.java @@ -9,6 +9,7 @@ import org.apache.poi.ss.usermodel.Sheet; import org.tinygroup.tinyscript.dataset.AbstractDataSet; import org.tinygroup.tinyscript.dataset.Field; import org.tinygroup.tinyscript.excel.util.ExcelUtil; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; /** * excel结果集 @@ -19,10 +20,10 @@ import org.tinygroup.tinyscript.excel.util.ExcelUtil; public class SheetDataSet extends AbstractDataSet { private Sheet sheet; - private int currentRow = -1; //对应实际下标,而非显示下标 + private int currentRow = -1; // 对应实际下标,而非显示下标 private XlsArea xlsArea; - public SheetDataSet(Sheet sheet,XlsArea xlsArea) throws Exception { + public SheetDataSet(Sheet sheet, XlsArea xlsArea) throws Exception { this.sheet = sheet; this.xlsArea = xlsArea; @@ -31,7 +32,7 @@ public class SheetDataSet extends AbstractDataSet { // 生成field List fields = new ArrayList(); - + Row row = sheet.getRow(getFirstRowNum()); for (int i = getFirstCellNum(row); i < getLastCellNum(row); i++) { Cell nameCell = row.getCell(i); @@ -47,37 +48,38 @@ public class SheetDataSet extends AbstractDataSet { throw new Exception("没有数据存在,无法移动到第一行!"); } } - - private int getFirstRowNum(){ - if(xlsArea!=null && xlsArea.getDataY()>0){ - return xlsArea.getDataY(); + + private int getFirstRowNum() { + if (xlsArea != null && xlsArea.getDataY() > 0) { + return xlsArea.getDataY(); } return sheet.getFirstRowNum(); } - - private int getLastRowNum(){ - if(xlsArea!=null && xlsArea.getDataY()>0 && xlsArea.getHeight()>0){ - return xlsArea.getDataY()+xlsArea.getHeight(); + + private int getLastRowNum() { + if (xlsArea != null && xlsArea.getDataY() > 0 && xlsArea.getHeight() > 0) { + return xlsArea.getDataY() + xlsArea.getHeight(); } return sheet.getLastRowNum(); } - - private int getFirstCellNum(Row row){ - if(xlsArea!=null && xlsArea.getDataX()>0){ - return xlsArea.getDataX(); + + private int getFirstCellNum(Row row) { + if (xlsArea != null && xlsArea.getDataX() > 0) { + return xlsArea.getDataX(); } return row.getFirstCellNum(); } - - private int getLastCellNum(Row row){ - if(xlsArea!=null && xlsArea.getDataX()>0 && xlsArea.getWidth()>0){ - return xlsArea.getDataX()+xlsArea.getWidth(); + + private int getLastCellNum(Row row) { + if (xlsArea != null && xlsArea.getDataX() > 0 && xlsArea.getWidth() > 0) { + return xlsArea.getDataX() + xlsArea.getWidth(); } return row.getLastCellNum(); } /** * 本方法私有,对应的参数指实际下标 + * * @param row * @param col * @return @@ -87,8 +89,8 @@ public class SheetDataSet extends AbstractDataSet { try { return sheet.getRow(row).getCell(col); } catch (Exception e) { - throw new Exception(String.format("查询第%d行,第%d列数据失败:", row, col), - e); + throw new Exception(ResourceBundleUtil.getResourceMessage("excel", "read.cell.error", row, col), e); + } } @@ -99,8 +101,6 @@ public class SheetDataSet extends AbstractDataSet { return field; } - - public boolean isReadOnly() { return false; } @@ -113,7 +113,7 @@ public class SheetDataSet extends AbstractDataSet { public boolean previous() throws Exception { checkValidate(); if (currentRow == getFirstRowNum()) { - throw new Exception("已经移动到第一行,无法继续前移!"); + throw new Exception(ResourceBundleUtil.getResourceMessage("excel", "row.move.error")); } currentRow--; return true; @@ -140,13 +140,13 @@ public class SheetDataSet extends AbstractDataSet { public boolean absolute(int row) throws Exception { checkValidate(); - if(isIndexFromOne()){ - if (row-1 < getFirstRowNum() || row-1 >= getLastRowNum()) { - throw new Exception(String.format("指定的行数%s不合法或越界!", row)); + if (isIndexFromOne()) { + if (row - 1 < getFirstRowNum() || row - 1 >= getLastRowNum()) { + throw new Exception(ResourceBundleUtil.getResourceMessage("excel", "excel.row.outofindex", row)); } - }else{ + } else { if (row < getFirstRowNum() || row >= getLastRowNum()) { - throw new Exception(String.format("指定的行数%s不合法或越界!", row)); + throw new Exception(ResourceBundleUtil.getResourceMessage("excel", "excel.row.outofindex", row)); } } currentRow = getActualIndex(row); @@ -160,7 +160,7 @@ public class SheetDataSet extends AbstractDataSet { public int getColumns() throws Exception { if (getFields() == null) { - throw new Exception("当前记录集没有定义字段相关数据!"); + throw new Exception(ResourceBundleUtil.getResourceMessage("excel", "excel.field.null")); } return getFields().size(); } @@ -175,18 +175,18 @@ public class SheetDataSet extends AbstractDataSet { row = getActualIndex(row); col = getActualIndex(col); Cell cell = getCell(row, col); - if(cell==null){ - sheet.getRow(row).createCell(col, Cell.CELL_TYPE_STRING); - cell = getCell(row, col); + if (cell == null) { + sheet.getRow(row).createCell(col, Cell.CELL_TYPE_STRING); + cell = getCell(row, col); } - if(data==null){ - cell.setCellType(Cell.CELL_TYPE_BLANK); - }else if(data.getClass().equals(String.class)){ - cell.setCellType(Cell.CELL_TYPE_STRING); - cell.setCellValue(data.toString()); - }else { - cell.setCellType(Cell.CELL_TYPE_STRING); - cell.setCellValue(String.valueOf(data)); + if (data == null) { + cell.setCellType(Cell.CELL_TYPE_BLANK); + } else if (data.getClass().equals(String.class)) { + cell.setCellType(Cell.CELL_TYPE_STRING); + cell.setCellValue(data.toString()); + } else { + cell.setCellType(Cell.CELL_TYPE_STRING); + cell.setCellValue(String.valueOf(data)); } } diff --git a/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/excel/util/ExcelUtil.java b/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/excel/util/ExcelUtil.java index c8e8e008b61dbecaf85629b7c5254d95370d3d7f..37027bd499979263006d1c1cd5abd326c64800a0 100644 --- a/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/excel/util/ExcelUtil.java +++ b/org.tinygroup.tinyscript.excel/src/main/java/org/tinygroup/tinyscript/excel/util/ExcelUtil.java @@ -9,10 +9,12 @@ import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.tinygroup.tinyscript.interpret.ResourceBundleUtil; import org.tinygroup.vfs.FileObject; /** * excel辅助工具类 + * * @author yancheng11334 * */ @@ -23,33 +25,36 @@ public class ExcelUtil { /** * 根据模板加载excel + * * @param fileObject * @return * @throws Exception */ - public static Workbook readWorkbook(FileObject fileObject) throws Exception { + public static Workbook readWorkbook(FileObject fileObject) throws Exception { Workbook wb = null; try { String fileName = fileObject.getFileName().toLowerCase(); - if(fileName.endsWith(XLSX_FILE_NAME) || fileName.endsWith(XLTX_FILE_NAME)){ + if (fileName.endsWith(XLSX_FILE_NAME) || fileName.endsWith(XLTX_FILE_NAME)) { wb = new XSSFWorkbook(fileObject.getInputStream()); - }else{ + } else { wb = new HSSFWorkbook(fileObject.getInputStream()); } - + } catch (IOException e) { - throw new Exception(String.format("解析excel文件%s,发生异常",fileObject.getFileName()),e); + throw new Exception( + ResourceBundleUtil.getResourceMessage("excel", "read.excelfile.error", fileObject.getFileName()), e); } return wb; } - + /** * 创建新的excel + * * @param fileObject * @return * @throws Exception */ - public static Workbook createWorkbook(FileObject fileObject) throws Exception { + public static Workbook createWorkbook(FileObject fileObject) throws Exception { Workbook wb = null; String fileName = fileObject.getFileName().toLowerCase(); if (fileName.endsWith(XLSX_FILE_NAME) || fileName.endsWith(XLTX_FILE_NAME)) { @@ -59,59 +64,60 @@ public class ExcelUtil { } return wb; } - + /** * 复制行 + * * @param srcRow * @param dstRow * @throws Exception */ - public static void copyRow(Row srcRow,Row dstRow) throws Exception { -// CellStyle srcSylte = srcRow.getRowStyle(); -// CellStyle dstStyle = dstRow.getRowStyle(); -// dstStyle.cloneStyleFrom(srcSylte); -// dstRow.setRowStyle(dstStyle); -// dstRow.setHeight(srcRow.getHeight()); -// dstRow.setHeightInPoints(srcRow.getHeightInPoints()); -// dstRow.setRowNum(srcRow.getRowNum()); -// dstRow.setZeroHeight(srcRow.getZeroHeight()); - if(srcRow==null || dstRow==null){ - return; + public static void copyRow(Row srcRow, Row dstRow) throws Exception { + // CellStyle srcSylte = srcRow.getRowStyle(); + // CellStyle dstStyle = dstRow.getRowStyle(); + // dstStyle.cloneStyleFrom(srcSylte); + // dstRow.setRowStyle(dstStyle); + // dstRow.setHeight(srcRow.getHeight()); + // dstRow.setHeightInPoints(srcRow.getHeightInPoints()); + // dstRow.setRowNum(srcRow.getRowNum()); + // dstRow.setZeroHeight(srcRow.getZeroHeight()); + if (srcRow == null || dstRow == null) { + return; } - for(int j=srcRow.getFirstCellNum();j