将excel工作表转换为sql脚本 [英] convert excel worksheet to sql script
问题描述
我尝试过各种工具,如 http://www.sqlconverter.com/ ,但我是无法得到正确的解决方案。我可以做任何其他方式吗?
我注意到你的评论说使用导入向导比一个解决方案更复杂您必须尝试加载数据。
您可以尝试 BULK INSERT
:
首先,在每个工作表上执行SAVE AS,并将其转换为CSV文件。您应该为要导入的每个工作表都有一个CSV文件。
接下来,创建一个具有类似数据类型和数据类型的表。典型的Excel单元格是一个VARCHAR(255),(可能更像NVARCHAR(255),如果你想具体,但我们将避免unicode为这个解决方案。)
所以,如果你的excel表有5列:
CREATE TABLE Sheet1
(Column1 VARCHAR(255)
,Column2 VARCHAR(255)
,Column3 VARCHAR(255)
,Column4 VARCHAR(255)
,Column5 VARCHAR(255)
)
然后你可以写一个简单的批量插入到表PROVIDED你有网络共享上的文件或本地到服务器/机器,其中SQL实例是。例如,如果您的机器上有该文件,并希望尝试在网络上推送服务器,则SQL会在下面的脚本中考虑 C:\
在服务器上,而不是您的机器。您必须共享文件夹并通过网络进行访问: \\MyMachineName\SharedFolder\Sheet1.csv
BULK INSERT dbo.Sheet1
FROM'C:\LocalFolder\WhereTheFileIs\Sheet1.csv'
WITH(
FIELDTERMINATOR =','
,ROWTERMINATOR ='\\\
'
)
如果文件和表中存在相同数量的列,那么应该将数据转换到该表中。
它不漂亮,但很简单。 BULK INSERT
是一种经过验证的真正的基本和快速加载方法。
I have a excel worksheet (.xls). I need to convert it into sql script. The single excel worksheet consists of multiple tables. So the resultant script should have multiple create table and insert statements. I tried various tools such as http://www.sqlconverter.com/ but I am unable to get a proper solution. Any other way I can do it?
I noticed your comment that using the import wizard was more complicated of a solution than you wanted, so you must be trying to load data.
You can try BULK INSERT
:
First, do a SAVE AS on each sheet and convert them to CSV files. You should have one CSV file for each sheet you want to import.
Next, make a table with the similar data types and length that you'll be bringing in. A typical Excel cell is a VARCHAR(255), (probably more like NVARCHAR(255) if you want to be specific, but we'll avoid unicode for this solution).
So, if your excel sheet had 5 columns:
CREATE TABLE Sheet1
(Column1 VARCHAR(255)
, Column2 VARCHAR(255)
, Column3 VARCHAR(255)
, Column4 VARCHAR(255)
, Column5 VARCHAR(255)
)
Then you can write a simple bulk insert to the table PROVIDED you have the file on network share or local the to server/machine where the SQL instance is. For example, if you had the file on your machine and wanted to try and push to a server out on the network, SQL would think the C:\
in the script below was on the server and not your machine. You would have to share a folder and access it over the network: \\MyMachineName\SharedFolder\Sheet1.csv
BULK INSERT dbo.Sheet1
FROM 'C:\LocalFolder\WhereTheFileIs\Sheet1.csv'
WITH (
FIELDTERMINATOR = ','
, ROWTERMINATOR = '\n'
)
This should get the data into that table provided the same number of columns exist in the file and table.
It's not pretty, but it's simple. The BULK INSERT
is a tried and true method of basic and quick loading.
这篇关于将excel工作表转换为sql脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!