⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xls2sql.java

📁 JAVA EXCEL操作API
💻 JAVA
字号:
/*
 * Created on 2004-3-22
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package com.zosatapo.xls;

import java.io.FileInputStream;
import java.sql.Connection;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.zosatapo.xls.core.CoreException;
import com.zosatapo.xls.core.Record;
import com.zosatapo.xls.core.Schema;
import com.zosatapo.xls.io.SQLWriter;
import com.zosatapo.xls.io.XlsReader;
import com.zosatapo.xls.util.ConnUtils;


/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class Xls2SQL
{
  private final static String pathname="conf\\xls2sql.properties";
  
  private String schemaFile;
  private String inputFile;
  
  private boolean debug;
  
  public Xls2SQL()
  {   
  }
  
  public void process(String args[])
  throws CoreException
  {
    if( ! processArgs( args ) ) 
    {
      return;
    }
    
    try
    {
    long timeStart=System.currentTimeMillis();
    
    POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream(inputFile));
    HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
    HSSFSheet        sheet  = hssfworkbook.getSheetAt(0); 
    
    Schema schema=new Schema();
    
    if(schemaFile!=null)
    {
      schema.setPathname(schemaFile);
    }
    else
    {
      schema.setPathname(pathname);
    }
    
    if(debug)
    {
      System.err.println("Schema configFile="+schema.getPathname());
    }
    
    schema.open();

     
    
    Connection conn=ConnUtils.getConnection(schema.getStoreConfig());
    SQLWriter writer=new SQLWriter(schema,conn);
    
    Record record=null;    
    XlsReader reader=new XlsReader(schema,sheet);
    while(reader.hasNext())
    {
      record=reader.next();
      if(debug)
      {
        System.err.println("[Read] "+record);
      }
      writer.write(record);
      
      if(debug)
      {
        System.err.println("[Write] "+record);
      }     
    }
    
    conn.close(); 
    
    reader.close();
    writer.close();
    long timeEnd=System.currentTimeMillis(); 
    
    System.err.println("EXCEL2SQL started:"+(timeEnd-timeStart)); 
    }
    catch(Exception ex)
    {
      throw new CoreException(ex);
    }
  }
  
	public boolean processArgs(String[] args)
	{
		for (int i = 0; i < args.length; i++)
		{
			String arg = args[i];
			if (arg.equals("-schema") || arg.equals("-config") )
			{
        i++;
        if (i < args.length)
        {
          schemaFile = args[i];
        }
        else
        {
          printUsage();
          return (false);
        }
			}
			else if (arg.equals("-input") || arg.equals("-xls"))
			{
				i++;
				if (i < args.length)
				{
					inputFile = args[i];
				}
				else
				{
					printUsage();
					return (false);
				}
			}
      else if (arg.equals("-debug"))
      {
        i++;
        if (i < args.length)
        {
          debug = Boolean.valueOf(args[i]).booleanValue();
        }
        else
        {
          printUsage();
          return (false);
        }
      }
			else
			{
				printUsage();
				return false;
			}
		}
    
    if(inputFile==null)
    {
      printUsage();
      return false;
    }
		
    return true;
	}        
  public static void printUsage() 
  {
      System.out.println("Usage: java com.zosatapo.xls.Xls2SQL {options}");
      System.out.println("  Options are:");
      System.out.println("    -schema file          Use this file instead of xls2sql.properties(optional)");
      System.out.println("    -input  file          xls input file (required)");
      System.out.println("    -debug  [true|false]  ");
  }
  
  public static void main(String args[])
  {
    try
    {
      Xls2SQL imp=new Xls2SQL();
      imp.process(args);
    }
    catch(Exception ex)
    {
      ex.printStackTrace();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -