gridControl加载EXCEL数据的问题
gridControl可以导出EXCEL,我按照这个导出的EXCEL文件,再导进来该如何做?PS。gridControl中用的是bandedGridView,采用的是强类...
gridControl可以导出EXCEL,我按照这个导出的EXCEL文件,再导进来该如何做?
PS。 gridControl中用的是bandedGridView,采用的是强类型的DATASET读去数据库中的信息。
现在可以导出EXCEL文件了。可如何导入EXCEL文件呢??? 展开
PS。 gridControl中用的是bandedGridView,采用的是强类型的DATASET读去数据库中的信息。
现在可以导出EXCEL文件了。可如何导入EXCEL文件呢??? 展开
1个回答
展开全部
OpenFileDialog saveFileDialog1 = new OpenFileDialog();
saveFileDialog1.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog1.FilterIndex = 0;
saveFileDialog1.RestoreDirectory = true;
//saveFileDialog2.CreatePrompt = true;
// saveFileDialog1.Title = "导出文件保存路径";
saveFileDialog1.FileName = null;
saveFileDialog1.ShowDialog();
string Path = saveFileDialog1.FileName;
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
try
{
conn.Open();
string strExcel = "";
OleDbDataAdapter myda = null;
DataSet ds = null;
strExcel = "select * from [sheet1$]";
myda = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myda.Fill(ds);
//InsertSQLServer(ds.Tables[0]);
DataTable dt = ds.Tables[0];
//try
string strInsertComm;
for (int i = 0; i < dt.Rows.Count; i++)
{
strInsertComm = "";
strInsertComm = "Insert into guanliyuan";//数据库表的字段
strInsertComm += " values(";
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j > 0)
{
strInsertComm += ",'" + dt.Rows[i][j].ToString().Trim() + "'";
}
else
{
strInsertComm += "'" + dt.Rows[i][j].ToString().Trim() + "'";
}
}
strInsertComm += ")";
dbm.Command(strInsertComm);
MessageBox.Show("导入成功!");
}
}
catch (Exception ex)
{
if (ex.Message == "无效的参数量。")
{ }
else
{
MessageBox.Show(ex.Message);
conn.Close();
}
}
}
希望对你有帮助!
saveFileDialog1.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog1.FilterIndex = 0;
saveFileDialog1.RestoreDirectory = true;
//saveFileDialog2.CreatePrompt = true;
// saveFileDialog1.Title = "导出文件保存路径";
saveFileDialog1.FileName = null;
saveFileDialog1.ShowDialog();
string Path = saveFileDialog1.FileName;
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
try
{
conn.Open();
string strExcel = "";
OleDbDataAdapter myda = null;
DataSet ds = null;
strExcel = "select * from [sheet1$]";
myda = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myda.Fill(ds);
//InsertSQLServer(ds.Tables[0]);
DataTable dt = ds.Tables[0];
//try
string strInsertComm;
for (int i = 0; i < dt.Rows.Count; i++)
{
strInsertComm = "";
strInsertComm = "Insert into guanliyuan";//数据库表的字段
strInsertComm += " values(";
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j > 0)
{
strInsertComm += ",'" + dt.Rows[i][j].ToString().Trim() + "'";
}
else
{
strInsertComm += "'" + dt.Rows[i][j].ToString().Trim() + "'";
}
}
strInsertComm += ")";
dbm.Command(strInsertComm);
MessageBox.Show("导入成功!");
}
}
catch (Exception ex)
{
if (ex.Message == "无效的参数量。")
{ }
else
{
MessageBox.Show(ex.Message);
conn.Close();
}
}
}
希望对你有帮助!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询