.net WinForm窗体 数据导出

代码:

SqlConnection con = new SqlConnection("server=localhost;database=StaffingSystem;uid=sa;pwd=123");
        /// <summary>
        ///导入execl 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pictureBox1_Click(object sender, EventArgs e)
        {     
            //一个选择目录的控件
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            //设置文件类型
            saveFileDialog.Filter = "Execl   files   (*.xls)|*.xls";//设置Execl 文件存储文件后缀名
            saveFileDialog.FilterIndex = 0;//初始
            saveFileDialog.RestoreDirectory = true;//是否打开文件 //默认是false 所以要改为true
            saveFileDialog.CreatePrompt = true;//是否创建一个文件夹
            saveFileDialog.Title = "导出Excel 文件到";     
             
            //自动补充保存的文件名,不手动输入的话,自动生成
            DateTime now = DateTime.Now;                            
            saveFileDialog.FileName = now.Year.ToString().PadLeft(2)
            + now.Month.ToString().PadLeft(2, '0')//月份
            + now.Day.ToString().PadLeft(2, '0') + "-"//天
            + now.Hour.ToString().PadLeft(2, '0')//小时
            + now.Minute.ToString().PadLeft(2, '0')//分钟
            + now.Second.ToString().PadLeft(2, '0');//秒

            saveFileDialog.ShowDialog();

            Stream myStream;
            myStream = saveFileDialog.OpenFile(); //打开保存的文件的操作权限以便写入 
            StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
           
             //放总列
            string str = "";

            //将datgetview放到表格去
            try
            {
                //得到列名称
                for (int i = 0; i < dgv_Statistics.ColumnCount; i++)
                {
                    if (i > 0)
                    {
                        str += "\t";
                    }
                    str += dgv_Statistics.Columns[i].HeaderText;
                }

                sw.WriteLine(str);

                //写内容   
                for (int j = 0; j < dgv_Statistics.Rows.Count; j++)//循环遍历出表格有多少行
                {
                    string tempStr = "";//定义一个string的temp 装列里面的对应的值
                    for (int k = 0; k < dgv_Statistics.Columns.Count; k++)//循环有多少列
                    {
                        if (k > 0)  //                                  
                        {
                            tempStr += "\t";            
                        }
                        tempStr += dgv_Statistics.Rows[j].Cells[k].Value.ToString();
                        //得到行和列找到准确的位置进行value的赋值
                    }
                    sw.WriteLine(tempStr); 
                    
                }
                //循环完以后关闭
                sw.Close();
                myStream.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());//异常的处理
            }
            finally
            {
                sw.Close();
                myStream.Close();
                //异常以后自动关闭
            }
        }

猜你喜欢

转载自blog.csdn.net/weixin_39856293/article/details/85111611