`
lancijk
  • 浏览: 384442 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

数据库数据直接导入到EXCEL中

阅读更多
/** 导入文本文件 */
EXEC master..xp_cmdshell 'bcp dbname..tablename in c:\DT.txt -c -S servername -U sa -P password' 

/** 导出文本文件 */
EXEC master..xp_cmdshell 'bcp dbname..tablename out c:\DT.txt -c -S servername -U sa -P password' 
或 
EXEC master..xp_cmdshell 'bcp "Select * from dbname..tablename" queryout c:\DT.txt -c -S servername  -U sa -P password' 


例如:

EXEC master..xp_cmdshell 'bcp myDatabase..TableTest1 out e:\test.doc  -c -S localhost -U sa -P sa123'

可以生成txt,doc,xls等格式

 

注:bcp的用法

 bcp {dbtable | query} {in | out | queryout | format} 数据文件

  [-m 最大错误数]             [-f 格式化文件]         [-e 错误文件]

  [-F 首行]                   [-L 末行]             [-b 批大小]

  [-n 本机类型]               [-c 字符类型]         [-w 宽字符类型]

  [-N 将非文本保持为本机类型] [-V 文件格式版本]     [-q 带引号的标识符]

  [-C 代码页说明符]           [-t 字段终止符]       [-r 行终止符]

  [-i 输入文件]               [-o 输出文件]         [-a 数据包大小]

  [-S 服务器名称]             [-U 用户名]           [-P 密码]

  [-T 可信连接]               [-v 版本]             [-R 允许使用区域设置]

  [-k 保留空值]               [-E 保留标识值]

  [-h"加载提示"]              [-x 生成 xml 格式化文件]

 

--注意:在运行xp_cmdshell的时候,SqlServer可能会阻止对xp_cmdshell的--访问,因为此组件已作为此服务器安全配置的一部分而被关闭,运行以下代码可以--解决问题。

-- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options', 1
GO
-- To update the currently configured value for advanced options.
RECONFIGURE
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1
GO
-- To update the currently configured value for this feature.
RECONFIGURE
GO

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics