using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.IO;
using System.Collections;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string name;
int sum;
//Array动态数组的建立,用来存列名
ArrayList aList = new ArrayList();
Console.WriteLine("输入要建立的文件名(包括后缀)");
name = Console.ReadLine().ToString();
Console.WriteLine("输入要建立几个列名");
sum = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < sum; i++)
{
Console.WriteLine("请输入列名");
aList.Add(Console.ReadLine().ToString());
}
addtable(name, aList);
}
//public static bool newtable(string name)
//{
// FileStream f1 = new FileStream(name, FileMode.Open);
// if (f1 != null)
// {
// return true;
// }
// return false;
//}
public static void addtable(string name, ArrayList a)
{
//try
//{
// FileStream f = new FileStream(name, FileMode.Create);
// f.Close();
//}
//catch
//{
// Console.WriteLine("jianlishibei");
//}
//string strOdbcCon = @"Provider=Microsoft.ACE.OLEDB.12.0; Persist Security Info=False;Data Source=1.xlsx; Extended Properties=Excel 8.0";
string strOdbcCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" + name + ";Extended Properties='Excel 8.0;'";
//string strOdbcCon = @"Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info=False;Data Source=1.xlsx;Extended Properties='Excel 8.0;'";
OleDbConnection OleDB = new OleDbConnection(strOdbcCon);
//string strCom = " Create Table [Sheet1] (" + col1 + " varchar(20)," + col2 + " varchar(20))";
OleDB.Open();
foreach (object o in a)
{
//string strCom = "Create Table [Sheet1] (" + o.ToString() + " varchar(20))";
string strCom = "alter table [Sheet1] add " + o.ToString() + " varchar(20)";
OleDbCommand ass = new OleDbCommand(strCom, OleDB);
ass.ExecuteNonQuery();
}
OleDB.Close();
}
}
}