c#中怎样实现文件的导入的!多提示下。新手 这么回事。单击“导入按钮”导入excel文件到dataview里面去,execel上面的内容和dataview里面的字段对应。各位有做过这个功能的么。介绍下思路或者写下代码都可以的。网上找了半天但是没有看懂。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 解析Excel,根据OleDbConnection直接连Excel /// </summary> /// <param name="filePath"></param> /// <param name="name"></param> /// <returns></returns> public static DataSet LoadDataFromExcel(string filePath, string name) { try { string strConn; // strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filePath + ";Extended Properties=Excel 8.0"; strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=No\""; OleDbConnection OleConn = new OleDbConnection(strConn); OleConn.Open(); string sql = "SELECT * FROM [" + name + "$]";//可是更改Sheet名称,比如sheet2,等等 OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn); DataSet OleDsExcle = new DataSet(); OleDaExcel.Fill(OleDsExcle, name); OleConn.Close(); return OleDsExcle; } catch (Exception err) { MessageBox.Show("数据绑定Excel失败! 失败原因:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return null; } } dataview.DataSource=LoadDataFromExcel(path,name);dataview设置相应的字段 private void button1_Click(object sender, EventArgs e) { //调用自定义函数ExportDataGridview ExportDataGridview(dataGridView1, true); } public bool ExportDataGridview(DataGridView dgv, bool isShowExcle) { if (dgv.Rows.Count == 0) return false; //建立Excel对象 Excel.Application excel = new Excel.Application(); excel.Application.Workbooks.Add(true); excel.Visible = isShowExcle; //生成字段名称 for (int i = 0; i < dgv.ColumnCount; i++) { excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText; } //填充数据 for (int i = 0; i < dgv.RowCount - 1; i++) { for (int j = 0; j < dgv.ColumnCount; j++) { if (dgv[j, i].ValueType == typeof(string)) { excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString(); } else { excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString(); } } } return true; } 谢谢happy09li和yongleshangpu我现在还在摸索 不过应该很快可以弄出来了!谢谢了! .net串行端口通信问题,着急 点击更改不能传值和错误 如何将字符串 转换成ascii码等 你们有没有碰到这样一个奇怪的问题 DataGrid中显示xml中数据 请教大家 论坛群发器的原理 以及实现方式 请教!有空的朋友帮忙指点一下,谢谢!关于update的问题 想学C#,不知道能不能学好 winform中背景图片的问题 在线等 VS2003IDE环境里,属性窗口里居然找不到gotfocus和lostfocus两个事件,这是咋个道理咛 IIS网站响应超时 有没有比较有效的办法解决啊 如何在form窗体中画矩形????
/// <summary>
/// 解析Excel,根据OleDbConnection直接连Excel
/// </summary>
/// <param name="filePath"></param>
/// <param name="name"></param>
/// <returns></returns>
public static DataSet LoadDataFromExcel(string filePath, string name)
{
try
{
string strConn;
// strConn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = " + filePath + ";Extended Properties=Excel 8.0";
strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=No\"";
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
string sql = "SELECT * FROM [" + name + "$]";//可是更改Sheet名称,比如sheet2,等等
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
DataSet OleDsExcle = new DataSet();
OleDaExcel.Fill(OleDsExcle, name);
OleConn.Close();
return OleDsExcle;
}
catch (Exception err)
{
MessageBox.Show("数据绑定Excel失败! 失败原因:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
return null;
}
}
dataview.DataSource=LoadDataFromExcel(path,name);
dataview设置相应的字段
private void button1_Click(object sender, EventArgs e)
{
//调用自定义函数ExportDataGridview
ExportDataGridview(dataGridView1, true);
}
public bool ExportDataGridview(DataGridView dgv, bool isShowExcle)
{
if (dgv.Rows.Count == 0)
return false;
//建立Excel对象
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
//生成字段名称
for (int i = 0; i < dgv.ColumnCount; i++)
{
excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < dgv.RowCount - 1; i++)
{
for (int j = 0; j < dgv.ColumnCount; j++)
{
if (dgv[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();
}
}
}
return true;
}