Eg:
private void ImportNum_Click(object sender, EventArgs e)
{
System.Windows.Forms.OpenFileDialog fd = new OpenFileDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
try
{
TransferNumData(fd.FileName, "saved", connStr);
string filepath = fd.FileName;
int index = fd.FileName.LastIndexOf("\\");
string filename = fd.FileName.Substring(index + 1);
int fileIndex = filename.LastIndexOf(".");
SaveNum_to_sql();
}
catch (Exception ex)
{
MessageBox.Show("报错!" + ex.Message.ToString());
}
}
}
public void TransferNumData(string excelFile, string sheetName, string connectionString)
{
DataSet ds = new DataSet();
try
{
//获取全部数据
string strConn = "Provider =Microsoft.ACE.OleDb.12.0;" + "Data Source=" + excelFile + ";" + "Extended Properties = 'Excel 8.0;HDR=YES;IMEX=1;'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = string.Format("select * from [{0}$]", sheetName);
myCommand = new OleDbDataAdapter(strExcel, strConn);
DataColumn col = new DataColumn();
col.ColumnName = "";
//ds.Tables.Add("");
myCommand.Fill(ds, sheetName);
//如果目标表不存在则创建
string strSql = string.Format("if object_id('{0}') is null create table {0}(", "tempTelInfo");
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(NumSqlRowsCopied);
bcp.BatchSize = 100;//每次传输的行数
bcp.NotifyAfter = 100;//进度提示的行数
bcp.DestinationTableName = "tempTelInfo";//目标表
//for (int i = 0; i <ds.Tables[0].Rows.Count; i++)
//{
// if (ds.Tables[0].Rows[i][0].ToString().Trim() == "")
// break;
//}
bcp.WriteToServer(ds.Tables[0]);
}
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
//进度显示
void NumSqlRowsCopied(object sender, System.Data.SqlClient.SqlRowsCopiedEventArgs e)
{
this.Text = e.RowsCopied.ToString();
this.Update();
}
1、导入数据,出现丢失数据问题,则将HDR改为YES,IMIE改为2
2、照上面修改完毕之后,数据不丢失了,但导入的每列数据超过11位,则导入进来的数据以科学技术法表示,故将IMIE改为1
3、同样数据丢失,所以将驱动换成了Microsoft.ACE.OleDb.12.0,然后就好了
4、string connstr = "Provider=Microsoft.ACE.OLEDB.12.0; Persist Security Info=False;Data Source=" + @Excfilename + "; Extended Properties=Excel 8.0;HDR=Yes;IMEX=2";
在这种情况下,有可能出现“找不到可安装的ISAM ”。——解决方法:Regsvr32 c:\winnt\system32\msexcl40.dll
附:
参数HDR的值:
HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不是标题,做为数据来使用。系统默认的是YES
参数Excel 8.0
对于Excel 97以上版本都用Excel 8.0
IMEX ( IMport EXport mode )设置
IMEX 有三种模式:
0 is Export mode
1 is Import mode
2 is Linked mode (full update capabilities)
我这里特别要说明的就是 IMEX 参数了,因为不同的模式代表著不同的读写行为:
当 IMEX=0 时为“汇出模式”,这个模式开启的 Excel 档案只能用来做“写入”用途。
当 IMEX=1 时为“汇入模式”,这个模式开启的 Excel 档案只能用来做“读取”用途。
当 IMEX=2 时为“连結模式”,这个模式开启的 Excel 档案可同时支援“读取”与“写入”用途。
意义如下:
0 ---输出模式;
1---输入模式;
2----链接模式(完全更新能力)