求求各位高手,帮我这个菜鸟解决一下这个问题。我对编程基本上一窍不通,所以请尽量详细,最好是有详细代码。
解决方案 »
- Crystal Reports 水晶报表折线图问题
- socket 通信远程主机强迫关闭了一个现有的连接。急!!!在线等。
- 操作数数据类型 text 对于 subtract 运算符无效。
- 求助sql语句
- 如何去掉窗体的标题栏
- vs2005中选择"将解决方案添加到源代码管理"时如何添加注释/备注/COMMENT?
- 相同功能的按钮 菜单 的事件代码如何做到不重写?
- 怎样从图片文件读取某块区域的图像?(对高手来说很简单~~~~~)
- C# winform 怎么操作网页上传文件(post,或者模拟均可)
- DefaultCellStyle.Format
- 请问怎么把long型或int型转换成Hex(16进制)
- C#中怎样返回三个参数?
代码如下: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() +");
}
}
}
{
//选择文件
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() +");
}
}
}