简体   繁体   English

将SQL文件导出到Excel

[英]Export SQL file into Excel

I have a sql file which has a lot of insert statements (1000+), 我有一个SQL文件,其中包含很多插入语句(1000+),

eg 例如

insert into `pubs_for_client` (`ID`, `num`, `pub_name`, `pub_address`, `pub_tele`, `pub_fax`, `pub_email`, `publ_website`, `publ_vat`, `pub_last_year`, `titles_on_backlist`, `Personnel`) values('2475','2473','xxx xxx xxx','xxx xxx, xxxx, xxxx, xxxx, ','000000 ','0000 ',NULL,NULL,NULL,NULL,NULL,NULL);

My client wants these in an excel file. 我的客户希望将这些保存在excel文件中。

Is it possible to extract the information from this sql file and import it into an excel spreadsheet? 是否可以从此sql文件中提取信息并将其导入到Excel电子表格中? I'm using excel 2007. 我正在使用Excel 2007。

如果您在某处安装了mysql并且安装了phpMyAdmin,则只需导入.sql文件,然后直接从phpMyAdmin中将表导出为.xls文件即可。

edit: If you want to extract the data from the Insert statements without actually running them, you could: 编辑:如果要从Insert语句中提取数据而不实际运行它们,则可以:

  • Save the SQL file as a CSV file 将SQL文件另存为CSV文件
  • Open it in Excel and it will automatically split the statements up into bits using the commas 在Excel中将其打开,它将使用逗号自动将语句拆分为多个位
  • Most of the columns to the left of the data wont be worth keeping, so delete them 数据左侧的大多数列都不值得保留,因此请将其删除
  • The right-most columns should contain the data 最右边的列应包含数据

If you are not keen on installing any tools you could try this...its simple but needs some regex matching with trial and error for strings. 如果您不希望安装任何工具,可以尝试一下...虽然很简单,但是需要一些正则表达式,并需要反复试验字符串。

  1. Copy over the column names to excel 将列名称复制到Excel
  2. Open the file with the insert statements and hit replace all. 使用insert语句打开文件,然后全部替换。 Insert into pubs_for_client (ID, num, pub_name, pub_address, pub_tele, pub_fax, pub_email, publ_website, publ_vat, pub_last_year, titles_on_backlist, Personnel) values( with nothing. 插入pubs_for_client(ID,num,pub_name,pub_address,pub_tele,pub_fax,pub_email,publ_website,publ_vat,pub_last_year,titles_on_backlist,Personnel)值(不包含任何内容。
  3. Find )\\n with wild card and remove all of them. 使用通配符找到)\\ n并将其全部删除。
  4. Now this text file is ready to be imported to excel. 现在,此文本文件已准备好导入excel。 You can copy it over to clipboard and then to excel or open with excel and use. 您可以将其复制到剪贴板,然后复制到excel或使用excel打开并使用。

Side Note : you could also try asking your client to send it in excel henceforth its usually available in most data providers. 旁注:您也可以尝试要求您的客户以excel格式发送它,此后它通常在大多数数据提供程序中都可用。

You mean export the values? 您是说导出值?

Assuming you're using the SQL server + tools the simplest method would be to 假设您使用的是SQL Server +工具,最简单的方法是

  1. actually insert those values into a table (creating a temp table if necessary) 实际上将这些值插入表中(如有必要,创建临时表)
  2. select * from pubs_for_client
  3. copy + paste the results table into Excel. 复制结果表并将其粘贴到Excel中。

You'll have to set the column widths yourself, and if you have number-as-a-string columns you want to keep as strings (eg to preserve leading zeroes) then you'll need to set the receiving column type first. 您必须自己设置列宽,如果您要保留字符串形式的数字列(例如,保留前导零),则需要首先设置接收列的类型。

If not, and if you can't use similar tools for your database, you can use regexps or just use your text editor to convert your SQL file into a CSV which Excel will read. 如果不是这样,并且如果不能在数据库中使用类似的工具,则可以使用正则表达式或仅使用文本编辑器将SQL文件转换为Excel可以读取的CSV。 You'd also have to edit out quotes from values and nulls and doubled-to-quote apostrophes. 您还必须删除值,空值和双引号撇号的引号。 Unfortunately you can't then specify column types in advance and you would eg lose leading zeros on numbers. 不幸的是,您之后不能预先指定列类型,例如,数字会丢失前导零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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