3个回答
展开全部
我这个是导入到DataGridView希望对你有用。 private void btImport_Click(object sender, EventArgs e)
{
try
{
lbmation.Visible = true;
dgvWayBill.Columns["savestatus"].Visible = false;
string strFileNamePath = string.Empty;
OpenFileDialog ofdShowDialog = new OpenFileDialog();
ofdShowDialog.FileName = "";
ofdShowDialog.Title = "请选择要导入的Excel文件,导入EXCEL文件的工作表名必须是Sheet1";
ofdShowDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
ofdShowDialog.Filter = "Excel文件(*.xls)|*.xls";
ofdShowDialog.CheckFileExists = true;
ofdShowDialog.CheckPathExists = true;
if (ofdShowDialog.ShowDialog() == DialogResult.OK)
strFileNamePath = ofdShowDialog.FileName;
if (string.IsNullOrEmpty(strFileNamePath))
{
MessageBox.Show("请选择要导入的文件", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
string strFileName = "Sheet1";
OleDbDataAccess odaData = new OleDbDataAccess(strFileNamePath);
DataSet dsImport = new DataSet();
dsImport = odaData.ReadExcelData(strFileName);
if (DataSetUtil.IsNull(dsImport) || DataTableUtil.IsNull(dsImport.Tables[0])) return;
DataBind(dsImport);
MessageBox.Show("导入成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
lbmation.Visible = false;
}
catch (Exception ex)
{
MessageBox.Show("导入失败,失败原因:" + ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//导入数据放到DataDridView中
private void DataBind(DataSet dsImport)
{
//这里你想将数据放到哪个控件里去就直接将数据源设置为dsImport.table[0]就可以了。
}
{
try
{
lbmation.Visible = true;
dgvWayBill.Columns["savestatus"].Visible = false;
string strFileNamePath = string.Empty;
OpenFileDialog ofdShowDialog = new OpenFileDialog();
ofdShowDialog.FileName = "";
ofdShowDialog.Title = "请选择要导入的Excel文件,导入EXCEL文件的工作表名必须是Sheet1";
ofdShowDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
ofdShowDialog.Filter = "Excel文件(*.xls)|*.xls";
ofdShowDialog.CheckFileExists = true;
ofdShowDialog.CheckPathExists = true;
if (ofdShowDialog.ShowDialog() == DialogResult.OK)
strFileNamePath = ofdShowDialog.FileName;
if (string.IsNullOrEmpty(strFileNamePath))
{
MessageBox.Show("请选择要导入的文件", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
string strFileName = "Sheet1";
OleDbDataAccess odaData = new OleDbDataAccess(strFileNamePath);
DataSet dsImport = new DataSet();
dsImport = odaData.ReadExcelData(strFileName);
if (DataSetUtil.IsNull(dsImport) || DataTableUtil.IsNull(dsImport.Tables[0])) return;
DataBind(dsImport);
MessageBox.Show("导入成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
lbmation.Visible = false;
}
catch (Exception ex)
{
MessageBox.Show("导入失败,失败原因:" + ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
//导入数据放到DataDridView中
private void DataBind(DataSet dsImport)
{
//这里你想将数据放到哪个控件里去就直接将数据源设置为dsImport.table[0]就可以了。
}
展开全部
/// <summary>
/// 导入Excel到DataSet
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private DataSet InExecl(string filePath)
{
DataSet ds = new DataSet();
string connStr = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
OleDbConnection myConn = new OleDbConnection(connStr);
string strSQL = " SELECT * FROM [Sheet1$]";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strSQL, myConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "[Sheet1$]");
myConn.Close();
return myDataSet;
}
/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnIn_Click(object sender, EventArgs e)
{
DataSet ds = InExecl(this.File1.PostedFile.FileName);
this.GridView2.DataSource = ds;
this.GridView2.DataBind();
}
/// 导入Excel到DataSet
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
private DataSet InExecl(string filePath)
{
DataSet ds = new DataSet();
string connStr = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
OleDbConnection myConn = new OleDbConnection(connStr);
string strSQL = " SELECT * FROM [Sheet1$]";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strSQL, myConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "[Sheet1$]");
myConn.Close();
return myDataSet;
}
/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnIn_Click(object sender, EventArgs e)
{
DataSet ds = InExecl(this.File1.PostedFile.FileName);
this.GridView2.DataSource = ds;
this.GridView2.DataBind();
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1.连接excel
2.解析Excel中的数据(返回的是一个dataset)
3.对数据进行整理,然后赋给数组
2.解析Excel中的数据(返回的是一个dataset)
3.对数据进行整理,然后赋给数组
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询