欢迎光临小站,愿能为您提供帮助与启发,热爱分享、享受分享、乐于分享,让我们携手共同进步。
WinForm开发系列文章、技巧http://www.cnblogs.com/peterzb/tag/WinForm/
这是我们群主写的一个把datagridview的数据导出到excel表中的一个方法:
1、先导入com组件的引用:Microsoft Excel 11.0 Object Library
2、/// <summary>
/// 将DataGridView控件中数据导出到Excel
/// </summary>
/// <param name="gridView">DataGridView对象</param>
/// <param name="isShowExcle">是否显示Excel界面</param>
/// <returns></returns>
public bool ExportDataGridview(DataGridView gridView, bool isShowExcle)
{
if (gridView.Rows.Count == 0)
return false;
//建立Excel对象,导入引用Microsoft Excel 11.0 Object Library
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isShowExcle;
//生成字段名称
for (int i = 0; i < gridView.ColumnCount; i++)
{
excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
}
//填充数据
for (int i = 0; i < gridView.RowCount - 1; i++)
{
for (int j = 0; j < gridView.ColumnCount; j++)
{
if (gridView[j, i].ValueType == typeof(string))
{
excel.Cells[i + 2, j + 1] = "'" + gridView[j, i].Value.ToString();
}
else
{
excel.Cells[i + 2, j + 1] = gridView[j, i].Value.ToString();
}
}
}
return true;
}
3、在datagridview添加下拉列表框的实现,在当前编辑单元格中显示下拉列表框的方法,不是时刻都显示下拉老板框
#region 在datagridview加入下拉列表框
private ComboBox cmb = new ComboBox();//新建下拉框
/// <summary>
/// 下拉框绑定性别
/// </summary>
private void BindSex()
{
DataTable dt = new DataTable();
dt.Columns.Add("value");
dt.Columns.Add("name");
DataRow dr ;
dr = dt.NewRow();
dr[0] = "0";
dr[1] = "男";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "1";
dr[1] = "女";
dt.Rows.Add(dr);
cmb.ValueMember = "value";
cmb.DisplayMember = "name";
cmb.DataSource = dt;
cmb.DropDownStyle = ComboBoxStyle.DropDownList;
}
/// <summary>
/// 绑定数据给datagridview
/// </summary>
private void BindDate()
{
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("name");
dt.Columns.Add("sex");
DataRow dr;
dr = dt.NewRow();
dr[0] = "0";
dr[1] = "li si";
dr[2] = "1";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr[0] = "1";
dr[1] = "li hui";
dr[2] = "0";
dt.Rows.Add(dr);
dgv.DataSource = dt;
}
//当移动到性别列时显示下拉框
private void dgv_CurrentCellChanged(object sender, EventArgs e)
{
try
{
if (dgv.CurrentCell.ColumnIndex == 2)
{
Rectangle rt = dgv.GetCellDisplayRectangle(dgv.CurrentCell.ColumnIndex, dgv.CurrentCell.RowIndex, false);
string st = dgv.CurrentCell.Value.ToString();
if (st == "0")
cmb.Text = "男";
else
cmb.Text = "女";
cmb.Top = rt.Top;
cmb.Left = rt.Left;
cmb.Width = rt.Width;
cmb.Height = rt.Height;
cmb.Visible = true;
}
else
cmb.Visible = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//选择下拉框时改变表格的内容
private void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
if (((ComboBox)sender).Text == "男")
{
dgv.CurrentCell.Value = "男";
dgv.CurrentCell.Tag = "0";
}
else
{
dgv.CurrentCell.Value = "女";
dgv.CurrentCell.Tag = "1";
}
}
//滚动的时候下拉框不可见
private void dgv_Scroll(object sender, ScrollEventArgs e)
{
this.cmb.Visible = false;
}
//改变列宽时下拉框不可见
private void dgv_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
{
this.cmb.Visible = false;
}
//绑定数据表后将性别列中的每一单元格的Value和Tag属性(Tag为值文本,Value为显示文本)
private void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
for (int i = 0; i < dgv.Rows.Count; i++)
{
if (dgv.Rows[i].Cells[2].Value != null && dgv.Rows[i].Cells[2].ColumnIndex == 2)
{
dgv.Rows[i].Cells[2].Tag = dgv.Rows[i].Cells[2].Value.ToString();
if (dgv.Rows[i].Cells[2].Value.ToString() == "0")
dgv.Rows[i].Cells[2].Value = "男";
else
if(dgv.Rows [i].Cells [2].Value .ToString ()=="1")
dgv.Rows[i].Cells[2].Value = "女";
}
}
}
#endregion
private void Form1_Load(object sender, EventArgs e)
{
BindSex();
BindDate();
cmb.Visible = false;
//添加事件
cmb.SelectedIndexChanged += new EventHandler(cmb_SelectedIndexChanged);
//将下拉框添加到列表框中
this.dgv.Controls.Add(cmb);
}