将Excel生成实体类
package com.excel.test;
import java.util.List;
public class createUtil {
public static String appendPrivate(List<String> types,List<String> attributes,List<String> mark){
StringBuffer sb = new StringBuffer();
if(types.size()==attributes.size()&&types.size()==mark.size()){
for(int i=0;i<types.size();i++){
sb.append(" /**\n");
sb.append(" *"+mark.get(i)+"\n");
sb.append(" */\n");
sb.append(" private "+types.get(i)+" "+attributes.get(i)+";\n\n");
}
}
return sb.toString();
}
public static String getSet(List<String> types,List<String> attributes,List<String> mark){
StringBuffer sb = new StringBuffer();
if(types.size()==attributes.size()){
for(int i=0;i<types.size();i++){
String attribute = attributes.get(i);
String c = String.valueOf(attribute.charAt(0));
c = c.toUpperCase();
attribute = c + attribute.substring(1);
sb.append(" /**\n");
sb.append(" * 设置"+mark.get(i)+"\n");
sb.append(" * @param "+attributes.get(i)+" "+mark.get(i)+"\n");
sb.append(" */\n");
sb.append(" public void set"+attribute+"("+types.get(i)+" "+attributes.get(i)+"){\n");
sb.append(" this."+attributes.get(i)+" = "+attributes.get(i)+";\n");
sb.append(" }\n\n");
sb.append(" /**\n");
sb.append(" *获得"+mark.get(i)+"\n");
sb.append(" * @return "+attributes.get(i)+" "+mark.get(i)+"\n");
sb.append(" */\n");
sb.append(" public "+types.get(i)+" get"+attribute+"(){\n");
sb.append(" return "+attributes.get(i)+";\n");
sb.append(" }\n\n");
}
}
return sb.toString();
}
}
package com.excel.test; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ReadExcel3 { public static void main(String[] args) throws IOException, InvalidFormatException {
// TODO Auto-generated method stub File file = new File("D:\\user.xlsx");
String fileName = file.getName();
String className = "demo";
File file1 = null;
FileOutputStream fop = null;
List<String> types = new ArrayList<String>();
List<String> attributes = new ArrayList<String>();
List<String> marks = new ArrayList<String>();
if (fileName.endsWith("xlsx")){
XSSFWorkbook workbooks = new XSSFWorkbook(file);
XSSFSheet xssfSheet = workbooks.getSheetAt(0);
int totalRows = xssfSheet.getPhysicalNumberOfRows();
XSSFRow row = xssfSheet.getRow(0);
className = row.getCell(0).getStringCellValue();
System.out.println(className+"total:"+totalRows);
for(int i=2;i<totalRows;i++){
XSSFRow row2 = xssfSheet.getRow(i);
attributes.add(row2.getCell(0).getStringCellValue());
types.add(row2.getCell(1).getStringCellValue());
marks.add(row2.getCell(2).getStringCellValue());
}
}else {
HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file));
//读取默认第一个工作表sheet
HSSFSheet sheet = workbook.getSheetAt(0);
int firstRowNum = 0;
//获取sheet中最后一行行号
int lastRowNum = sheet.getLastRowNum();
HSSFRow row = sheet.getRow(firstRowNum);
className = row.getCell(0).getStringCellValue();
for (int i = 2; i <=lastRowNum; i++){
HSSFRow row1 = sheet.getRow(i);
attributes.add(row1.getCell(0).getStringCellValue());
types.add(row1.getCell(1).getStringCellValue());
marks.add(row1.getCell(2).getStringCellValue());
}
} StringBuffer sb = new StringBuffer(); sb.append("public class "+className+"{\n\n");
sb.append(createUtil.appendPrivate(types, attributes,marks));
sb.append(createUtil.getSet(types, attributes,marks));
sb.append("}"); file1 = new File("d:/"+className+".java");
fop = new FileOutputStream(file1);
if (!file1.exists()) {
file1.createNewFile();
}
byte[] contentInBytes = sb.toString().getBytes(); fop.write(contentInBytes);
fop.flush();
fop.close(); System.out.println("Done");
} }
将Excel生成实体类的更多相关文章
- 懒人小工具:T4生成实体类Model,Insert,Select,Delete以及导出Excel的方法
由于最近公司在用webform开发ERP,用到大量重复机械的代码,之前写了篇文章,懒人小工具:自动生成Model,Insert,Select,Delete以及导出Excel的方法,但是有人觉得这种方法 ...
- NHibernate生成实体类、xml映射文件
最近工作电脑装完win10后,之前使用的codeSmith安装不了,索性自己写一个. 界面比较简单,如下图: 第一行为Oracle数据库的连接字符串.连接成功后,填充表到第4行的下拉列表中. 第二行为 ...
- (转)使用myeclipse生成实体类和hibernate映射文件
转至:http://blog.sina.com.cn/s/blog_9658bdb40100uiod.html 1.下载并安装myeclipse,如果已经安装,则忽略该步骤; 2.打开myeclips ...
- MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
前文: hibernate带给我们的O/RMapping思想是很正确的,即从面相对象的角度来设计工程中的实体对象,建立pojo,然后在编写hbm.xml映射文件来生成数据表.但是在实际开发中,往往我们 ...
- .net 根据匿名类生成实体类,根据datatable生成实体类,根据sql生成实体类
在开发中可能会遇到这几种情况 1.EF或LINQ查询出来的匿名对象在其它地方调用不方便,又懒的手动建实体类 2.通过datatable反射实体需要先建一个类 ,头痛 3.通过SQL语句返回的实体也需要 ...
- T4模板根据DB生成实体类
1.前言 为什么会有这篇文章了,最近看到了一些框架,里面要写的代码太多了,故此就想偷懒,要是能写出一个T4模板,在数据库添加表后,根据模板就可以自动生成了类文件了,这样多好,心动不如行动.记得使用T4 ...
- 使用T4为数据库自动生成实体类
T4 (Text Template Transformation Toolkit) 是一个基于模板的代码生成器.使用T4你可以通过写一些ASP.NET-like模板,来生成C#, T-SQL, XML ...
- T4模板_根据DB生成实体类
为了减少重复劳动,可以通过T4读取数据库表结构,生成实体类,用下面的实例测试了一下 1.首先创建一个项目,并添加文本模板: 2.添加 文本模板: 3.向T4文本模板文件添加代码: <#@ tem ...
- MyEclipse数据库反向生成实体类
MyEclipse数据库反向生成实体类 “计应134(实验班) 凌豪” 当我们在开发项目涉及到的表太多时,一个一个的写JAVA实体类很是费事.然而强大的MyEclipse为我们提供简便的方法:数据库反 ...
随机推荐
- DB2配置信息查看及其更新命令
获取DB2配置信息 db2 get dbm cfg 更新DB2链接配置信息 db2 update dbm cfg using authentication server db2stop db2star ...
- redis2.8--主从机同步流程
- c#读写文本文档-1-用file类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- 转:gartner 2014-07 ETL工具象限排名
ref: http://www.gartner.com/technology/reprints.do?id=1-1YAXV15&ct=140728&st=sb
- ODI中的临时接口
在ODI 11g及后续的版本中,针对复杂的ETL处理,可分解为多个步骤,在中间步骤中使用临时接口,而不用建立相应的物理表,ODI会在处理过程中自动创建和删除这些中间表,从而降低ETL处理复杂度:同时, ...
- sql 之CONCAT用法
这是java交流群里一个网友面试的时候发过来的笔试题,我觉得题目的假定条件应该是某个字母对应的最小数字只有一个. 思路第一步是查出一个子表s1: select name,min(number) fro ...
- 深入理解SELinux
目录(?)[+] 1. 简介 SELinux带给Linux的主要价值是:提供了一个灵活的,可配置的MAC机制. Security-Enhanced Linux (SELinux)由以下两部分组 ...
- explain分析查询
参考以下文章,在此非常感谢原作者 explain分析查询
- encodeURIComponent编码2次
最近在项目中发现前台在往anction中发送数据时进行两次encodeURIComponent,经过上网查找发现以下解释比较合理,mask过来,以供参考,非常感谢原作者!!! 两次encodeURIC ...
- 不能使用weak修饰进行声明的类
These classes include NSTextView, NSFont and NSColorSpace; for the full list, see Transitioning to A ...