├── README.md ├── excel2db-browse ├── .gitignore ├── pom.xml ├── scripts │ └── x-java-startup.bat └── src │ └── main │ ├── java │ └── org │ │ └── excel2db │ │ └── browse │ │ ├── NDBBrowseFrame.java │ │ └── NDBTableModel.java │ └── resources │ ├── log4j.properties │ └── package.xml ├── excel2db-read ├── .gitignore ├── pom.xml ├── src │ ├── main │ │ ├── java │ │ │ └── org │ │ │ │ └── excel2db │ │ │ │ └── read │ │ │ │ ├── DBFile.java │ │ │ │ ├── Header.java │ │ │ │ ├── NdbDataManager.java │ │ │ │ └── util │ │ │ │ ├── BeanUtil.java │ │ │ │ ├── Constants.java │ │ │ │ ├── SuffixUtil.java │ │ │ │ └── TypeEnum.java │ │ └── resources │ │ │ └── log4j.properties │ └── test │ │ └── java │ │ └── test │ │ ├── ArrayTestData.java │ │ ├── ArrayTestDataManager.java │ │ ├── GeneralData.java │ │ ├── GeneralDataManager.java │ │ └── ReadMain.java └── suffix.properties ├── excel2db-write ├── .gitignore ├── config.properties ├── pom.xml ├── scripts │ ├── x-csharp-startup.bat │ └── x-java-startup.bat └── src │ └── main │ ├── java │ └── org │ │ └── excel2db │ │ └── write │ │ ├── WriteMain.java │ │ ├── genClass │ │ ├── AbstractGenerator.java │ │ ├── Config.java │ │ ├── GeneratorFactory.java │ │ ├── IGenerator.java │ │ ├── SheetInfo.java │ │ ├── generator │ │ │ ├── CSharpGenerator.java │ │ │ └── JavaGenerator.java │ │ └── sign │ │ │ ├── CSharpSign.java │ │ │ └── JavaSign.java │ │ ├── manager │ │ ├── ExcelParse.java │ │ ├── GeneratorBeanClass.java │ │ └── NDBGenerator.java │ │ └── util │ │ ├── ConfigUtil.java │ │ ├── Constants.java │ │ ├── EndfixEnum.java │ │ ├── ExcelColumn.java │ │ ├── FileUtil.java │ │ └── TypeEnum.java │ └── resources │ ├── log4j.properties │ └── package.xml ├── pom.xml ├── src ├── main │ └── java │ │ └── com │ │ └── excel2db │ │ └── App.java └── test │ └── java │ └── com │ └── excel2db │ └── AppTest.java ├── test.xls └── 二进制格式文档 /README.md: -------------------------------------------------------------------------------- 1 | Excel转二进制工具 2 | === 3 | 4 | 目的: 5 | --- 6 | 游戏开放中有原型数据,而原型数据经常都是通过excel进行编写,关于程序如何去读取excel中的数据,方法有很多种,比如:
7 | 1.将excel导入mysql中,然后由程序去读取数据库,但是客户端无法读取数据库,经常还要重新给客户端导出xml,比较麻烦
8 | 2.客户端服务器直接读取excel,excel是比较大的,不适合存放在客户端
9 | 10 | excel2db-write 11 | --- 12 | 将excel转成ndb文件(二进制文件),格式参考:二进制格式文档
13 | 读取excel,根据定义好的格式写入ndb文件中,同时根据指定不同的语言生成各自的bean类
14 | 使用: 15 | pom.xml打包,将target中的zip包拷贝出来,根据附录1中的参数说明填写参数,提供了测试的excel见附录2 16 | 17 | excel2db-read 18 | --- 19 | 提供java版本的解析ndb文件,并将结果映射到bean中 20 | 21 | excel2db-browse 22 | --- 23 | 提供查看ndb文件的客户端,将ndb文件拖入到窗口中查看 24 | 25 | 附录 26 | --- 27 | 1.bat中参数说明:
28 | language 指定语言支持java,csharp
29 | beanRoot 指定bean的生成路径,一般都是指定到我们项目中
30 | packageRoot 指定bean的包路径
31 | excelPath 指定excel的存放路径
32 | ndbPath 指定ndb的存放路径
33 | 34 | 2.test.xls 指定了excel的格式,兼容excel2003和2007
35 | 第三行指定类型分别有:int,float,long,string,double,ints,floats,longs,strings,doubles
36 | -------------------------------------------------------------------------------- /excel2db-browse/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /log -------------------------------------------------------------------------------- /excel2db-browse/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.excel2db 8 | excel2db 9 | 0.0.1-SNAPSHOT 10 | 11 | excel2db-browse 12 | excel2db-browse 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-jar-plugin 23 | 2.3.2 24 | 25 | 26 | false 27 | 28 | true 29 | lib/ 30 | org.excel2db.browse.NDBBrowseFrame 31 | 32 | 33 | 34 | 35 | 36 | 37 | org.apache.maven.plugins 38 | maven-assembly-plugin 39 | 2.4 40 | 41 | 42 | src/main/resources/package.xml 43 | 44 | 45 | 46 | 47 | make-assembly 48 | package 49 | 50 | single 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | com.excel2db 61 | excel2db-read 62 | 0.0.1-SNAPSHOT 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /excel2db-browse/scripts/x-java-startup.bat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ksfzhaohui/excel2db/781edd6d7ff172501f67355bcb491b2f6e01806a/excel2db-browse/scripts/x-java-startup.bat -------------------------------------------------------------------------------- /excel2db-browse/src/main/java/org/excel2db/browse/NDBBrowseFrame.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.browse; 2 | 3 | import java.awt.BorderLayout; 4 | import java.awt.datatransfer.DataFlavor; 5 | import java.awt.dnd.DnDConstants; 6 | import java.awt.dnd.DropTarget; 7 | import java.awt.dnd.DropTargetAdapter; 8 | import java.awt.dnd.DropTargetDropEvent; 9 | import java.io.File; 10 | import java.lang.reflect.Array; 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | import javax.swing.JFrame; 15 | import javax.swing.JScrollPane; 16 | import javax.swing.JTable; 17 | import javax.swing.ListSelectionModel; 18 | 19 | import org.apache.log4j.Logger; 20 | import org.excel2db.read.DBFile; 21 | import org.excel2db.read.util.Constants; 22 | 23 | /** 24 | * 浏览ndb文件的窗口 25 | * 26 | * @author zhaohui 27 | * 28 | */ 29 | public class NDBBrowseFrame extends JFrame { 30 | 31 | private final static Logger logger = Logger.getLogger(NDBBrowseFrame.class); 32 | private static final long serialVersionUID = 1L; 33 | 34 | private static final int frame_width = 800; 35 | private static final int frame_height = 500; 36 | private JTable table; 37 | private JScrollPane scrollPane; 38 | 39 | public NDBBrowseFrame() { 40 | table = new JTable(); 41 | table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 42 | scrollPane = new JScrollPane(table); 43 | this.add(scrollPane, BorderLayout.CENTER); 44 | 45 | handlerDrag(); 46 | this.setTitle("NDB Browser"); 47 | this.setSize(frame_width, frame_height); 48 | this.setResizable(false); 49 | this.setVisible(true); 50 | this.setLocationRelativeTo(null); 51 | this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 52 | } 53 | 54 | /** 55 | * 定义的拖拽方法 56 | */ 57 | private void handlerDrag() { 58 | new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE, 59 | new DropTargetAdapter() { 60 | @Override 61 | public void drop(DropTargetDropEvent dtde) { 62 | try { 63 | if (dtde.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { 64 | dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE); 65 | @SuppressWarnings("unchecked") 66 | List list = (List) (dtde 67 | .getTransferable() 68 | .getTransferData(DataFlavor.javaFileListFlavor)); 69 | 70 | browseNDB(list.get(0)); 71 | dtde.dropComplete(true); 72 | } else { 73 | dtde.rejectDrop(); 74 | } 75 | } catch (Exception e) { 76 | logger.error("", e); 77 | } 78 | } 79 | }); 80 | } 81 | 82 | /** 83 | * 浏览ndb中的数据 84 | * 85 | * @param file 86 | * 拖进来的ndb文件 87 | */ 88 | private void browseNDB(File file) { 89 | DBFile dbFile = new DBFile(); 90 | dbFile.init(file.getAbsolutePath()); 91 | 92 | List> dataMap = dbFile.getDataMap(); 93 | Object[] columnNames = dbFile.getColumnNames().toArray(); 94 | Object[][] tableVales = new Object[dataMap.size()][columnNames.length]; 95 | for (int i = 0; i < dataMap.size(); i++) { 96 | Map map = dataMap.get(i); 97 | Object tableValue[] = new Object[columnNames.length]; 98 | for (int index = 0; index < columnNames.length; index++) { 99 | tableValue[index] = map.get(columnNames[index]); 100 | 101 | if (tableValue[index].getClass().isArray()) { 102 | StringBuffer sb = new StringBuffer(); 103 | int len = Array.getLength(tableValue[index]); 104 | for (int j = 0; j < len; j++) { 105 | sb.append(Array.get(tableValue[index], j)); 106 | if (j != len - 1) { 107 | sb.append(Constants.SEPARATOR); 108 | } 109 | } 110 | 111 | tableValue[index] = sb.toString(); 112 | } 113 | } 114 | tableVales[i] = tableValue; 115 | } 116 | 117 | table.setModel(new NDBTableModel(columnNames, tableVales)); 118 | } 119 | 120 | public static void main(String[] args) { 121 | new NDBBrowseFrame(); 122 | } 123 | 124 | } 125 | -------------------------------------------------------------------------------- /excel2db-browse/src/main/java/org/excel2db/browse/NDBTableModel.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.browse; 2 | 3 | import javax.swing.table.AbstractTableModel; 4 | 5 | public class NDBTableModel extends AbstractTableModel { 6 | 7 | private static final long serialVersionUID = 1L; 8 | 9 | private Object[] columnNames; 10 | private Object[][] tableVales; 11 | 12 | public NDBTableModel(Object[] columnNames, Object[][] tableVales) { 13 | this.columnNames = columnNames; 14 | this.tableVales = tableVales; 15 | } 16 | 17 | @Override 18 | public int getRowCount() { 19 | return tableVales.length; 20 | } 21 | 22 | @Override 23 | public int getColumnCount() { 24 | return columnNames.length; 25 | } 26 | 27 | @Override 28 | public String getColumnName(int column) { 29 | return (String) columnNames[column]; 30 | } 31 | 32 | @Override 33 | public Object getValueAt(int rowIndex, int columnIndex) { 34 | return tableVales[rowIndex][columnIndex]; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /excel2db-browse/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO,fileA,consoleA 2 | 3 | log4j.appender.fileA=org.apache.log4j.DailyRollingFileAppender 4 | log4j.appender.fileA.File=./logs/excel2db-browse.log 5 | log4j.appender.fileA.append=true 6 | log4j.appender.fileA.ImmediateFlush=true 7 | log4j.appender.fileA.layout=org.apache.log4j.PatternLayout 8 | log4j.appender.fileA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %L::: %-5p %C - %m%n 9 | 10 | log4j.appender.consoleA=org.apache.log4j.ConsoleAppender 11 | log4j.appender.consoleA.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.consoleA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} - %m%n -------------------------------------------------------------------------------- /excel2db-browse/src/main/resources/package.xml: -------------------------------------------------------------------------------- 1 | 2 | bin 3 | 4 | 5 | zip 6 | 7 | 8 | 9 | 10 | 11 | 12 | false 13 | lib 14 | false 15 | 16 | 17 | 18 | 19 | 20 | 21 | scripts 22 | 23 | 24 | *.bat 25 | 26 | 27 | 28 | 29 | ${project.build.directory} 30 | 31 | 32 | *.jar 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /excel2db-read/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /log -------------------------------------------------------------------------------- /excel2db-read/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.excel2db 8 | excel2db 9 | 0.0.1-SNAPSHOT 10 | 11 | excel2db-read 12 | excel2db-read 13 | http://maven.apache.org 14 | 15 | UTF-8 16 | 17 | 18 | -------------------------------------------------------------------------------- /excel2db-read/src/main/java/org/excel2db/read/DBFile.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.read; 2 | 3 | import java.io.FileInputStream; 4 | import java.io.IOException; 5 | import java.io.UnsupportedEncodingException; 6 | import java.nio.ByteBuffer; 7 | import java.nio.channels.FileChannel; 8 | import java.util.ArrayList; 9 | import java.util.HashMap; 10 | import java.util.List; 11 | import java.util.Map; 12 | 13 | import org.apache.log4j.Logger; 14 | import org.excel2db.read.util.Constants; 15 | import org.excel2db.read.util.TypeEnum; 16 | 17 | /** 18 | * ndb文件的抽象类 19 | * 20 | * @author zhaohui 21 | * 22 | * @param 23 | */ 24 | public class DBFile { 25 | private final static Logger logger = Logger.getLogger(DBFile.class); 26 | 27 | /** header的长度 **/ 28 | private static final int HEADER_LENGTH = 5 * 4; 29 | /** 字符串编码格式 **/ 30 | private static final String STRING_ENCODING = "UTF-8"; 31 | 32 | private Header header; 33 | private List columnNames = new ArrayList(); 34 | private List typeEnums = new ArrayList(); 35 | private List> dataMap = new ArrayList>(); 36 | 37 | public void init(String filePath) { 38 | FileChannel fc = null; 39 | try { 40 | fc = new FileInputStream(filePath).getChannel(); 41 | parseHeader(fc); 42 | parseColumnName(fc); 43 | parseColumnType(fc); 44 | parseDate(fc); 45 | } catch (Exception e) { 46 | logger.error("init dbfile error", e); 47 | } finally { 48 | if (fc != null) { 49 | try { 50 | fc.close(); 51 | } catch (IOException e) { 52 | } 53 | } 54 | } 55 | } 56 | 57 | /** 58 | * 解析字段的类型 59 | * 60 | * @param fc 61 | * @throws IOException 62 | */ 63 | private void parseDate(FileChannel fc) throws IOException { 64 | ByteBuffer buffer = ByteBuffer.allocate(header.getDatabuflength()); 65 | fc.read(buffer); 66 | buffer.flip(); 67 | for (int i = 0; i < header.getRecordSize(); i++) { 68 | Map map = new HashMap(); 69 | for (int k = 0; k < typeEnums.size(); k++) { 70 | TypeEnum type = typeEnums.get(k); 71 | String column = columnNames.get(k); 72 | Object obj = null; 73 | if (type == TypeEnum.INT) { 74 | obj = buffer.getInt(); 75 | } else if (type == TypeEnum.FLOAT) { 76 | obj = buffer.getFloat(); 77 | } else if (type == TypeEnum.LONG) { 78 | obj = buffer.getLong(); 79 | } else if (type == TypeEnum.STRING) { 80 | obj = getString(buffer); 81 | } else if (type == TypeEnum.DOUBLE) { 82 | obj = buffer.getDouble(); 83 | } else if (type == TypeEnum.INTS) { 84 | String values[] = getString(buffer).split( 85 | Constants.SEPARATOR); 86 | int ints[] = new int[values.length]; 87 | for (int j = 0; j < values.length; j++) { 88 | ints[j] = Integer.valueOf(values[j]); 89 | } 90 | obj = ints; 91 | } else if (type == TypeEnum.FLOATS) { 92 | String values[] = getString(buffer).split( 93 | Constants.SEPARATOR); 94 | float floats[] = new float[values.length]; 95 | for (int j = 0; j < values.length; j++) { 96 | floats[j] = Float.valueOf(values[j]); 97 | } 98 | obj = floats; 99 | } else if (type == TypeEnum.LONGS) { 100 | String values[] = getString(buffer).split( 101 | Constants.SEPARATOR); 102 | long longs[] = new long[values.length]; 103 | for (int j = 0; j < values.length; j++) { 104 | longs[j] = Long.valueOf(values[j]); 105 | } 106 | obj = longs; 107 | } else if (type == TypeEnum.STRINGS) { 108 | obj = getString(buffer).split(Constants.SEPARATOR); 109 | } else if (type == TypeEnum.DOUBLES) { 110 | String values[] = getString(buffer).split( 111 | Constants.SEPARATOR); 112 | double doubles[] = new double[values.length]; 113 | for (int j = 0; j < values.length; j++) { 114 | doubles[j] = Double.valueOf(values[j]); 115 | } 116 | obj = doubles; 117 | } 118 | map.put(column, obj); 119 | } 120 | dataMap.add(map); 121 | } 122 | } 123 | 124 | /** 125 | * 解析字段的类型 126 | * 127 | * @param fc 128 | * @throws IOException 129 | */ 130 | private void parseColumnType(FileChannel fc) throws IOException { 131 | ByteBuffer buffer = ByteBuffer.allocate(header.getTypebuflength()); 132 | fc.read(buffer); 133 | buffer.flip(); 134 | for (int i = 0; i < header.getFieldSize(); i++) { 135 | byte b = buffer.get(); 136 | typeEnums.add(TypeEnum.type(b)); 137 | } 138 | logger.info(typeEnums); 139 | } 140 | 141 | /** 142 | * 解析字段的名称 143 | * 144 | * @param fc 145 | * @throws IOException 146 | */ 147 | private void parseColumnName(FileChannel fc) throws IOException { 148 | ByteBuffer buffer = ByteBuffer.allocate(header.getNamebuflength()); 149 | fc.read(buffer); 150 | buffer.flip(); 151 | for (int i = 0; i < header.getFieldSize(); i++) { 152 | columnNames.add(getString(buffer)); 153 | } 154 | logger.info(columnNames); 155 | } 156 | 157 | /** 158 | * 解析头部 159 | * 160 | * @param fc 161 | * @throws IOException 162 | */ 163 | private void parseHeader(FileChannel fc) throws IOException { 164 | ByteBuffer buffer = ByteBuffer.allocate(HEADER_LENGTH); 165 | fc.read(buffer); 166 | buffer.flip(); 167 | int records = buffer.getInt(); 168 | int fields = buffer.getInt(); 169 | int namebuflength = buffer.getInt(); 170 | int typebuflength = buffer.getInt(); 171 | int databuflength = buffer.getInt(); 172 | header = new Header(records, fields, namebuflength, typebuflength, 173 | databuflength); 174 | logger.info(header.toString()); 175 | } 176 | 177 | private String getString(ByteBuffer buffer) 178 | throws UnsupportedEncodingException { 179 | int len = buffer.getInt(); 180 | byte dst[] = new byte[len]; 181 | buffer.get(dst); 182 | return new String(dst, STRING_ENCODING); 183 | } 184 | 185 | public List> getDataMap() { 186 | return dataMap; 187 | } 188 | 189 | public List getColumnNames() { 190 | return columnNames; 191 | } 192 | 193 | public List getTypeEnums() { 194 | return typeEnums; 195 | } 196 | 197 | public void setTypeEnums(List typeEnums) { 198 | this.typeEnums = typeEnums; 199 | } 200 | 201 | } 202 | -------------------------------------------------------------------------------- /excel2db-read/src/main/java/org/excel2db/read/Header.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.read; 2 | 3 | public class Header { 4 | 5 | /** 记录数量 **/ 6 | private int recordSize; 7 | /** 字段数量 **/ 8 | private int fieldSize; 9 | /** 名称buff的长度 **/ 10 | private int namebuflength; 11 | /** 类型buff的长度 **/ 12 | private int typebuflength; 13 | /** 数据buff的长度 **/ 14 | private int databuflength; 15 | 16 | public Header(int recordSize, int fieldSize, int namebuflength, 17 | int typebuflength, int databuflength) { 18 | this.recordSize = recordSize; 19 | this.fieldSize = fieldSize; 20 | this.namebuflength = namebuflength; 21 | this.typebuflength = typebuflength; 22 | this.databuflength = databuflength; 23 | } 24 | 25 | @Override 26 | public String toString() { 27 | return "headerInfo:[recordSize=" + recordSize + ",fieldSize=" 28 | + fieldSize + ",namebuflength=" + namebuflength 29 | + ",typebuflength=" + typebuflength + ",databuflength=" 30 | + databuflength + "]"; 31 | } 32 | 33 | public int getRecordSize() { 34 | return recordSize; 35 | } 36 | 37 | public void setRecordSize(int recordSize) { 38 | this.recordSize = recordSize; 39 | } 40 | 41 | public int getFieldSize() { 42 | return fieldSize; 43 | } 44 | 45 | public void setFieldSize(int fieldSize) { 46 | this.fieldSize = fieldSize; 47 | } 48 | 49 | public int getNamebuflength() { 50 | return namebuflength; 51 | } 52 | 53 | public void setNamebuflength(int namebuflength) { 54 | this.namebuflength = namebuflength; 55 | } 56 | 57 | public int getTypebuflength() { 58 | return typebuflength; 59 | } 60 | 61 | public void setTypebuflength(int typebuflength) { 62 | this.typebuflength = typebuflength; 63 | } 64 | 65 | public int getDatabuflength() { 66 | return databuflength; 67 | } 68 | 69 | public void setDatabuflength(int databuflength) { 70 | this.databuflength = databuflength; 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /excel2db-read/src/main/java/org/excel2db/read/NdbDataManager.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.read; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import java.util.Map; 7 | 8 | import org.excel2db.read.util.BeanUtil; 9 | import org.excel2db.read.util.SuffixUtil; 10 | 11 | /** 12 | * ndb管理抽象类 13 | * 14 | * @author zhaohui 15 | * 16 | * @param 17 | */ 18 | public abstract class NdbDataManager { 19 | 20 | private DBFile dbFile; 21 | private String ndbFilePath; 22 | private List beanList = new ArrayList(); 23 | 24 | public abstract String getNdbName(); 25 | 26 | public abstract Class clazz(); 27 | 28 | public void init() { 29 | dbFile = new DBFile(); 30 | dbFile.init(ndbFilePath + File.separator + getNdbName() 31 | + SuffixUtil.getFileSuffix()); 32 | 33 | List> dataMap = dbFile.getDataMap(); 34 | for (Map map : dataMap) { 35 | beanList.add(BeanUtil.reverse(map, clazz())); 36 | } 37 | } 38 | 39 | public List getBeanList() { 40 | return beanList; 41 | } 42 | 43 | public String getNdbFilePath() { 44 | return ndbFilePath; 45 | } 46 | 47 | public void setNdbFilePath(String ndbFilePath) { 48 | this.ndbFilePath = ndbFilePath; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /excel2db-read/src/main/java/org/excel2db/read/util/BeanUtil.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.read.util; 2 | 3 | import java.beans.Introspector; 4 | import java.beans.PropertyDescriptor; 5 | import java.lang.reflect.Method; 6 | import java.util.Map; 7 | 8 | import org.apache.log4j.Logger; 9 | 10 | /** 11 | * Bean/Map 转换工具 12 | * 13 | * @author zhaohui 14 | * 15 | */ 16 | public class BeanUtil { 17 | private static Logger logger = Logger.getLogger(BeanUtil.class); 18 | private static final String CLASS = "class"; 19 | 20 | /** 21 | * 将map转成指定的对象 22 | * 23 | * @param beanMap 24 | * map数据 25 | * @param clazz 26 | * 指定的类对象 27 | * @return 28 | */ 29 | public static T reverse(Map beanMap, Class clazz) { 30 | T bean = getBean(clazz); 31 | try { 32 | PropertyDescriptor[] ps = Introspector.getBeanInfo(clazz) 33 | .getPropertyDescriptors(); 34 | for (PropertyDescriptor propertyDescriptor : ps) { 35 | String propertyName = propertyDescriptor.getName(); 36 | if (propertyName != null && !propertyName.equals(CLASS)) { 37 | Method setter = propertyDescriptor.getWriteMethod(); 38 | Object value = beanMap.get(propertyName); 39 | if (setter != null && value != null) { 40 | setter.invoke(bean, value); 41 | } 42 | } 43 | } 44 | } catch (Exception e) { 45 | logger.error(e); 46 | e.printStackTrace(); 47 | } 48 | return bean; 49 | } 50 | 51 | @SuppressWarnings("unchecked") 52 | private static T getBean(Class _class) { 53 | try { 54 | return (T) Class.forName(_class.getName()).newInstance(); 55 | } catch (Exception e) { 56 | logger.error(e); 57 | } 58 | return null; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /excel2db-read/src/main/java/org/excel2db/read/util/Constants.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.read.util; 2 | 3 | public class Constants { 4 | 5 | /** excel配置数组的分隔符 **/ 6 | public static final String SEPARATOR = "#"; 7 | } 8 | -------------------------------------------------------------------------------- /excel2db-read/src/main/java/org/excel2db/read/util/SuffixUtil.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.read.util; 2 | 3 | import java.io.FileInputStream; 4 | import java.util.Properties; 5 | 6 | import org.apache.log4j.Logger; 7 | 8 | public class SuffixUtil { 9 | 10 | private final static Logger logger = Logger.getLogger(SuffixUtil.class); 11 | 12 | private static Properties prop = new Properties(); 13 | 14 | static { 15 | try { 16 | prop.load(new FileInputStream("suffix.properties")); 17 | } catch (Exception e) { 18 | logger.error("", e); 19 | } 20 | } 21 | 22 | public static String getFileSuffix() { 23 | return prop.getProperty("file_suffix"); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /excel2db-read/src/main/java/org/excel2db/read/util/TypeEnum.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.read.util; 2 | 3 | public enum TypeEnum { 4 | 5 | INT('i'), FLOAT('f'), LONG('l'), STRING('s'), DOUBLE('d'), INTS('x'), FLOATS( 6 | 'y'), LONGS('z'), STRINGS('j'), DOUBLES('k'); 7 | 8 | private char value; 9 | 10 | private TypeEnum(char value) { 11 | this.value = value; 12 | } 13 | 14 | public char getValue() { 15 | return value; 16 | } 17 | 18 | public static TypeEnum type(byte b) { 19 | char type = (char) b; 20 | if (type == 'i') { 21 | return INT; 22 | } else if (type == 'f') { 23 | return FLOAT; 24 | } else if (type == 'l') { 25 | return LONG; 26 | } else if (type == 's') { 27 | return STRING; 28 | } else if (type == 'd') { 29 | return DOUBLE; 30 | } else if (type == 'x') { 31 | return INTS; 32 | } else if (type == 'y') { 33 | return FLOATS; 34 | } else if (type == 'z') { 35 | return LONGS; 36 | } else if (type == 'j') { 37 | return STRINGS; 38 | } else if (type == 'k') { 39 | return DOUBLES; 40 | } else { 41 | throw new RuntimeException( 42 | "error type:" 43 | + type 44 | + "support:int,float,long,string,double,ints,floats,longs,strings,doubles"); 45 | } 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /excel2db-read/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO,fileA,consoleA 2 | 3 | log4j.appender.fileA=org.apache.log4j.DailyRollingFileAppender 4 | log4j.appender.fileA.File=./logs/excel2db-read.log 5 | log4j.appender.fileA.append=true 6 | log4j.appender.fileA.ImmediateFlush=true 7 | log4j.appender.fileA.layout=org.apache.log4j.PatternLayout 8 | log4j.appender.fileA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %L::: %-5p %C - %m%n 9 | 10 | log4j.appender.consoleA=org.apache.log4j.ConsoleAppender 11 | log4j.appender.consoleA.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.consoleA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} - %m%n -------------------------------------------------------------------------------- /excel2db-read/src/test/java/test/ArrayTestData.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | public class ArrayTestData { 4 | 5 | private int[] pid; 6 | private float[] name2; 7 | private String[] name3; 8 | private double[] name4; 9 | private long[] name5; 10 | 11 | public int[] getPid() { 12 | return pid; 13 | } 14 | 15 | public void setPid(int[] pid) { 16 | this.pid = pid; 17 | } 18 | 19 | public float[] getName2() { 20 | return name2; 21 | } 22 | 23 | public void setName2(float[] name2) { 24 | this.name2 = name2; 25 | } 26 | 27 | public String[] getName3() { 28 | return name3; 29 | } 30 | 31 | public void setName3(String[] name3) { 32 | this.name3 = name3; 33 | } 34 | 35 | public double[] getName4() { 36 | return name4; 37 | } 38 | 39 | public void setName4(double[] name4) { 40 | this.name4 = name4; 41 | } 42 | 43 | public long[] getName5() { 44 | return name5; 45 | } 46 | 47 | public void setName5(long[] name5) { 48 | this.name5 = name5; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /excel2db-read/src/test/java/test/ArrayTestDataManager.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import java.util.List; 4 | 5 | import org.excel2db.read.NdbDataManager; 6 | 7 | public class ArrayTestDataManager extends NdbDataManager { 8 | 9 | @Override 10 | public void init() { 11 | super.init(); 12 | 13 | List list = getBeanList(); 14 | for (ArrayTestData array : list) { 15 | System.out.println(array.getPid().length + "," + array.getName2().length + "," 16 | + array.getName3().length + "," + array.getName4().length + "," 17 | + array.getName5().length); 18 | } 19 | } 20 | 21 | @Override 22 | public String getNdbName() { 23 | return "P_arrayTest"; 24 | } 25 | 26 | @Override 27 | public Class clazz() { 28 | return ArrayTestData.class; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /excel2db-read/src/test/java/test/GeneralData.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | public class GeneralData { 4 | 5 | private int name1; 6 | private long name2; 7 | private float name3; 8 | private String name4; 9 | 10 | public int getName1() { 11 | return name1; 12 | } 13 | 14 | public void setName1(int name1) { 15 | this.name1 = name1; 16 | } 17 | 18 | public long getName2() { 19 | return name2; 20 | } 21 | 22 | public void setName2(long name2) { 23 | this.name2 = name2; 24 | } 25 | 26 | public float getName3() { 27 | return name3; 28 | } 29 | 30 | public void setName3(float name3) { 31 | this.name3 = name3; 32 | } 33 | 34 | public String getName4() { 35 | return name4; 36 | } 37 | 38 | public void setName4(String name4) { 39 | this.name4 = name4; 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /excel2db-read/src/test/java/test/GeneralDataManager.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | import java.util.List; 4 | 5 | import org.excel2db.read.NdbDataManager; 6 | 7 | public class GeneralDataManager extends NdbDataManager { 8 | 9 | @Override 10 | public void init() { 11 | super.init(); 12 | 13 | List list = getBeanList(); 14 | for (GeneralData city : list) { 15 | System.out.println(city.getName1() + "," + city.getName2() + "," 16 | + city.getName3() + "," + city.getName4()); 17 | } 18 | } 19 | 20 | @Override 21 | public String getNdbName() { 22 | return "general"; 23 | } 24 | 25 | @Override 26 | public Class clazz() { 27 | return GeneralData.class; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /excel2db-read/src/test/java/test/ReadMain.java: -------------------------------------------------------------------------------- 1 | package test; 2 | 3 | public class ReadMain { 4 | 5 | public static void main(String[] args) { 6 | // GeneralDataManager generalDataManager = new GeneralDataManager(); 7 | // generalDataManager.setNdbFilePath("D:\\ndbtest"); 8 | // generalDataManager.init(); 9 | 10 | ArrayTestDataManager arrayTestDataManager = new ArrayTestDataManager(); 11 | arrayTestDataManager.setNdbFilePath("D:\\ndbtest"); 12 | arrayTestDataManager.init(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /excel2db-read/suffix.properties: -------------------------------------------------------------------------------- 1 | file_suffix = .bytes -------------------------------------------------------------------------------- /excel2db-write/.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /log -------------------------------------------------------------------------------- /excel2db-write/config.properties: -------------------------------------------------------------------------------- 1 | sheetStartWith = P_ 2 | file_suffix = .bytes -------------------------------------------------------------------------------- /excel2db-write/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.excel2db 8 | excel2db 9 | 0.0.1-SNAPSHOT 10 | 11 | excel2db-write 12 | 13 | excel2db-write 14 | http://maven.apache.org 15 | 16 | UTF-8 17 | 18 | 19 | 20 | 21 | 22 | org.apache.maven.plugins 23 | maven-jar-plugin 24 | 2.3.2 25 | 26 | 27 | false 28 | 29 | true 30 | lib/ 31 | org.excel2db.write.WriteMain 32 | 33 | 34 | 35 | 36 | 37 | 38 | org.apache.maven.plugins 39 | maven-assembly-plugin 40 | 2.4 41 | 42 | 43 | src/main/resources/package.xml 44 | 45 | 46 | 47 | 48 | make-assembly 49 | package 50 | 51 | single 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | org.apache.poi 62 | poi 63 | 3.7 64 | 65 | 66 | org.apache.poi 67 | poi-ooxml 68 | 3.7 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /excel2db-write/scripts/x-csharp-startup.bat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ksfzhaohui/excel2db/781edd6d7ff172501f67355bcb491b2f6e01806a/excel2db-write/scripts/x-csharp-startup.bat -------------------------------------------------------------------------------- /excel2db-write/scripts/x-java-startup.bat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ksfzhaohui/excel2db/781edd6d7ff172501f67355bcb491b2f6e01806a/excel2db-write/scripts/x-java-startup.bat -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/WriteMain.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write; 2 | 3 | import java.io.File; 4 | import java.util.List; 5 | 6 | import org.apache.log4j.Logger; 7 | import org.excel2db.write.genClass.Config; 8 | import org.excel2db.write.manager.ExcelParse; 9 | import org.excel2db.write.manager.GeneratorBeanClass; 10 | import org.excel2db.write.manager.NDBGenerator; 11 | import org.excel2db.write.util.ConfigUtil; 12 | import org.excel2db.write.util.EndfixEnum; 13 | import org.excel2db.write.util.FileUtil; 14 | 15 | public class WriteMain { 16 | private final static Logger logger = Logger.getLogger(WriteMain.class); 17 | 18 | public static void main(String[] args) { 19 | if (args == null || args.length == 0) { 20 | args = new String[5]; 21 | args[0] = "java"; 22 | args[1] = "D:\\ndbtest"; 23 | args[2] = "test"; 24 | args[3] = "D:\\ndbtest"; 25 | args[4] = "D:\\ndbtest"; 26 | } 27 | 28 | Config config = getConfig(args); 29 | String excelPath = config.getExcelPath(); 30 | excelPath = excelPath + File.separator; 31 | logger.info("path:" + excelPath); 32 | List fileList = FileUtil.getFileList(excelPath); 33 | logger.info("fileList:" + fileList); 34 | 35 | FileUtil.deleteAllFilesOfDir(new File(config.getNdbPath()), 36 | ConfigUtil.getFileSuffix()); 37 | FileUtil.deleteAllFilesOfDir(new File(config.getBeanRoot()), 38 | EndfixEnum.endfix(config.getLanguage())); 39 | 40 | for (String fileName : fileList) { 41 | logger.info("++++++handle file:【" + fileName + "】++++++"); 42 | ExcelParse excel = new ExcelParse(excelPath + fileName); 43 | excel.readExcel(); 44 | 45 | NDBGenerator db = new NDBGenerator(excel); 46 | db.writeDB(config.getNdbPath() + File.separator); 47 | 48 | GeneratorBeanClass generator = new GeneratorBeanClass(excel, config); 49 | generator.generator(); 50 | } 51 | } 52 | 53 | private static Config getConfig(String[] args) { 54 | if (args == null || args.length < 1) { 55 | logger.error("error init param:[language,beanRoot,namespace,excelPath,ndbPath]"); 56 | System.exit(0); 57 | } 58 | Config config = null; 59 | if (args.length != 5 && args.length != 4) { 60 | logger.error("error init param:[language,beanRoot,namespace,excelPath,ndbPath]"); 61 | System.exit(0); 62 | } 63 | if (args.length == 5) { 64 | config = new Config(args[0], args[1], args[2], args[3], args[4]); 65 | } else if (args.length == 4) { 66 | config = new Config(args[0], args[1], "", args[2], args[3]); 67 | } 68 | return config; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/genClass/AbstractGenerator.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.genClass; 2 | 3 | import java.io.File; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.io.PrintStream; 7 | 8 | import org.apache.log4j.Logger; 9 | import org.excel2db.write.util.EndfixEnum; 10 | 11 | public abstract class AbstractGenerator implements IGenerator { 12 | 13 | private final static Logger logger = Logger 14 | .getLogger(AbstractGenerator.class); 15 | 16 | protected SheetInfo info; 17 | protected Config config; 18 | protected PrintStream out; 19 | 20 | @Override 21 | public void generator(SheetInfo info, Config config) { 22 | this.info = info; 23 | this.config = config; 24 | 25 | String beanRoot = config.getBeanRoot(); 26 | File dir = new File(beanRoot); 27 | if (!dir.exists()) { 28 | dir.mkdirs(); 29 | } 30 | 31 | String language = config.getLanguage(); 32 | info.setName(toFirstUpperCase(info.getName())); 33 | 34 | String fullName = beanRoot + File.separator + info.getName() 35 | + EndfixEnum.endfix(language); 36 | 37 | File beanFile = new File(fullName); 38 | if (beanFile.exists()) { 39 | beanFile.delete(); 40 | } 41 | try { 42 | beanFile.createNewFile(); 43 | out = new PrintStream(new FileOutputStream(beanFile)); 44 | generatorBean(); 45 | } catch (IOException e) { 46 | logger.error("generator bean error", e); 47 | throw new RuntimeException(e); 48 | } finally { 49 | if (out != null) { 50 | out.close(); 51 | } 52 | } 53 | } 54 | 55 | protected String toFirstUpperCase(String columnName) { 56 | String firstStr = columnName.substring(0, 1); 57 | columnName = columnName.replaceFirst(firstStr, firstStr.toUpperCase()); 58 | return columnName; 59 | } 60 | 61 | public abstract void generatorBean(); 62 | } 63 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/genClass/Config.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.genClass; 2 | 3 | /** 4 | * 初始化配置信息 5 | * 6 | * @author zhaohui 7 | * 8 | */ 9 | public class Config { 10 | 11 | private String language;// 语言 12 | private String beanRoot;// bean生成路径 13 | private String packageRoot;// 包路径 14 | private String excelPath; 15 | private String ndbPath; 16 | 17 | public Config(String language, String beanRoot, String packageRoot, 18 | String excelPath, String ndbPath) { 19 | this.language = language; 20 | this.beanRoot = beanRoot; 21 | this.packageRoot = packageRoot; 22 | this.excelPath = excelPath; 23 | this.ndbPath = ndbPath; 24 | } 25 | 26 | public Config(String language, String beanRoot, String excelPath) { 27 | this.language = language; 28 | this.beanRoot = beanRoot; 29 | this.excelPath = excelPath; 30 | } 31 | 32 | public String getLanguage() { 33 | return language; 34 | } 35 | 36 | public void setLanguage(String language) { 37 | this.language = language; 38 | } 39 | 40 | public String getBeanRoot() { 41 | return beanRoot; 42 | } 43 | 44 | public void setBeanRoot(String beanRoot) { 45 | this.beanRoot = beanRoot; 46 | } 47 | 48 | public String getPackageRoot() { 49 | return packageRoot; 50 | } 51 | 52 | public void setPackageRoot(String packageRoot) { 53 | this.packageRoot = packageRoot; 54 | } 55 | 56 | public String getExcelPath() { 57 | return excelPath; 58 | } 59 | 60 | public void setExcelPath(String excelPath) { 61 | this.excelPath = excelPath; 62 | } 63 | 64 | public String getNdbPath() { 65 | return ndbPath; 66 | } 67 | 68 | public void setNdbPath(String ndbPath) { 69 | this.ndbPath = ndbPath; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/genClass/GeneratorFactory.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.genClass; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | 6 | import org.excel2db.write.genClass.generator.CSharpGenerator; 7 | import org.excel2db.write.genClass.generator.JavaGenerator; 8 | 9 | public class GeneratorFactory { 10 | 11 | private static Map generatorMap = new HashMap(); 12 | 13 | static { 14 | generatorMap.put("java", new JavaGenerator()); 15 | generatorMap.put("csharp", new CSharpGenerator()); 16 | } 17 | 18 | public static IGenerator getGenerator(String language) { 19 | IGenerator generator = generatorMap.get(language); 20 | if (generator == null) { 21 | throw new RuntimeException("error language:" + language 22 | + " support:java,csharp"); 23 | } 24 | return generator; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/genClass/IGenerator.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.genClass; 2 | 3 | public interface IGenerator { 4 | 5 | public void generator(SheetInfo info, Config config); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/genClass/SheetInfo.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.genClass; 2 | 3 | import java.util.List; 4 | 5 | import org.excel2db.write.util.TypeEnum; 6 | 7 | public class SheetInfo { 8 | 9 | private String name; 10 | private List columnNames; 11 | private List typeEnums; 12 | 13 | public SheetInfo(String name, List columnNames, 14 | List typeEnums) { 15 | this.name = name; 16 | this.columnNames = columnNames; 17 | this.typeEnums = typeEnums; 18 | } 19 | 20 | public String getName() { 21 | return name; 22 | } 23 | 24 | public void setName(String name) { 25 | this.name = name; 26 | } 27 | 28 | public List getColumnNames() { 29 | return columnNames; 30 | } 31 | 32 | public void setColumnNames(List columnNames) { 33 | this.columnNames = columnNames; 34 | } 35 | 36 | public List getTypeEnums() { 37 | return typeEnums; 38 | } 39 | 40 | public void setTypeEnums(List typeEnums) { 41 | this.typeEnums = typeEnums; 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/genClass/generator/CSharpGenerator.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.genClass.generator; 2 | 3 | import java.util.List; 4 | 5 | import org.excel2db.write.genClass.AbstractGenerator; 6 | import org.excel2db.write.genClass.sign.CSharpSign; 7 | import org.excel2db.write.util.TypeEnum; 8 | 9 | /** 10 | * csharp类生成器 11 | * 12 | * @author zhaohui 13 | * 14 | */ 15 | public class CSharpGenerator extends AbstractGenerator { 16 | 17 | @Override 18 | public void generatorBean() { 19 | 20 | out.println(CSharpSign.using[0]); 21 | out.println(CSharpSign.using[1]); 22 | out.println(); 23 | 24 | boolean isNameSpace = false; 25 | int index = 0; 26 | if (config.getPackageRoot() != null 27 | && !config.getPackageRoot().equals("")) { 28 | isNameSpace = true; 29 | } else { 30 | index++; 31 | } 32 | 33 | if (isNameSpace) { 34 | out.println(CSharpSign.NameSpace + config.getPackageRoot()); 35 | out.println("{"); 36 | } 37 | 38 | out.println(CSharpSign.Tab[1 - index] + CSharpSign.Public 39 | + CSharpSign.Class + toFirstUpperCase(info.getName()) 40 | + CSharpSign.Left_Brace); 41 | out.println(); 42 | 43 | List columnNames = info.getColumnNames(); 44 | List typeEnums = info.getTypeEnums(); 45 | 46 | for (int i = 0; i < typeEnums.size(); i++) { 47 | TypeEnum type = typeEnums.get(i); 48 | String columnName = columnNames.get(i); 49 | out.println(CSharpSign.Tab[2 - index] + CSharpSign.Private 50 | + CSharpSign.fullType(type) + columnName + ";"); 51 | } 52 | out.println(); 53 | 54 | for (int i = 0; i < typeEnums.size(); i++) { 55 | TypeEnum type = typeEnums.get(i); 56 | String columnName = columnNames.get(i); 57 | 58 | out.println(CSharpSign.Tab[2 - index] + CSharpSign.Public 59 | + CSharpSign.fullType(type) + toGetMethod(columnName) 60 | + CSharpSign.Left_Brace); 61 | out.println(CSharpSign.Tab[3 - index] + CSharpSign.Return 62 | + columnName + ";"); 63 | out.println(CSharpSign.Tab[2 - index] + CSharpSign.Right_Brace); 64 | out.println(); 65 | 66 | out.println(CSharpSign.Tab[2 - index] + CSharpSign.Public 67 | + CSharpSign.Void 68 | + toSetMethod(columnName, CSharpSign.fullType(type)) 69 | + CSharpSign.Left_Brace); 70 | out.println(CSharpSign.Tab[3 - index] + CSharpSign.This + "." 71 | + columnName + " = " + columnName + ";"); 72 | out.println(CSharpSign.Tab[2 - index] + CSharpSign.Right_Brace); 73 | out.println(); 74 | } 75 | out.println(CSharpSign.Tab[1 - index] + CSharpSign.Right_Brace); 76 | if (isNameSpace) { 77 | out.println(CSharpSign.Right_Brace); 78 | } 79 | } 80 | 81 | private String toGetMethod(String columnName) { 82 | return "Get" + toFirstUpperCase(columnName) + "()"; 83 | } 84 | 85 | private String toSetMethod(String columnName, String typeStr) { 86 | return "Set" + toFirstUpperCase(columnName) + "(" + typeStr 87 | + columnName + ")"; 88 | } 89 | 90 | } 91 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/genClass/generator/JavaGenerator.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.genClass.generator; 2 | 3 | import java.util.List; 4 | 5 | import org.excel2db.write.genClass.AbstractGenerator; 6 | import org.excel2db.write.genClass.sign.JavaSign; 7 | import org.excel2db.write.util.TypeEnum; 8 | 9 | /** 10 | * java类生成器 11 | * 12 | * @author zhaohui 13 | * 14 | */ 15 | public class JavaGenerator extends AbstractGenerator { 16 | 17 | @Override 18 | public void generatorBean() { 19 | if (config.getPackageRoot() != null 20 | && !config.getPackageRoot().equals("")) { 21 | out.println(JavaSign.Package + config.getPackageRoot() + ";"); 22 | out.println(); 23 | } 24 | 25 | out.println(JavaSign.Public + JavaSign.Class 26 | + toFirstUpperCase(info.getName()) + JavaSign.Left_Brace); 27 | out.println(); 28 | 29 | List columnNames = info.getColumnNames(); 30 | List typeEnums = info.getTypeEnums(); 31 | 32 | for (int i = 0; i < typeEnums.size(); i++) { 33 | TypeEnum type = typeEnums.get(i); 34 | String columnName = columnNames.get(i); 35 | out.println(JavaSign.Tab[1] + JavaSign.Private 36 | + JavaSign.fullType(type) + columnName + ";"); 37 | } 38 | out.println(); 39 | 40 | for (int i = 0; i < typeEnums.size(); i++) { 41 | TypeEnum type = typeEnums.get(i); 42 | String columnName = columnNames.get(i); 43 | 44 | out.println(JavaSign.Tab[1] + JavaSign.Public 45 | + JavaSign.fullType(type) + toGetMethod(columnName) + JavaSign.Left_Brace); 46 | out.println(JavaSign.Tab[2] + JavaSign.Return + columnName + ";"); 47 | out.println(JavaSign.Tab[1] + JavaSign.Right_Brace); 48 | out.println(); 49 | 50 | out.println(JavaSign.Tab[1] + JavaSign.Public + JavaSign.Void 51 | + toSetMethod(columnName, JavaSign.fullType(type)) + JavaSign.Left_Brace); 52 | out.println(JavaSign.Tab[2] + JavaSign.This + "." + columnName 53 | + " = " + columnName + ";"); 54 | out.println(JavaSign.Tab[1] + JavaSign.Right_Brace); 55 | out.println(); 56 | } 57 | out.println(JavaSign.Right_Brace); 58 | } 59 | 60 | private String toGetMethod(String columnName) { 61 | return "get" + toFirstUpperCase(columnName) + "()"; 62 | } 63 | 64 | private String toSetMethod(String columnName, String typeStr) { 65 | return "set" + toFirstUpperCase(columnName) + "(" + typeStr 66 | + columnName + ")"; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/genClass/sign/CSharpSign.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.genClass.sign; 2 | 3 | import org.excel2db.write.util.TypeEnum; 4 | 5 | public class CSharpSign { 6 | 7 | public static String Private = "private "; 8 | public static String Public = "public "; 9 | public static String Void = "void "; 10 | public static String Return = "return "; 11 | public static String This = "this"; 12 | public static String Class = "class "; 13 | public static String NameSpace = "namespace "; 14 | public static String Left_Brace = " {"; 15 | public static String Right_Brace = "}"; 16 | 17 | public static final String[] using = new String[] { "using UnityEngine;", 18 | "using System.Collections;" }; 19 | 20 | public static final String[] Tab = new String[] { "", "\t", "\t\t", 21 | "\t\t\t", "\t\t\t\t", "\t\t\t\t\t", "\t\t\t\t\t\t" }; 22 | 23 | public static String fullType(TypeEnum type) { 24 | switch (type) { 25 | case INT: 26 | return "int "; 27 | case FLOAT: 28 | return "float "; 29 | case LONG: 30 | return "long "; 31 | case STRING: 32 | return "string "; 33 | case DOUBLE: 34 | return "double "; 35 | case INTS: 36 | return "int[] "; 37 | case FLOATS: 38 | return "float[] "; 39 | case LONGS: 40 | return "long[] "; 41 | case STRINGS: 42 | return "string[] "; 43 | case DOUBLES: 44 | return "double[] "; 45 | default: 46 | throw new RuntimeException("error type:" + type 47 | + " support:int,float,long,string,double,ints,floats,longs,strings,doubles"); 48 | } 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/genClass/sign/JavaSign.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.genClass.sign; 2 | 3 | import org.excel2db.write.util.TypeEnum; 4 | 5 | public class JavaSign { 6 | 7 | public static String Package = "package "; 8 | public static String Private = "private "; 9 | public static String Public = "public "; 10 | public static String Void = "void "; 11 | public static String Return = "return "; 12 | public static String This = "this"; 13 | public static String Class = "class "; 14 | public static String Left_Brace = " {"; 15 | public static String Right_Brace = "}"; 16 | 17 | public static final String[] Tab = new String[] { "", "\t", "\t\t", 18 | "\t\t\t", "\t\t\t\t", "\t\t\t\t\t", "\t\t\t\t\t\t" }; 19 | 20 | public static String fullType(TypeEnum type) { 21 | switch (type) { 22 | case INT: 23 | return "int "; 24 | case FLOAT: 25 | return "float "; 26 | case LONG: 27 | return "long "; 28 | case STRING: 29 | return "String "; 30 | case DOUBLE: 31 | return "double "; 32 | case INTS: 33 | return "int[] "; 34 | case FLOATS: 35 | return "float[] "; 36 | case LONGS: 37 | return "long[] "; 38 | case STRINGS: 39 | return "String[] "; 40 | case DOUBLES: 41 | return "double[] "; 42 | default: 43 | throw new RuntimeException( 44 | "error type:" 45 | + type 46 | + " support:int,float,long,string,double,ints,floats,longs,strings,doubles"); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/manager/ExcelParse.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.manager; 2 | 3 | import java.io.FileInputStream; 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | import org.apache.log4j.Logger; 10 | import org.apache.poi.ss.usermodel.Cell; 11 | import org.apache.poi.ss.usermodel.Row; 12 | import org.apache.poi.ss.usermodel.Sheet; 13 | import org.apache.poi.ss.usermodel.Workbook; 14 | import org.apache.poi.ss.usermodel.WorkbookFactory; 15 | import org.excel2db.write.util.ConfigUtil; 16 | import org.excel2db.write.util.Constants; 17 | import org.excel2db.write.util.ExcelColumn; 18 | import org.excel2db.write.util.TypeEnum; 19 | 20 | /** 21 | * 读取excel 22 | * 23 | * @author zhaohui 24 | * 25 | */ 26 | public class ExcelParse { 27 | 28 | private final static Logger logger = Logger.getLogger(ExcelParse.class); 29 | 30 | /** 数据开始行 **/ 31 | private static int DATA_STAR_ROW = 5; 32 | /** 类型的行 **/ 33 | private static int TYPE_ROW = 2; 34 | /** 名字的行 **/ 35 | private static int NAME_ROW = 0; 36 | 37 | /** excel文件路径 **/ 38 | private String filePath; 39 | 40 | /** 字段名称,字段类型,数据 **/ 41 | private Map> columnNameMap = new HashMap>(); 42 | private Map> columnTypeMap = new HashMap>(); 43 | private Map>> dataMap = new HashMap>>(); 44 | 45 | public ExcelParse(String filePath) { 46 | this.filePath = filePath; 47 | } 48 | 49 | /** 50 | * 读取excel 51 | */ 52 | public void readExcel() { 53 | logger.debug("start read excel"); 54 | Workbook book = null; 55 | try { 56 | book = WorkbookFactory.create(new FileInputStream(filePath)); 57 | 58 | List sheetStartWithList = ConfigUtil.getSheetStartWith(); 59 | int sheetNum = book.getNumberOfSheets(); 60 | for (int num = 0; num < sheetNum; num++) { 61 | Sheet sheet = book.getSheetAt(num); 62 | String sheetName = sheet.getSheetName(); 63 | 64 | if (!isStartWith(sheetStartWithList, sheetName)) { 65 | continue; 66 | } 67 | 68 | int rows = getRowNum(book, num); 69 | int columns = getColumnNum(book, num); 70 | 71 | List columnList = new ArrayList(); 72 | List typeList = new ArrayList(); 73 | List nameList = new ArrayList(); 74 | 75 | for (int column = 0; column < columns; column++) { 76 | String type = getValue(sheet, column, TYPE_ROW); 77 | TypeEnum typeEnum = TypeEnum.type(type); 78 | // 不存在的类型直接抛弃,主要是方便添加一些注释的列 79 | if (typeEnum != null) { 80 | typeList.add(typeEnum); 81 | String name = getValue(sheet, column, NAME_ROW); 82 | if (name.equals("")) { 83 | throw new RuntimeException( 84 | "NameNullException:sheet=" + sheetName 85 | + ",column=" + toAZ(column + 1)); 86 | } 87 | nameList.add(name); 88 | columnList.add(column); 89 | } 90 | } 91 | 92 | if (nameList.size() > 0 && typeList.size() > 0) { 93 | columnNameMap.put(sheetName, nameList); 94 | columnTypeMap.put(sheetName, typeList); 95 | 96 | List> dataList = new ArrayList>(); 97 | next: for (int row = DATA_STAR_ROW; row <= rows; row++) { 98 | List list = new ArrayList(); 99 | for (int index = 0; index < columnList.size(); index++) { 100 | int column = columnList.get(index); 101 | TypeEnum typeEnum = typeList.get(index); 102 | 103 | String content = getValue(sheet, column, row); 104 | if (index == 0 && content.equals("")) { 105 | break next; 106 | } 107 | String value = getInitValue(content); 108 | 109 | checkValue(sheetName, typeEnum, value, row, column); 110 | list.add(value); 111 | } 112 | dataList.add(list); 113 | } 114 | 115 | dataMap.put(sheetName, dataList); 116 | } else { 117 | logger.info("please check sheet[" + sheetName 118 | + "],name or type is invalid."); 119 | } 120 | } 121 | logger.debug("end read excel"); 122 | } catch (Exception e) { 123 | logger.error("readExcel error", e); 124 | } 125 | } 126 | 127 | private boolean isStartWith(List sheetStartWithList, 128 | String sheetName) { 129 | for (String sw : sheetStartWithList) { 130 | if (sheetName.startsWith(sw)) { 131 | return true; 132 | } 133 | } 134 | return false; 135 | } 136 | 137 | /** 138 | * 返回Excel最大行index值,实际行数要加1 139 | * 140 | * @return 141 | */ 142 | public int getRowNum(Workbook book, int sheetIndex) { 143 | Sheet sheet = book.getSheetAt(sheetIndex); 144 | return sheet.getLastRowNum(); 145 | } 146 | 147 | /** 148 | * 返回数据的列数 149 | * 150 | * @return 151 | */ 152 | public int getColumnNum(Workbook book, int sheetIndex) { 153 | Sheet sheet = book.getSheetAt(sheetIndex); 154 | Row row = sheet.getRow(0); 155 | if (row != null && row.getLastCellNum() > 0) { 156 | return row.getLastCellNum(); 157 | } 158 | return 0; 159 | } 160 | 161 | /** 162 | * 获取单元格中的数值 163 | * 164 | * @param sheet 165 | * @param column 166 | * @param row 167 | * @return 168 | */ 169 | private String getValue(Sheet sheet, int column, int row) { 170 | Row rowObj = sheet.getRow(row); 171 | if (rowObj == null) { 172 | return ""; 173 | } 174 | Cell cell = rowObj.getCell(column); 175 | if (cell == null) { 176 | return ""; 177 | } 178 | cell.setCellType(Cell.CELL_TYPE_STRING); 179 | return cell.getStringCellValue().trim(); 180 | } 181 | 182 | /** 183 | * 对excle中单元格数组就行检查 184 | * 185 | * @param typeEnum 186 | * @param value 187 | * @param row 188 | * @param column 189 | */ 190 | private void checkValue(String sheetName, TypeEnum typeEnum, String value, 191 | int row, int column) { 192 | try { 193 | if (typeEnum == TypeEnum.INT) { 194 | Integer.valueOf(value); 195 | } else if (typeEnum == TypeEnum.FLOAT) { 196 | Float.valueOf(value); 197 | } else if (typeEnum == TypeEnum.LONG) { 198 | Long.valueOf(value); 199 | } else if (typeEnum == TypeEnum.DOUBLE) { 200 | Double.valueOf(value); 201 | } else if (typeEnum == TypeEnum.INTS) { 202 | String values[] = value.split(Constants.SEPARATOR); 203 | for (String v : values) { 204 | Integer.valueOf(v); 205 | } 206 | } else if (typeEnum == TypeEnum.FLOATS) { 207 | String values[] = value.split(Constants.SEPARATOR); 208 | for (String v : values) { 209 | Float.valueOf(v); 210 | } 211 | } else if (typeEnum == TypeEnum.LONGS) { 212 | String values[] = value.split(Constants.SEPARATOR); 213 | for (String v : values) { 214 | Long.valueOf(v); 215 | } 216 | } else if (typeEnum == TypeEnum.DOUBLES) { 217 | String values[] = value.split(Constants.SEPARATOR); 218 | for (String v : values) { 219 | Double.valueOf(v); 220 | } 221 | } 222 | } catch (Exception e) { 223 | if (typeEnum == TypeEnum.INT || typeEnum == TypeEnum.FLOAT 224 | || typeEnum == TypeEnum.DOUBLE || typeEnum == TypeEnum.LONG) { 225 | throw new RuntimeException("numberFormatException:sheet=" 226 | + sheetName + ",row=" + (row + 1) + ",column=" 227 | + toAZ(column + 1)); 228 | } else { 229 | throw new RuntimeException("numberFormatException:sheet=" 230 | + sheetName + ",row=" + (row + 1) + ",column=" 231 | + toAZ(column + 1) + ",separator is " 232 | + Constants.SEPARATOR); 233 | } 234 | } 235 | } 236 | 237 | private String toAZ(int column) { 238 | return ExcelColumn.excelColIndexToStr(column); 239 | } 240 | 241 | /** 242 | * 获取非string类型的初始值 243 | * 244 | * @param value 245 | * @return 246 | */ 247 | private String getInitValue(String value) { 248 | if (value.equals("")) { 249 | return "0"; 250 | } 251 | return value; 252 | } 253 | 254 | public Map> getColumnNameMap() { 255 | return columnNameMap; 256 | } 257 | 258 | public Map> getColumnTypeMap() { 259 | return columnTypeMap; 260 | } 261 | 262 | public Map>> getDataMap() { 263 | return dataMap; 264 | } 265 | 266 | } 267 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/manager/GeneratorBeanClass.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.manager; 2 | 3 | import java.util.List; 4 | import java.util.Map; 5 | import java.util.Set; 6 | 7 | import org.apache.log4j.Logger; 8 | import org.excel2db.write.genClass.Config; 9 | import org.excel2db.write.genClass.GeneratorFactory; 10 | import org.excel2db.write.genClass.IGenerator; 11 | import org.excel2db.write.genClass.SheetInfo; 12 | import org.excel2db.write.util.TypeEnum; 13 | 14 | /** 15 | * 生成bean类 16 | * 17 | * @author zhaohui 18 | * 19 | */ 20 | public class GeneratorBeanClass { 21 | 22 | private final static Logger logger = Logger.getLogger(NDBGenerator.class); 23 | 24 | private static final String NAME_ENDFIX = "Data"; 25 | 26 | private Config config; 27 | private Map> columnNameMap; 28 | private Map> columnTypeMap; 29 | 30 | public GeneratorBeanClass(ExcelParse excelManager, Config config) { 31 | columnNameMap = excelManager.getColumnNameMap(); 32 | columnTypeMap = excelManager.getColumnTypeMap(); 33 | this.config = config; 34 | } 35 | 36 | public void generator() { 37 | logger.debug("start generator bean file"); 38 | try { 39 | Set keys = columnNameMap.keySet(); 40 | for (String key : keys) { 41 | List columnNames = columnNameMap.get(key); 42 | List typeEnums = columnTypeMap.get(key); 43 | if (columnNames.size() <= 0 || typeEnums.size() <= 0) { 44 | break; 45 | } 46 | 47 | SheetInfo info = new SheetInfo(key + NAME_ENDFIX, columnNames, 48 | typeEnums); 49 | IGenerator generator = GeneratorFactory.getGenerator(config 50 | .getLanguage()); 51 | generator.generator(info, config); 52 | } 53 | logger.debug("end generator bean file"); 54 | } catch (Exception e) { 55 | logger.error("generator bean error", e); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/manager/NDBGenerator.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.manager; 2 | 3 | import java.io.File; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.io.UnsupportedEncodingException; 7 | import java.nio.ByteBuffer; 8 | import java.nio.channels.FileChannel; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | import java.util.Map; 12 | import java.util.Set; 13 | 14 | import org.apache.log4j.Logger; 15 | import org.excel2db.write.util.ConfigUtil; 16 | import org.excel2db.write.util.TypeEnum; 17 | 18 | /** 19 | * 生成ndb文件管理 20 | * 21 | * @author ksfzhaohui 22 | * 23 | */ 24 | public class NDBGenerator { 25 | 26 | private final static Logger logger = Logger.getLogger(NDBGenerator.class); 27 | 28 | /** 字符串编码格式 **/ 29 | private static final String STRING_ENCODING = "UTF-8"; 30 | 31 | /** header的长度 **/ 32 | private static final int HEADER_LENGTH = 5 * 4; 33 | 34 | private Map> columnNameMap; 35 | private Map> columnTypeMap; 36 | private Map>> dataMap; 37 | 38 | public NDBGenerator(ExcelParse excelManager) { 39 | columnNameMap = excelManager.getColumnNameMap(); 40 | columnTypeMap = excelManager.getColumnTypeMap(); 41 | dataMap = excelManager.getDataMap(); 42 | } 43 | 44 | public void writeDB(String path) { 45 | logger.debug("start write db file"); 46 | FileChannel fc = null; 47 | try { 48 | Set keys = columnNameMap.keySet(); 49 | for (String key : keys) { 50 | List columnNames = columnNameMap.get(key); 51 | List typeEnums = columnTypeMap.get(key); 52 | List> datas = dataMap.get(key); 53 | if (columnNames.size() <= 0 || typeEnums.size() <= 0 54 | || datas.size() <= 0) { 55 | break; 56 | } 57 | 58 | ByteBuffer headerBuffer = getHeaderBuffer(datas.size(), 59 | columnNames.size()); 60 | ByteBuffer columnNameBuffer = getColumnNameBuffer(columnNames); 61 | ByteBuffer columnTypeBuffer = getColumnTypeBuffer(typeEnums); 62 | ByteBuffer dataBuffer = getDataBuffer(datas, typeEnums); 63 | 64 | headerBuffer.putInt(columnNameBuffer.limit()); 65 | headerBuffer.putInt(columnTypeBuffer.limit()); 66 | headerBuffer.putInt(dataBuffer.limit()); 67 | 68 | File ndbPath = new File(path); 69 | if (!ndbPath.exists()) { 70 | ndbPath.mkdirs(); 71 | } 72 | 73 | fc = new FileOutputStream(path + key 74 | + ConfigUtil.getFileSuffix()).getChannel(); 75 | 76 | headerBuffer.flip(); 77 | columnNameBuffer.flip(); 78 | columnTypeBuffer.flip(); 79 | dataBuffer.flip(); 80 | 81 | fc.write(headerBuffer); 82 | fc.write(columnNameBuffer); 83 | fc.write(columnTypeBuffer); 84 | fc.write(dataBuffer); 85 | } 86 | logger.debug("end write db file"); 87 | } catch (Exception e) { 88 | logger.error("writeDB error", e); 89 | } finally { 90 | if (fc != null) { 91 | try { 92 | fc.close(); 93 | } catch (IOException e) { 94 | } 95 | } 96 | } 97 | } 98 | 99 | /** 100 | * 头文件buff 101 | * 102 | * @param dataSize 103 | * @param columnSize 104 | * @return 105 | */ 106 | private ByteBuffer getHeaderBuffer(int dataSize, int columnSize) { 107 | ByteBuffer headerBuffer = ByteBuffer.allocate(HEADER_LENGTH); 108 | headerBuffer.putInt(dataSize); 109 | headerBuffer.putInt(columnSize); 110 | return headerBuffer; 111 | } 112 | 113 | /** 114 | * 字段名称buff 115 | * 116 | * @param columnNames 117 | * @return 118 | * @throws UnsupportedEncodingException 119 | */ 120 | private ByteBuffer getColumnNameBuffer(List columnNames) 121 | throws UnsupportedEncodingException { 122 | List buffList = new ArrayList(); 123 | int length = 0; 124 | for (String columnName : columnNames) { 125 | ByteBuffer buffer = stringBuff(columnName); 126 | buffList.add(buffer); 127 | length += buffer.limit(); 128 | } 129 | ByteBuffer columnBuff = ByteBuffer.allocate(length); 130 | for (ByteBuffer buffer : buffList) { 131 | columnBuff.put(buffer); 132 | } 133 | return columnBuff; 134 | } 135 | 136 | /** 137 | * 获取字段类型buff 138 | * 139 | * @param typeEnums 140 | * @return 141 | */ 142 | private ByteBuffer getColumnTypeBuffer(List typeEnums) { 143 | ByteBuffer buffer = ByteBuffer.allocate(typeEnums.size()); 144 | for (TypeEnum type : typeEnums) { 145 | buffer.put(type.value()); 146 | } 147 | return buffer; 148 | } 149 | 150 | /** 151 | * 获取数据buff 152 | * 153 | * @param datas 154 | * @param typeEnums 155 | * @return 156 | * @throws UnsupportedEncodingException 157 | */ 158 | private ByteBuffer getDataBuffer(List> datas, 159 | List typeEnums) throws UnsupportedEncodingException { 160 | List bufferList = new ArrayList(); 161 | for (List data : datas) { 162 | int length = getDataBuffSize(typeEnums, data); 163 | 164 | ByteBuffer buffer = getDataBuff(length, typeEnums, data); 165 | bufferList.add(buffer); 166 | } 167 | int allLength = 0; 168 | for (ByteBuffer buffer : bufferList) { 169 | allLength += buffer.limit(); 170 | } 171 | ByteBuffer allBuffer = ByteBuffer.allocate(allLength); 172 | for (ByteBuffer buffer : bufferList) { 173 | allBuffer.put(buffer); 174 | } 175 | return allBuffer; 176 | } 177 | 178 | /** 179 | * 获取string的缓存区 180 | * 181 | * @param str 182 | * @return 183 | * @throws UnsupportedEncodingException 184 | */ 185 | private ByteBuffer stringBuff(String str) 186 | throws UnsupportedEncodingException { 187 | ByteBuffer buffer = ByteBuffer.allocate(getStringBuffSize(str)); 188 | byte bytes[] = str.getBytes(STRING_ENCODING); 189 | buffer.putInt(bytes.length); 190 | buffer.put(bytes); 191 | buffer.flip(); 192 | return buffer; 193 | } 194 | 195 | /** 196 | * 获取每一行的数据缓存大小 197 | * 198 | * @param typeEnums 199 | * @param data 200 | * @return 201 | * @throws UnsupportedEncodingException 202 | */ 203 | private int getDataBuffSize(List typeEnums, List data) 204 | throws UnsupportedEncodingException { 205 | int length = 0; 206 | for (int i = 0; i < data.size(); i++) { 207 | TypeEnum type = typeEnums.get(i); 208 | if (type == TypeEnum.STRING || type == TypeEnum.STRINGS 209 | || type == TypeEnum.INTS || type == TypeEnum.LONGS 210 | || type == TypeEnum.FLOATS || type == TypeEnum.DOUBLES) { 211 | length += getStringBuffSize(data.get(i)); 212 | } else { 213 | length += TypeEnum.size(type); 214 | } 215 | } 216 | return length; 217 | } 218 | 219 | private ByteBuffer getDataBuff(int length, List typeEnums, 220 | List data) throws UnsupportedEncodingException { 221 | ByteBuffer buffer = ByteBuffer.allocate(length); 222 | for (int i = 0; i < data.size(); i++) { 223 | TypeEnum type = typeEnums.get(i); 224 | if (type == TypeEnum.INT) { 225 | buffer.putInt(Integer.valueOf(getInitValue(data.get(i)))); 226 | } else if (type == TypeEnum.FLOAT) { 227 | buffer.putFloat(Float.valueOf(getInitValue(data.get(i)))); 228 | } else if (type == TypeEnum.LONG) { 229 | buffer.putLong(Long.valueOf(getInitValue(data.get(i)))); 230 | } else if (type == TypeEnum.STRING) { 231 | buffer.put(stringBuff(data.get(i))); 232 | } else if (type == TypeEnum.DOUBLE) { 233 | buffer.putDouble(Double.valueOf(getInitValue(data.get(i)))); 234 | } else if (type == TypeEnum.INTS || type == TypeEnum.FLOATS 235 | || type == TypeEnum.LONGS || type == TypeEnum.DOUBLES 236 | || type == TypeEnum.STRINGS) { 237 | buffer.put(stringBuff(data.get(i))); 238 | } else { 239 | throw new RuntimeException( 240 | "error type:" 241 | + type 242 | + "support:int,float,long,string,double,ints,floats,longs,strings,doubles"); 243 | } 244 | } 245 | 246 | buffer.flip(); 247 | return buffer; 248 | } 249 | 250 | /** 251 | * 获取字符串写入缓存区的大小 byte[].Length + Int.size 252 | * 253 | * @param str 254 | * @return 255 | * @throws UnsupportedEncodingException 256 | */ 257 | private int getStringBuffSize(String str) 258 | throws UnsupportedEncodingException { 259 | byte bytes[] = str.getBytes(STRING_ENCODING); 260 | return bytes.length + TypeEnum.size(TypeEnum.INT); 261 | } 262 | 263 | /** 264 | * 获取非string类型的初始值 265 | * 266 | * @param value 267 | * @return 268 | */ 269 | private String getInitValue(String value) { 270 | if (value.equals("")) { 271 | return "0"; 272 | } 273 | return value; 274 | } 275 | } 276 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/util/ConfigUtil.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.util; 2 | 3 | import java.io.FileInputStream; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | import java.util.Properties; 7 | 8 | import org.apache.log4j.Logger; 9 | 10 | public class ConfigUtil { 11 | 12 | private final static Logger logger = Logger.getLogger(ConfigUtil.class); 13 | 14 | private static Properties prop = new Properties(); 15 | 16 | private static List startWithList = new ArrayList(); 17 | 18 | static { 19 | try { 20 | prop.load(new FileInputStream("config.properties")); 21 | String startWiths[] = prop.getProperty("sheetStartWith").split(","); 22 | if (startWiths != null) { 23 | for (String sw : startWiths) { 24 | startWithList.add(sw); 25 | } 26 | } 27 | } catch (Exception e) { 28 | logger.error("", e); 29 | } 30 | } 31 | 32 | public static List getSheetStartWith() { 33 | return startWithList; 34 | } 35 | 36 | public static String getFileSuffix() { 37 | return prop.getProperty("file_suffix"); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/util/Constants.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.util; 2 | 3 | public class Constants { 4 | 5 | /** excel配置数组的分隔符 **/ 6 | public static final String SEPARATOR = "#"; 7 | } 8 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/util/EndfixEnum.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.util; 2 | 3 | public enum EndfixEnum { 4 | 5 | JAVA("java", ".java"), // 6 | CSHARP("csharp", ".cs"); 7 | 8 | private String language; 9 | private String endfix; 10 | 11 | EndfixEnum(String language, String endfix) { 12 | this.language = language; 13 | this.endfix = endfix; 14 | } 15 | 16 | public static String endfix(String language) { 17 | if (language.equals(JAVA.language)) { 18 | return JAVA.endfix; 19 | } else if (language.equals(CSHARP.language)) { 20 | return CSHARP.endfix; 21 | } else { 22 | throw new RuntimeException("error language:" + language 23 | + " support:java,csharp"); 24 | } 25 | } 26 | 27 | public static EndfixEnum getType(String language) { 28 | if (language.equals(JAVA.language)) { 29 | return JAVA; 30 | } else if (language.equals(CSHARP.language)) { 31 | return CSHARP; 32 | } else { 33 | throw new RuntimeException("error language:" + language 34 | + " support:java,csharp"); 35 | } 36 | } 37 | 38 | public String getEndfix() { 39 | return endfix; 40 | } 41 | 42 | public String getLanguage() { 43 | return language; 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/util/ExcelColumn.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.util; 2 | 3 | public class ExcelColumn { 4 | 5 | public static void main(String[] args) { 6 | String colstr = "AA"; 7 | int colIndex = excelColStrToNum(colstr, colstr.length()); 8 | System.out.println("'" + colstr + "' column index of " + colIndex); 9 | 10 | colIndex = 26; 11 | colstr = excelColIndexToStr(colIndex); 12 | System.out.println(colIndex + " column in excel of " + colstr); 13 | 14 | colstr = "AAAA"; 15 | colIndex = excelColStrToNum(colstr, colstr.length()); 16 | System.out.println("'" + colstr + "' column index of " + colIndex); 17 | 18 | colIndex = 466948; 19 | colstr = excelColIndexToStr(colIndex); 20 | System.out.println(colIndex + " column in excel of " + colstr); 21 | } 22 | 23 | /** 24 | * Excel column index begin 1 25 | * 26 | * @param colStr 27 | * @param length 28 | * @return 29 | */ 30 | public static int excelColStrToNum(String colStr, int length) { 31 | int num = 0; 32 | int result = 0; 33 | for (int i = 0; i < length; i++) { 34 | char ch = colStr.charAt(length - i - 1); 35 | num = (int) (ch - 'A' + 1); 36 | num *= Math.pow(26, i); 37 | result += num; 38 | } 39 | return result; 40 | } 41 | 42 | /** 43 | * Excel column index begin 1 44 | * 45 | * @param columnIndex 46 | * @return 47 | */ 48 | public static String excelColIndexToStr(int columnIndex) { 49 | if (columnIndex <= 0) { 50 | return null; 51 | } 52 | String columnStr = ""; 53 | columnIndex--; 54 | do { 55 | if (columnStr.length() > 0) { 56 | columnIndex--; 57 | } 58 | columnStr = ((char) (columnIndex % 26 + (int) 'A')) + columnStr; 59 | columnIndex = (int) ((columnIndex - columnIndex % 26) / 26); 60 | } while (columnIndex > 0); 61 | return columnStr; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/util/FileUtil.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.util; 2 | 3 | import java.io.File; 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | public class FileUtil { 8 | 9 | /** excel文件后缀 **/ 10 | public static final String XLS = ".xls"; 11 | public static final String XLSX = ".xlsx"; 12 | 13 | /** 14 | * 获取指定路径下所有的excel文件 15 | * 16 | * @param path 17 | * @return 18 | */ 19 | public static List getFileList(String path) { 20 | List fileList = new ArrayList(); 21 | if (path == null || path.equals("")) { 22 | return fileList; 23 | } 24 | File f = new File(path); 25 | if (f.isDirectory()) { 26 | File[] fs = f.listFiles(); 27 | if (fs != null) { 28 | for (int i = 0; i < fs.length; i++) { 29 | fileList.addAll(getFileList(fs[i].getPath())); 30 | } 31 | } 32 | } else if (f.getName().endsWith(XLS) || f.getName().endsWith(XLSX)) { 33 | fileList.add(f.getName()); 34 | } 35 | return fileList; 36 | } 37 | 38 | /** 39 | * 删除指定目录的所有文件 40 | * 41 | * @param path 42 | * @param suffix 43 | * 指定后缀 44 | */ 45 | public static void deleteAllFilesOfDir(File path, String suffix) { 46 | if (!path.exists()) 47 | return; 48 | if (path.isFile()) { 49 | path.delete(); 50 | return; 51 | } 52 | File[] files = path.listFiles(); 53 | for (int i = 0; i < files.length; i++) { 54 | if (files[i].getName().endsWith(suffix)) { 55 | deleteAllFilesOfDir(files[i], suffix); 56 | } 57 | } 58 | path.delete(); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /excel2db-write/src/main/java/org/excel2db/write/util/TypeEnum.java: -------------------------------------------------------------------------------- 1 | package org.excel2db.write.util; 2 | 3 | public enum TypeEnum { 4 | 5 | INT('i'), FLOAT('f'), LONG('l'), STRING('s'), DOUBLE('d'), INTS('x'), FLOATS( 6 | 'y'), LONGS('z'), STRINGS('j'), DOUBLES('k'); 7 | 8 | private char value; 9 | 10 | private TypeEnum(char value) { 11 | this.value = value; 12 | } 13 | 14 | public byte value() { 15 | return (byte) value; 16 | } 17 | 18 | public static int size(TypeEnum type) { 19 | if (type == INT || type == FLOAT) { 20 | return 4; 21 | } else if (type == LONG || type == DOUBLE) { 22 | return 8; 23 | } else { 24 | throw new RuntimeException("error type:" + type 25 | + " support:int,float,long,double"); 26 | } 27 | } 28 | 29 | public static TypeEnum type(String type) { 30 | if (type.equalsIgnoreCase("int")) { 31 | return INT; 32 | } else if (type.equalsIgnoreCase("float")) { 33 | return FLOAT; 34 | } else if (type.equalsIgnoreCase("long")) { 35 | return LONG; 36 | } else if (type.equalsIgnoreCase("string")) { 37 | return STRING; 38 | } else if (type.equalsIgnoreCase("double")) { 39 | return DOUBLE; 40 | } else if (type.equalsIgnoreCase("ints")) { 41 | return INTS; 42 | } else if (type.equalsIgnoreCase("floats")) { 43 | return FLOATS; 44 | } else if (type.equalsIgnoreCase("longs")) { 45 | return LONGS; 46 | } else if (type.equalsIgnoreCase("strings")) { 47 | return STRINGS; 48 | } else if (type.equalsIgnoreCase("doubles")) { 49 | return DOUBLES; 50 | } else { 51 | return null; 52 | } 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /excel2db-write/src/main/resources/log4j.properties: -------------------------------------------------------------------------------- 1 | log4j.rootLogger=INFO,fileA,consoleA 2 | 3 | log4j.appender.fileA=org.apache.log4j.DailyRollingFileAppender 4 | log4j.appender.fileA.File=./logs/excel2db-write.log 5 | log4j.appender.fileA.append=true 6 | log4j.appender.fileA.ImmediateFlush=true 7 | log4j.appender.fileA.layout=org.apache.log4j.PatternLayout 8 | log4j.appender.fileA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %L::: %-5p %C - %m%n 9 | 10 | log4j.appender.consoleA=org.apache.log4j.ConsoleAppender 11 | log4j.appender.consoleA.layout=org.apache.log4j.PatternLayout 12 | log4j.appender.consoleA.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} - %m%n -------------------------------------------------------------------------------- /excel2db-write/src/main/resources/package.xml: -------------------------------------------------------------------------------- 1 | 2 | bin 3 | 4 | 5 | zip 6 | 7 | 8 | 9 | 10 | 11 | 12 | false 13 | lib 14 | false 15 | 16 | 17 | 18 | 19 | 20 | 21 | scripts 22 | 23 | 24 | *.bat 25 | 26 | 27 | 28 | 29 | 30 | 31 | *.properties 32 | 33 | 34 | 35 | 36 | 37 | ${project.build.directory} 38 | 39 | 40 | *.jar 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.excel2db 7 | excel2db 8 | 0.0.1-SNAPSHOT 9 | pom 10 | 11 | excel2db 12 | http://maven.apache.org 13 | 14 | 15 | UTF-8 16 | 17 | 18 | 19 | 20 | 21 | org.apache.maven.plugins 22 | maven-compiler-plugin 23 | 2.0.2 24 | 25 | 1.6 26 | 1.6 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | log4j 35 | log4j 36 | 1.2.12 37 | 38 | 39 | 40 | 41 | excel2db-write 42 | excel2db-read 43 | excel2db-browse 44 | 45 | -------------------------------------------------------------------------------- /src/main/java/com/excel2db/App.java: -------------------------------------------------------------------------------- 1 | package com.excel2db; 2 | 3 | /** 4 | * Hello world! 5 | * 6 | */ 7 | public class App 8 | { 9 | public static void main( String[] args ) 10 | { 11 | System.out.println( "Hello World!" ); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/test/java/com/excel2db/AppTest.java: -------------------------------------------------------------------------------- 1 | package com.excel2db; 2 | 3 | import junit.framework.Test; 4 | import junit.framework.TestCase; 5 | import junit.framework.TestSuite; 6 | 7 | /** 8 | * Unit test for simple App. 9 | */ 10 | public class AppTest 11 | extends TestCase 12 | { 13 | /** 14 | * Create the test case 15 | * 16 | * @param testName name of the test case 17 | */ 18 | public AppTest( String testName ) 19 | { 20 | super( testName ); 21 | } 22 | 23 | /** 24 | * @return the suite of tests being tested 25 | */ 26 | public static Test suite() 27 | { 28 | return new TestSuite( AppTest.class ); 29 | } 30 | 31 | /** 32 | * Rigourous Test :-) 33 | */ 34 | public void testApp() 35 | { 36 | assertTrue( true ); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /test.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ksfzhaohui/excel2db/781edd6d7ff172501f67355bcb491b2f6e01806a/test.xls -------------------------------------------------------------------------------- /二进制格式文档: -------------------------------------------------------------------------------- 1 | header 2 | { 3 | int records 记录数量; 4 | int fields 字段数量; 5 | int namebuflength 名称buff的长度; 6 | int typebuflength 类型buff的长度; 7 | int databuflength 数据buff的长度; 8 | } 9 | 10 | columnName 11 | { 12 | for(){ 13 | int len; 14 | byte[] name; 15 | } 16 | } 17 | 18 | columnType 19 | { 20 | for(){ 21 | byte type; 22 | } 23 | } 24 | 25 | data 26 | { 27 | for(){ 28 | if(type==int || type==float || type==long || type==double){ 29 | byte[] value; 30 | }else if(type==string){ 31 | int len; 32 | byte[] value; 33 | } 34 | } 35 | } --------------------------------------------------------------------------------