signed

QiShunwang

“诚信为本、客户至上”

【WPF】将DataGrid内容导出到Excel

2021/1/28 16:53:52   来源:

引言

在做项目时要求将datagrid的内容导出到Excel,以前做winform项目时遇到过,就把代码搬过来用,但wpf和winform还是有些不同,就修改了一些东西,使其能实现这个功能。

本文是导出2个datagrid的内容,并将其拼接。第一部分是鼠标选择的datagrid的那一行,第二部分是第2个datagrid的所有内容。具体情况可参考代码修改。

 

代码

//导出Excel(左卫狐)
        private void OutPutExcelButton_Click(object sender, EventArgs e)
        {

            SaveFileDialog saveFileDialog = new SaveFileDialog();
            DateTime dt = DateTime.Now;
            string sj = dt.ToString("yyyyMMddHHmmss");
            saveFileDialog.Filter = "Execl表格文件 (*.xls)|*.xls";
            saveFileDialog.FilterIndex = 0;
            saveFileDialog.RestoreDirectory = true;
            saveFileDialog.CreatePrompt = true;
            saveFileDialog.Title = "导出Excel文件到";
            saveFileDialog.FileName = this.Title + sj;
            saveFileDialog.ShowDialog();
            Stream myStream;
            try
            {
                myStream = saveFileDialog.OpenFile();
            }
            catch (IndexOutOfRangeException)
            {
                return;
            }
            //StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0));
            string str1 = "";
            string str2 = "";
            try
            {
                //写标题
                for (int i = 0; i < this.dgvMain.Columns.Count; i++)
                {
                    if (i > 0)
                    {
                        str1 += "\t";
                    }
                    str1 += dgvMain.Columns[i].Header.ToString();
                }
                sw.WriteLine(str1);

                DataRowView mySelectedElement = (DataRowView)dgvMain.SelectedItem;  //选择的那一行  
                string tempStr1 = "";
                //写内容
                for (int j = 0; j < dgvMain.Items.Count; j++)
                {
                    
                    if (j > 0)
                    {
                        tempStr1 += "\t";
                    }
                    tempStr1 += (mySelectedElement.Row[j].ToString());
                }
                sw.WriteLine(tempStr1);
                //写标题               
                for (int i = 0; i < this.dgvDetail.Columns.Count; i++)
                {
                    if (i > 0)
                    {
                        str2 += "\t";
                    }
                    str2 += dgvDetail.Columns[i].Header.ToString();

                }
                sw.WriteLine(str2);
                //写内容
                string tempStr2 = "";
                for (int j = 0; j < dgvDetail.Items.Count; j++)
                {
                    DataRowView mySelectedElements = (DataRowView)dgvDetail.Items[j];
                    for (int k = 0; k < dgvDetail.Columns.Count; k++)
                    {
                        if (k > 0)
                        {
                            tempStr2 += "\t";
                        }
                        tempStr2 += mySelectedElements.Row[k].ToString();          
                    }
                    if (j < dgvDetail.Items.Count)
                    {
                        tempStr2 += "\r\n";
                    }
                }
                sw.WriteLine(tempStr2);
    
                sw.Close();
                myStream.Close();
                MessageBox.Show("导出成功");
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.ToString());
            }

            finally
            {
                sw.Close();
                myStream.Close();
            }
        }

 

 

体会

没有解决不了的问题。在解决问题的过程中得按照顺序一步步去分析,不要这里试一下,那里试一下。