如何使用 Excel 列创建 SQL 表?
我需要帮助自动从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够迭代 DataTable 的列集合以获取列名称。
You should be able to iterate over the DataTable's columns collection to get the column names.
必须是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/
这不是一个 C# 解决方案...这是一个直接来自 Excel 的快速但肮脏的解决方案。
C# 解决方案会更强大,并且允许您最有可能将其指向目标 xls 并让它为您提供答案 - 如果您需要快速找到答案并且没有时间编写程序,或者如果有人他们的计算机上没有 C# 开发环境。
获得所需结果的一种可能方法是:
go到 B2 并将此公式粘贴到:
=CONCATENATE("[",SUBSTITUTE(A1," ",""),"] varchar(20),")
然后将该公式一直粘贴到列标题列旁边
将结果复制到 SQL Server,然后添加最上面一行代码
"CREATE TABLE [dbo].[Addresses_Temp] ( "
然后添加右括号
我们所做的是:
< /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:
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: