乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 如何实现Excel数据批量导入到远程SQL Server数据...-excel批量导入mysql,mysql导入多个ex

如何实现Excel数据批量导入到远程SQL Server数据...-excel批量导入mysql,mysql导入多个ex

作者:乔山办公网日期:

返回目录:excel表格制作


在查询分析器里,直接写 SQL语句:
如果是导入数据到现有表,则采用形式
INSERT INTO 表 SELECT * FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0' ,'Excel 5.0;HDR=YES;DATABASE=c:\test.xls',sheet1$)
如果是导入数据并新增表,则采用形式
SELECT * INTO 表 FROM OPENROWSET('MICROSOFT.JET.OLEDB.4.0' ,'Excel 5.0;HDR=YES;DATABASE=c:\test.xls',sheet1$)
SQL2005中直接可以实现导入功能 SQL2008不知道可不可以。 操作过程如下:
第一步:登录到 SQL Server Management Studio
第二步:在 “对象资源管理器 ”中右键单击 “管理 ”,在弹出列表中单击 “导入数据 ”
第三步:在 “导入向e799bee5baa6e79fa5e98193e4b893e5b19e333导 ”对话框中单击 “下一步 ”,进入到 “选择数据源 ”对话框,在 “数据源 ”列表中选择 “Microsoft Excel ”,同时选择相应的 Excel 文档,完成后单击 “下一步 ”(一定要勾选该对话框中的 “首行包含列名称 ”,因此它是将 Excel文档中的列标题为数据库表中的列项标题)
第四步:指定目标数据库服务,依次单击 “下一步 ”。。。。至到 “完成 ”
第五步:重新打到 SQL Server Management Studio,进入到导入的数据库表,可以发现所导入的 Excel文档数据。
不行的话参考http://blog.sina.com.cn/s/blog_48e42dc90100n1m3.html

数据批量导入程SQL服务器亲身实
http://blog.csdn.net/hoostone/article/details/5600042
第一步:建立数据库和数据表(按照自己的Excel数据设立字段)。
[sql] view plain copy print?
CREATE DATABASE php_excel;
USE php_excel;
CREATE TABLE IF NOT EXISTS php_excel(
id int(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
gid varchar(20) NOT NULL,
stu_no varchar(20) NOT NULL,
name varchar(45) NOT NULL,
age int(4) NOT NULL
)ENGINE=MyISAM DEFAULT CHARSET=utf8;

第二步:前台index.php文件。
[html] view plain copy print?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http:///1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>phpexcel导入excel数据到MYSQL数据库</title>
</head>

<body>
<form name="frm1" action="insertdb.php" method="post" enctype="multipart/form-data">
<input name="filename" type="file" />
<input name="submit" type="submit" value="import" />
</form>
</body>
</html>

第三步:向数据库插入数据的insertdb.php文件。
[php] view plain copy print?
session_start();
header("Content-type:text/html;charset:utf-8");
//全局变量

$succ_result=0;
$error_result=0;
$file=$_FILES['filename'];
$max_size="2000000"; //最大文件限制(单位:byte)
$fname=$file['name'];
$ftype=strtolower(substr(strrchr($fname,'.'),1));
//文件格式
$uploadfile=$file['tmp_name'];
if($_SERVER['REQUEST_METHOD']=='POST'){
if(is_uploaded_file($uploadfile)){
if($file['size']>$max_size){
echo "Import file is too large";
exit;
}
if($ftype!='xls'){
echo "Import file type is error";
exit;
}
}else{
echo "The file is not empty!";
exit;
}
}
require("./conn.php"); //连接7a64e4b893e5b19e330mysql数据库

//调用phpexcel类库
require_once 'PHPExcel.php';
require_once 'PHPExcel\IOFactory.php';
require_once 'PHPExcel\Reader\Excel5.php';

$objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
$objPHPExcel = $objReader->load($uploadfile);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
$arr_result=array();
$strs=array();

for($j=2;$j<=$highestRow;$j++)
{
unset($arr_result);
unset($strs);
for($k='A';$k<= $highestColumn;$k++)
{
//读取单元格
$arr_result .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().',';
}
$strs=explode(",",$arr_result);
$sql="insert into php_excel(gid,stu_no,name,age) values ($strs[0],'$strs[1]','$strs[2]',$strs[3])";
echo $sql."<br/>";
mysql_query("set names utf8");
$result=mysql_query($sql) or die("执行错误");

$insert_num=mysql_affected_rows();
if($insert_num>0){
$succ_result+=1;
}else{
$error_result+=1;
}

}
echo "插入成功".$succ_result."条数据!!!";
echo "插入失败".$error_result."条数据!!!";

其中conn.php代码如下:
[php] view plain copy print?
$mysql=mysql_connect("localhost","root","") or die("数据库连接失败!");
mysql_select_db("php_excel",$mysql);
mysql_query("set names utf8");

我的导入效果如下:
至此,从Excel文件读取数据批量导入到Mysql数据库完成。

string connString = server=localhost;uid=sa;pwd=sqlgis;database=master;
System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK){TransferData(fd.FileName, sheet1″, connString);}}public void TransferData(string excelFile, string sheetName, string connectionString){DataSet ds = new DataSet();try{//获取全部数据
string strConn = Provider=Microsoft.Jet.OLEDB.4.0; + Data Source= + excelFile + ; + Extended Properties=Excel 8.0;;
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = ;
OleDbDataAdapter myCommand = null;
strExcel = string.Format(select * from [{0}$], sheetName);
myCommand = new OleDbDataAdapter(strExcel, strConn);
myCommand.Fill(ds, sheetName);
//如果目标表不存在则创建
string strSql = string.Format(if object_id(’{0}’) is null create table {0}(, sheetName);
foreach (System.Data.DataColumn c in ds.Tables[0].Columns){strSql += string.Format([{0}] varchar(255),, c.ColumnName);}strSql = strSql.Trim(’,') + );
using (System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection(connectionString)){sqlconn.Open();
System.Data.SqlClient.SqlCommand command = sqlconn.CreateCommand();
command.CommandText = strSql;
command.ExecuteNonQuery();
sqlconn.Close();}//用bcp导入数据
using (System.Data.SqlClient.SqlBulkCopy bcp = new System.Data.SqlClient.SqlBulkCopy(connectionString)){bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcp_SqlRowsCopied);
bcp.BatchSize = 100;//每次传输的行e799bee5baa6e79fa5e98193e59b9ee7ad94365
bcp.NotifyAfter = 100;//进度提示的行数
bcp.DestinationTableName = sheetName;//目标表

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网