CSDN博客

img nanqing

winform中轉Excel檔

发表于2008/10/1 11:54:00  384人阅读

  #region 轉Excel檔
        private void button5_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass MyExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
            MyExcel.Visible = true;
            if (MyExcel == null)
            {
                MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            int rowcount = 0;
            int columncount = 0;
            MyExcel.Application.Workbooks.Add(true);
            columncount = this.dataGridView1.ColumnCount;
            rowcount = this.dataGridView1.RowCount;
            for (int m = 1; m < columncount; m++)
            {
                MyExcel.Cells[1, m] = this.dataGridView1.Columns[m - 1].HeaderText;//去除dataGridView1的编号列
            }
            for (int i = 0; i < rowcount; i++)
            {
                for (int j = 1; j < columncount; j++)
                {
                    MyExcel.Cells[i + 2, j] = Convert.ToString(this.dataGridView1[j - 1, i].Value);
                }
            }
        }
        #endregion
0 0

相关博文

我的热门文章

img
取 消
img