zoukankan      html  css  js  c++  java
  • 将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(" 	/**
    ");
    				sb.append("	*"+mark.get(i)+"
    ");
    				sb.append("	*/
    ");
    				sb.append("	private "+types.get(i)+" "+attributes.get(i)+";
    
    ");
    			}
    		}
    		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(" 	/**
    ");
    				sb.append("	* 设置"+mark.get(i)+"
    ");
    				sb.append("	* @param "+attributes.get(i)+" "+mark.get(i)+"
    ");
    				sb.append("	*/
    ");
    				sb.append("	public void set"+attribute+"("+types.get(i)+" "+attributes.get(i)+"){
    ");
    				sb.append("		this."+attributes.get(i)+" = "+attributes.get(i)+";
    ");
    				sb.append("	}
    
    ");
    				sb.append(" 	/**
    ");
    				sb.append("	*获得"+mark.get(i)+"
    ");
    				sb.append("	* @return "+attributes.get(i)+" "+mark.get(i)+"
    ");
    				sb.append("	*/
    ");
    				sb.append("	public "+types.get(i)+" get"+attribute+"(){
    ");
    				sb.append("		return "+attributes.get(i)+";
    ");
    				sb.append("	}
    
    ");
    			}
    		}
    		
    		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+"{
    
    ");
            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");
        }
    
    }
  • 相关阅读:
    第一章 Java Collections Framework总览
    MySQL常用sql语句
    static关键字
    Mysql常用索引及优化
    JDK安装及环境变量配置
    破解IntelliJ IDEA 2017
    Spring自我总结
    Java中根据字节截取字符串
    外键的主要作用:保持数据的一致性、完整性
    使用SSI框架写的简单Demo(查询模块)
  • 原文地址:https://www.cnblogs.com/james-roger/p/5391809.html
Copyright © 2011-2022 走看看