zoukankan      html  css  js  c++  java
  • EXCEL导入数据库二

    2、将Excel的数据导入SQL server :
    SELECT * into newtable
    FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
      'Data Source="c:\book1.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...[Sheet1$]

    实例:
    SELECT * into newtable
    FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
      'Data Source="c:\Finance\account.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions

    3、将SQL SERVER中查询到的数据导成一个Excel文件
    T-SQL代码:
    EXEC master..xp_cmdshell 'bcp 库名.dbo.表名out c:\Temp.xls -c -q -S"servername" -U"sa" -P""'
    参数:S 是SQL服务器名;U是用户;P是密码
    说明:还可以导出文本文件等多种格式

    实例:EXEC master..xp_cmdshell 'bcp saletesttmp.dbo.CusAccount out c:\temp1.xls -c -q -S"pmserver" -U"sa" -P"sa"'

    EXEC master..xp_cmdshell 'bcp "SELECT au_fname, au_lname FROM pubs..authors ORDER BY au_lname" queryout C:\ authors.xls -c -Sservername -Usa -Ppassword'

    ==============================================================================================

    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, comobj, StdCtrls, DB, ADODB;

    type
    TForm1 = class(TForm)
        Button1: TButton;
        OpenDialog: TOpenDialog;
        Query1: TADOQuery;
        procedure Button1Click(Sender: TObject);
    private
        { Private declarations }
    public
        { Public declarations }
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.Button1Click(Sender: TObject);
    var
    excelx,excely,excelz : string;
    ExcelApp,WorkBook: Olevariant;
    ExcelRowCount,i :Integer;
    begin
    opendialog.Execute;
    try
        ExcelApp := CreateOleObject('Excel.Application');
       // WorkBook :=ExcelApp;
        WorkBook := ExcelApp.WorkBooks.Open(opendialog.FileName);//使用opendialog对话框指定
          //excel档路径
        ExcelApp.Visible := false;
        ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
        for i := 1 to excelrowcount + 1 do
        begin
          excelx := excelapp.Cells[i,1].Value;
          excely := excelapp.Cells[i,2].Value;
          excelz := excelapp.Cells[i,3].Value;
       (* if ((excelapp.Cells[i,1].Value = '') and (ExcelApp.Cells[i,2].Value = '')) then //指定excel档的第 i 行 ,第 1,2(看情况而定)行如果为空就退出,这样的设定,最好是你的档案力这两行//对应数据库中不能为空的数据
         exit
        else *)
        with query1 do
        begin
        close;
        sql.clear;
        sql.add('insert into ceshi(one,two,three) values(:a,:b,:c)');
        Parameters.ParamByName('a').Value := excelx;//excel档的第一列插入到aa表的 a 栏位;
        Parameters.ParamByName('b').Value := excely;//excel档的第二列插入到aa表的 b 栏位;
        Parameters.ParamByName('c').Value := excelz;
        execsql;
        end;
        end;
        finally
          WorkBook.Close;
          ExcelApp.Quit;
          ExcelApp := Unassigned;
          WorkBook := Unassigned;
          showmessage('导入成功!');
        end;
    end;
    end.

  • 相关阅读:
    linux基础知识之vi编辑器的使用
    Linux的通信命令
    Linux学习之文件的压缩与解压
    Liux文件操作
    Linux简单学习
    Drupal V7.3.1 框架处理不当导致SQL注入
    Typecho V1.1反序列化导致代码执行分析
    浅析PHP反序列化漏洞之PHP常见魔术方法(一)
    python正则表达式记录
    SQLmap源码分析之框架初始化(一)
  • 原文地址:https://www.cnblogs.com/mingdep/p/2332229.html
Copyright © 2011-2022 走看看