[ASP.NET] 如何实现将Excel导入数据库(SQLServer)

作者在 2007-04-04 01:25:00 发布以下内容
大体思路是:将Excel的数据提出放在数据集中,在过循环将主表数据插入,在通过循环将从表插入:
代码如下:


private void button1_Click(object sender, System.EventArgs e)
{
//选择文件
ofdSelectExcel.Filter = "Excel Files(*.xls)|*.xls";
ofdSelectExcel.RestoreDirectory = true;
if( ofdSelectExcel.ShowDialog() == DialogResult.OK )
{
if ( ofdSelectExcel.FileName.Trim().Length == 0)
{
MessageBox.Show(this,"Please select a Excel file first!");
return;
}
else
{
ImportExcelToSqlServer(ofdSelectExcel.FileName.Trim());
}

}
}

********************************************************
提取数据
public void ImportExcelToSqlServer(string fileName)
{
if (fileName == null)
{
throw new ArgumentNullException("filename string is null!");
}

if (fileName.Length == 0)
{
throw new ArgumentException("filename string is empty!");
}

string oleDBConnString = String.Empty;
oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
oleDBConnString = "Data Source=";
oleDBConnString = fileName;
oleDBConnString = ";Extended Properties=Excel 8.0;";


OleDbConnection oleDBConn = null;
OleDbDataAdapter oleAdMaster = null;
DataTable m_tableName=new DataTable();;
DataSet ds=new DataSet();
try
{
oleDBConn = new OleDbConnection(oleDBConnString);
oleDBConn.Open();
m_tableName=oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);

if (m_tableName != null && m_tableName.Rows.Count > 0)
{

m_tableName.TableName =m_tableName.Rows[0]["TABLE_NAME"].ToString();

}
string sqlMaster;
sqlMaster=" select * from [" m_tableName "]";
oleAdMaster=new OleDbDataAdapter(sqlMaster,oleDBConn);
oleAdMaster.Fill(ds,"m_tableName");

MailRebateManager manger=new MailRebateManager();
bool isSucess=manger.AddExcelGmailRebate(ds.Tables["m_tableName"],ApplicationVariable.HomeCompanyID);
if(isSucess)
{
MessageBox.Show("Manipulate Succs!");
}
else
{
MessageBox.Show("Manipulate Failed");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
SimpleLogger.Log(ex);
try
{

}
catch (OleDbException e)
{
SimpleLogger.Log(e);
MessageBox.Show("An exception of type " e.GetType() ");
}
}


}
*****************************************
将数据进行处理分别插入主表和从表
public bool AddExcelGmailRebate(DataTable tb,string homeCompanyID)
{
bool ret=false;
SqlConnection con=null;

DataTable table=new DataTable();
table=tb;

string sConn = PublicManager.GetDBConnectionString(homeCompanyID);
con=new SqlConnection();
con.ConnectionString=sConn;

SqlTransaction tran=null;
SqlCommand com=null;
SqlCommand comm=null;

try
{
con.Open();
tran = con.BeginTransaction();

if (table != null && table.Rows.Count > 0)
{

for(int i=1;i<table.Rows.Count;i )
{

string m_PromoCode=Convert.ToString(table.Rows[0]);
if(m_PromoCode=="")
{
m_PromoCode=Convert.ToString(table.Rows[i-1][0]);
}
if(m_PromoCode.Length>50 )
{
m_PromoCode=m_PromoCode.Substring(0,50);
}


string m_ItemDescription=Convert.ToString(table.DefaultView[1]);
if(m_ItemDescription.IndexOf("(")>0)
{
int num=m_ItemDescription.IndexOf("(");
m_ItemDescription=m_ItemDescription.Substring(0,num);
if(m_ItemDescription.Length>50)
{
m_ItemDescription=m_ItemDescription.Substring(0,50);
}

}
if(m_ItemDescription.Length>50)
{
m_ItemDescription=m_ItemDescription.Substring(0,50);
}


string begin=Convert.ToString(table.DefaultView[2]);
string m_BeginPromoPeriodDate;
string m_EndPromoPeriodEndDate;
if(begin=="")
{
continue;
}
else
{
string beginTime=begin.Substring(0,8);
beginTime=beginTime.Replace("/","-");
m_BeginPromoPeriodDate=beginTime;
string endTime=begin.Substring(begin.Length-8);
endTime=endTime.Replace("/","-");
m_EndProm

数据库类 | 阅读 3175 次
文章评论,共0条
游客请输入验证码