zoukankan      html  css  js  c++  java
  • Winform DataGridView控件数据导出EXcel

    /// <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;
    }
    }

  • 相关阅读:
    CTO成长之路分享会
    MySQL 数据库 varchar 到底可以存多少个汉字,多少个英文呢?我们来搞搞清楚
    《共享经济》创业沙龙
    旅游代购
    沙漏哟的书单
    qt5---滑动条QSlider
    qt5---步长调节器SpinBox和QDoubleSpinBox
    qt5--自定义控件封装
    qt5--表格控件QTableWidget
    qt5--树形控件QTreeWidget
  • 原文地址:https://www.cnblogs.com/xifengyeluo/p/6410296.html
Copyright © 2011-2022 走看看