简体   繁体   中英

How can I create a SQL table using excel columns?

I need to help to generate column name from excel automatically. I think that: we can do below codes:

CREATE TABLE [dbo].[Addresses_Temp] ( 
    [FirstName]   VARCHAR(20), 
    [LastName]    VARCHAR(20), 
    [Address]     VARCHAR(50), 
    [City]        VARCHAR(30), 
    [State]       VARCHAR(2), 
    [ZIP]         VARCHAR(10) 
) 

via C#. How can I learn column name from Excel?

private void Form1_Load(object sender, EventArgs e)
{
   ExcelToSql();
}

void ExcelToSql()
{
    string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Source\MPD.xlsm;Extended Properties=""Excel 12.0;HDR=YES;""";
    // if you don't want to show the header row (first row)            
    // use 'HDR=NO' in the string             
    string strSQL = "SELECT * FROM [Sheet1$]";            
    OleDbConnection excelConnection = new OleDbConnection(connectionString);           
    excelConnection.Open(); // This code will open excel file.            
    OleDbCommand dbCommand = new OleDbCommand(strSQL, excelConnection);          
    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(dbCommand);            

    // create data table            
    DataTable dTable = new DataTable();            
    dataAdapter.Fill(dTable);            

    // bind the datasource          
    //  dataBingingSrc.DataSource = dTable;         
    // assign the dataBindingSrc to the DataGridView         
    // dgvExcelList.DataSource = dataBingingSrc;             // dispose used objects          
    if (dTable.Rows.Count > 0)
       MessageBox.Show("Count:" + dTable.Rows.Count.ToString());
    dTable.Dispose();           
    dataAdapter.Dispose();           
    dbCommand.Dispose();            
    excelConnection.Close();           
    excelConnection.Dispose();
}

Does it have to be C#? If you're willing to use Java, I've had really good results with Apache POI: http://poi.apache.org/

You should be able to iterate over the DataTable's columns collection to get the column names.

System.Data.DataTable dt;
dt = new System.Data.DataTable();
foreach(System.Data.DataColumn col in dt.Columns)
{
     System.Diagnostics.Debug.WriteLine(col.ColumnName);
}

This is not a C# solution... it is a quick and dirty solution right from excel. A c# solution would be more robust and allow you to most likely point it to a target xls and have it give you the answers - this solution is for if you need the answers fast and don't have time to write a program or if someone does not have C# development environment on their computer.

One possible way to get the results you're looking for is:

  • highlight the row in excel that has the column headers
  • copy them
  • go to a new worksheet
  • right click cell A1
  • click paste-transpose
  • it will paste them in column format
  • go to B2 and paste this formula in:

    =CONCATENATE("[",SUBSTITUTE(A1," ",""),"] varchar(20),")

  • then paste that formula all the way down next to your column of column headers

  • copy the results into SQL Server then add your top line of code "CREATE TABLE [dbo].[Addresses_Temp] ( "

  • then add your closing parentheses

What we did is:

  • we got all the colunn headers from the header ROW and
  • made them into a column
  • then removed all spaces (should they be multiword column headers) and
  • tacked onto the beginning the open bracket "[" and
  • tacked onto the end "] VARCHAR(20)," (the rest of the line of code)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM