C#中如何实现EXCEL的导入和导出,请提供详细的代码,哪位高手知道的请提供一下!急!

来源:百度知道 编辑:UC知道 时间:2024/06/17 02:15:52

打开过excel文件,将内容绑定在dataGridView1上,代码如下:try
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.DefaultExt = "xls";
openfile.Filter = "Excel文件|*.xls";
if (openfile.ShowDialog() == DialogResult.OK)
{
string s;

s = openfile.FileName.ToString();
string strCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + s + ";Extended properties=Excel 8.0";
OleDbConnection myconn = new OleDbConnection(strCon);
string strcom = "select * from [Sheet1$]";
myconn.Open();
OleDbCommand comm = new OleDbCommand(strcom, myconn);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = comm;
myconn.Close();
DataSet ds1 = new DataSet();
da.Fill(ds1, "[Sheet1$]");
dataGridView1.DataSource = ds1.Tables[0];
}
}
catch (Exception e1) { MessageBox.Show(e1.Message); }

将DataSet数据集里的数据写入excel的代码:<