标签:tag win show san step max sage void typeof
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="dgv"></param>
/// <param name="tempProgressBar"></param>
/// <param name="toolstrip"></param>
public void DataToExcel(DataGridView dgv, ProgressBar tempProgressBar, Label toolstrip)
{
if (dgv.Rows.Count == 0)
{
MessageBox.Show("无数据"); return;
}
//MessageBox.Show("开始生成要导出的数据", "导出提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
if (MessageBox.Show("是否开始生成要导出的数据?", "导出提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
this.progressBar1.Visible = true;
Excel.Application excel = new Excel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = false;
for (int i = 0; i < dgv.ColumnCount; i++)
excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;
tempProgressBar.Visible = true;
tempProgressBar.Minimum = 1;
tempProgressBar.Maximum = dgv.RowCount;
tempProgressBar.Step = 1;
toolstrip.Visible = true;
for (int i = 0; i < dgv.RowCount; 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();
}
}
toolstrip.Text = "|| 状态:正在生成第 " + i + "/" + dgv.RowCount + " 个";
tempProgressBar.Value = i + 1;
}
toolstrip.Text = "|| 状态:生成Excel成功!";
MessageBox.Show("生成成功,请保存。", "生成提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
excel.Visible = true;
}
}
Winform DataGridView控件数据导出EXcel
标签:tag win show san step max sage void typeof
原文地址:http://www.cnblogs.com/xifengyeluo/p/6410296.html