【问题标题】:How to open sql .csv file?如何打开 sql .csv 文件?
【发布时间】:2014-07-14 10:22:55
【问题描述】:

在 sql 中使用 SELECT 查询我得到了包含许多行和列的结果。它按行和列很好地显示,没有一个混在一起。

我右键单击左上角并另存为 .csv。然后我使用 Excel 打开这个文件,我看到许多字段混合在一起,或者单词、值进入了错误的列。

你知道保存结果的正确方法是什么,或者从sql中查看这个.csv文件的正确方法是什么?

谢谢。

按要求更新:我使用 Microsoft SQL Server Management Studio。 在保存到 csv 文件之前,我的结果表中的一些行:(此处包含几列)

Company Pte Ltd Advanced Materials Technologies Pte Ltd. produces precision metal and ceramic parts. It offers metal hinges, metal packages, intricate cylinders, and valve components; latches and counterweights for hard disk drives; 

或者一行有两列(第一是公司名称,第二是公司特征):

ABC PTE LTD  •Project Management
  •Precision Mould Design and Fabrication
  •Multi Colours/Materials Moulding
  •Over Moulding
  •Extreme Thin Wall Compression Moulding
  •Micro Moulding
 

我尝试单击结果表的左上角以全选,然后右键单击并选择“带有标题的复制”然后过去到excel。但这里有一些问题: 在 SSMS 中,我在这些数据中清楚地看到了结果:

XYZ Ltd <p>  <table border="0" cellspacing="0" cellpadding="0" width="780">  <tbody>  <tr>  <td valign="top" width="140">  <table border="0" cellspacing="0" cellpadding="0" width="140" height="628">  <tbody>  <tr>  <td height="628" valign="top" width="140">  <p></p></td></tr></tbody></table></td>  <td valign="top" width="500">  <table border="0" cellspacing="0" cellpadding="0" width="500" align="center" height="619">  <tbody>  <tr>  <td height="619" width="500">  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><b><font color="#333333" face="arial"><span style="FONT-SIZE: 12pt">About Us</span></font></b></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p><img src="http://www.shaofook.com/images/01.jpg" width="240" height="180"> &nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.shaofook.com/images/02.jpg" width="240" height="180">   <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><strong><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"><br />Shao Fook Engineering </span></font></strong><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"><b>Pte Ltd </b>was established in March 1989 as a partnership offering contract services for industrial steel fabrication, installation of equipment for oil refineries, petrochemical, industrial plants, turnkey construction project works for factory expansion and most recently the pharmaceutical industry.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We undertake project work from laydown to complete installation of Packaged equipment such as Pipe Rack Structures, Equipment Structures, Flare Stack Structure, Fired Heaters, HRSG, Boilers, Incinerators, Bio-treaters, Cooling Tower as well as Modular equipment such as Compressors, Pumps, Reactors, Silos, Towers, Exchanges, Loading Arms and quick turn around plant maintenance &nbsp;projects.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"></span></font>&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We also perform Laser Alignment of Rotating Equipment.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We have clocked more than 5 million man-hours and handled more than 100,000 Ton of Steel Structure and 50,000 Ton of equipment within customer's budget and schedule. We diligently adhere to safety procedure outlined in our established Work Procedure, Method Statement and Job Safety Analysis.</span></font></p></td></tr></tbody></table></td></tr></tbody></table>  <table border="0" cellspacing="0" cellpadding="0" width="780">  <tbody>  <tr>  <td valign="top" width="140">  <table border="0" cellspacing="0" cellpadding="0" width="140" height="628">  <tbody>  <tr>  <td height="628" valign="top" width="140">  <p></p></td></tr></tbody></table></td>  <td valign="top" width="500">  <table border="0" cellspacing="0" cellpadding="0" width="500" align="center" height="619">  <tbody>  <tr>  <td height="619" width="500">  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><b><font color="#333333" face="arial"><span style="FONT-SIZE: 12pt">About Us</span></font></b></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p><img src="http://www.shaofook.com/images/01.jpg" width="240" height="180"> &nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.shaofook.com/images/02.jpg" width="240" height="180">   <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><strong><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"><br />Shao Fook Engineering </span></font></strong><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"><b>Pte Ltd </b>was established in March 1989 as a partnership offering contract services for industrial steel fabrication, installation of equipment for oil refineries, petrochemical, industrial plants, turnkey construction project works for factory expansion and most recently the pharmaceutical industry.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We undertake project work from laydown to complete installation of Packaged equipment such as Pipe Rack Structures, Equipment Structures, Flare Stack Structure, Fired Heaters, HRSG, Boilers, Incinerators, Bio-treaters, Cooling Tower as well as Modular equipment such as Compressors, Pumps, Reactors, Silos, Towers, Exchanges, Loading Arms and quick turn around plant maintenance &nbsp;projects.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"></span></font>&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We also perform Laser Alignment of Rotating Equipment.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We have clocked more than 5 million man-hours and handled more than 100,000 Ton of Steel Structure and 50,000 Ton of equipment within customer's budget and schedule. We diligently adhere to safety procedure outlined in our established Work Procedure, Method Statement and Job Safety Analysis.</span></font></p></td></tr></tbody></table></td></tr></tbody></table><font color="#333333"><strong><font size="2">Shao Fook Engineering </font></strong><font face="arial"><span style="FONT-SIZE: 9pt"><strong>Pte Ltd </strong>was established in March 1989 as a partnership offering contract services for industrial steel fabrication, installation of equipment for oil refineries, petrochemical, industrial plants, turnkey construction project works for factory expansion and most recently the pharmaceutical industry.</span></font></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We undertake project work from laydown to complete installation of Packaged equipment such as Pipe Rack Structures, Equipment Structures, Flare Stack Structure, Fired Heaters, HRSG, Boilers, Incinerators, Bio-treaters, Cooling Tower as well as Modular equipment such as Compressors, Pumps, Reactors, Silos, Towers, Exchanges, Loading Arms and quick turn around plant maintenance &nbsp;projects.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"></span></font>&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We also perform Laser Alignment of Rotating Equipment.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We have clocked more than 5 million man-hours and handled more than 100,000 Ton of Steel Structure and 50,000 Ton of equipment within customer's budget and schedule. We diligently adhere to safety procedure outlined in our established Work Procedure, Method Statement and Job Safety Analysis.</span></font></p>   - Heavy Lifting & Logistic Services    - Laser Alignment and Leveling of Rotating Equipment    - Industrial Fabrication Work & Plant Maintenance    - Equipment Installation for Factories & Industrial Plants    - Construction Works - Mechanical (Major), Civil, Electrical    - Conveyance & Installation of Material Handling & Production   Machines  - Heavy Lifting & Logistic Services    - Laser Alignment and Leveling of Rotating Equipment    - Industrial Fabrication Work & Plant Maintenance    - Equipment Installation for Factories & Industrial Plants    - Construction Works - Mechanical (Major), Civil, Electrical    - Conveyance & Installation of Material Handling & Production   Machines

但是当我粘贴到 excel 中时,我会得到这个:

XYZ Ltd <p>  <table border="0" cellspacing="0" cellpadding="0" width="780">  <tbody>  <tr>  <td valign="top" width="140">  <table border="0" cellspacing="0" cellpadding="0" width="140" height="628">  <tbody>  <tr>  <td height="628" valign="top" width="140">  <p></p></td></tr></tbody></table></td>  <td valign="top" width="500">  <table border="0" cellspacing="0" cellpadding="0" width="500" align="center" height="619">  <tbody>  <tr>  <td height="619" width="500">  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><b><font color="#333333" face="arial"><span style="FONT-SIZE: 12pt">About Us</span></font></b></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p><img src="http://www.shaofook.com/images/01.jpg" width="240" height="180"> &nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.shaofook.com/images/02.jpg" width="240" height="180">   <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><strong><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"><br />Shao Fook Engineering </span></font></strong><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"><b>Pte Ltd </b>was established in March 1989 as a partnership offering contract services for industrial steel fabrication, installation of equipment for oil refineries, petrochemical, industrial plants, turnkey construction project works for factory expansion and most recently the pharmaceutical industry.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We undertake project work from laydown to complete installation of Packaged equipment such as Pipe Rack Structures, Equipment Structures, Flare Stack Structure, Fired Heaters, HRSG, Boilers, Incinerators, Bio-treaters, Cooling Tower as well as Modular equipment such as Compressors, Pumps, Reactors, Silos, Towers, Exchanges, Loading Arms and quick turn around plant maintenance &nbsp;projects.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"></span></font>&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We also perform Laser Alignment of Rotating Equipment.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We have clocked more than 5 million man-hours and handled more than 100,000 Ton of Steel Structure and 50,000 Ton of equipment within customer's budget and schedule. We diligently adhere to safety procedure outlined in our established Work Procedure, Method Statement and Job Safety Analysis.</span></font></p></td></tr></tbody></table></td></tr></tbody></table>  <table border="0" cellspacing="0" cellpadding="0" width="780">  <tbody>  <tr>  <td valign="top" width="140">  <table border="0" cellspacing="0" cellpadding="0" width="140" height="628">  <tbody>  <tr>  <td height="628" valign="top" width="140">  <p></p></td></tr></tbody></table></td>  <td valign="top" width="500">  <table border="0" cellspacing="0" cellpadding="0" width="500" align="center" height="619">  <tbody>  <tr>  <td height="619" width="500">  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><b><font color="#333333" face="arial"><span style="FONT-SIZE: 12pt">About Us</span></font></b></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p><img src="http://www.shaofook.com/images/01.jpg" width="240" height="180"> &nbsp;&nbsp;&nbsp;&nbsp;<img src="http://www.shaofook.com/images/02.jpg" width="240" height="180">   <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><strong><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"><br />Shao Fook Engineering </span></font></strong><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"><b>Pte Ltd </b>was established in March 1989 as a partnership offering contract services for industrial steel fabrication, installation of equipment for oil refineries, petrochemical, industrial plants, turnkey construction project works for factory expansion and most recently the pharmaceutical industry.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We undertake project work from laydown to complete installation of Packaged equipment such as Pipe Rack Structures, Equipment Structures, Flare Stack Structure, Fired Heaters, HRSG, Boilers, Incinerators, Bio-treaters, Cooling Tower as well as Modular equipment such as Compressors, Pumps, Reactors, Silos, Towers, Exchanges, Loading Arms and quick turn around plant maintenance &nbsp;projects.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"></span></font>&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We also perform Laser Alignment of Rotating Equipment.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We have clocked more than 5 million man-hours and handled more than 100,000 Ton of Steel Structure and 50,000 Ton of equipment within customer's budget and schedule. We diligently adhere to safety procedure outlined in our established Work Procedure, Method Statement and Job Safety Analysis.</span></font></p></td></tr></tbody></table></td></tr></tbody></table><font color="#333333"><strong><font size="2">Shao Fook Engineering </font></strong><font face="arial"><span style="FONT-SIZE: 9pt"><strong>Pte Ltd </strong>was established in March 1989 as a partnership offering contract services for industrial steel fabrication, installation of equipment for oil refineries, petrochemical, industrial plants, turnkey construction project works for factory expansion and most recently the pharmaceutical industry.</span></font></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We undertake project work from laydown to complete installation of Packaged equipment such as Pipe Rack Structures, Equipment Structures, Flare Stack Structure, Fired Heaters, HRSG, Boilers, Incinerators, Bio-treaters, Cooling Tower as well as Modular equipment such as Compressors, Pumps, Reactors, Silos, Towers, Exchanges, Loading Arms and quick turn around plant maintenance &nbsp;projects.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt"></span></font>&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We also perform Laser Alignment of Rotating Equipment.</span></font></p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px">&nbsp;</p>  <p style="LINE-HEIGHT: 100%; MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px"><font color="#333333" face="arial"><span style="FONT-SIZE: 9pt">We have clocked more than 5 million man-hours and handled more than 100,000 Ton of Steel Structure and 50,000 Ton of equipment within customer's budget and schedule. We diligently adhere to safety procedure outlined in our established Work Procedure, Method Statement and Job Safety Analysis.</span></font></p>   #NAME?  #NAME?

所以有2列没有被识别并放错数据:

#NAME?  #NAME?

【问题讨论】:

  • 您的意思是,您想读取或导入 csv 文件?
  • 我的意思是我怎么能读取(不导入).csv 文件?是否有任何阅读器(如 excel?)可以以我在 sql server 中看到的相同表格格式阅读它?
  • 所以基本上你想使用哪种语言 .net 、php 或什么来读取 .csv 格式的 sql 表?
  • 这个链接一定能帮到你blog.sqlauthority.com/2008/02/06/…
  • 我不需要使用任何语言来阅读它,任何可以查看该文件的编辑器都可以(就像我们使用记事本、写字板、word ... 来阅读文本文件)。您发布的链接是另一种方式,将 .csv 导入 sql server。但是我从 sql server 导出到 .csv 并将这个文件传递给其他人,他不使用 sql server 但他想使用更简单的程序来查看这个文件并在桌子上做一些事情。

标签: sql excel csv


【解决方案1】:

很可能,当您保存 CSV 文件时,您要么在字段上没有任何分隔符,要么使用不是 Excel 所需的双引号分隔符(在这种情况下,很可能是单引号)的分隔符。

Excel 要求任何本身包含逗号的字段都必须用双引号引起来,否则它无法分辨每个字段在文件中的结束位置,并且在加载文件时结果会显得“混淆”。

这只是一个猜测,因为您没有向我们展示您在加载时遇到问题的 CSV 文件示例。如果您要显示该文件的几行,我们可能会更清楚发生了什么。

此外,由于您没有提及您使用哪个 SQL 产品和前端来创建 CSV 文件,因此无法建议您如何解决引用问题(如果确实是这个问题)。

【讨论】:

  • 更新了我的问题,使其更清晰。你的猜测和我想的非常接近。也许我可以将逗号更改为其他内容,就像我现在使用的选择查询一样:“SELECT ', '+ u.description from KeySkills u INNER JOIN Companies c ON c.companyId = u.companyId WHERE c.companyName = c1. companyName ORDER BY c.companyName FOR XML PATH('')), 1, 1, '') AS listStr " 但我认为它不能解决所有问题,因为它本身具有一些特殊字符的数据,我可以清理这部分数据。
  • 有几件事:1)您没有提到自己正在创建 CSV 格式。在这种情况下,您有责任添加字段引号并将文本内的任何引号加倍。最好提取您想要的字段并让 SSMS 创建 CSV 文件。 2)您的价值观中确实有内部逗号,因此需要引用。
  • 那么如何让 SSMS 创建 CSV 文件,以及如何进行引用?
  • 在 SSMS 中运行您的查询,然后右键单击结果部分中的任何列标题。选择将结果另存为。 . .,从下拉列表中选择 CSV,然后输入文件名。应正确引用生成的文件以加载到 Excel 中。
  • 这就是我使用的方式,而不是保存单列,我选择左上角保存整个结果表。所以我不会自己创建 CSV 格式吗?我正在做你提到的事情,但 Excel 没有正确加载它。我在 excel 中看到的非常混乱,不像在 SSMS 中那样。
猜你喜欢
  • 1970-01-01
  • 2020-10-09
  • 2016-11-16
  • 2011-08-14
  • 1970-01-01
  • 1970-01-01
  • 2015-12-13
  • 2019-11-16
  • 1970-01-01
相关资源
最近更新 更多