如何使用 Excel 列创建 SQL 表?

发布于 2024-09-05 02:46:07 字数 1759 浏览 5 评论 0原文

我需要帮助自动从 Excel 生成列名称。我认为:我们可以

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

通过 C# 执行以下代码。如何从 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();
}

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野侃 2024-09-12 02:46:07

您应该能够迭代 DataTable 的列集合以获取列名称。

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

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);
}
始终不够 2024-09-12 02:46:07

必须是C#吗?如果您愿意使用 Java,我在 Apache POI 上取得了非常好的结果:http://poi.apache .org/

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/

蓝颜夕 2024-09-12 02:46:07

这不是一个 C# 解决方案...这是一个直接来自 Excel 的快速但肮脏的解决方案。
C# 解决方案会更强大,并且允许您最有可能将其指向目标 xls 并让它为您提供答案 - 如果您需要快速找到答案并且没有时间编写程序,或者如果有人他们的计算机上没有 C# 开发环境。

获得所需结果的一种可能方法是:

  • 突出显示 Excel 中具有列标题的行
  • 将其复制
  • 到新工作表
  • 右键单击​​单元格 A1
  • 单击粘贴转置
  • 它将以列格式粘贴它们
  • go到 B2 并将此公式粘贴到:

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

  • 然后将该公式一直粘贴到列标题列旁边

  • 将结果复制到 SQL Server,然后添加最上面一行代码
    "CREATE TABLE [dbo].[Addresses_Temp] ( "

  • 然后添加右括号

我们所做的是:

  • 我们从标题 ROW 中获取所有列标题并将
  • 它们放入列中
  • 然后删除所有空格(如果它们是多字列标题)并
  • 添加到开括号“[”的开头,并
  • 添加到末尾“] VARCHAR(20),”(代码行的其余部分)

在此处输入图像描述< /a>

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)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文