• WinForm c# GridView导出Excel


    WinForm GridView导出Excel
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.IO;
    
    namespace gridview导出excel
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            SqlConnection con = new SqlConnection("server=.;database=TestDB;uid=sa;pwd=");
            private void btnSelect_Click(object sender, EventArgs e)
            {
                string Sql = "SELECT UserName,UserAge,UserPhone,UserAddress From tb_userinfo";
                con.Open();
                SqlDataAdapter da;
                da = new SqlDataAdapter(Sql, con);
                DataSet ds = new DataSet();
                da.Fill(ds, "tb_userinfo");
                this.dataGridView1.DataSource = ds.Tables["tb_userinfo"].DefaultView;
    
    
                con.Close();
            }
    
            private void btnOut_Click(object sender, EventArgs e)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Execl   files   (*.xls)|*.xls";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = 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 = "";
                try
                {
                    //写标题     
                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        if (i > 0)
                        {
                            str += "\t";
                        }
                        str += dataGridView1.Columns[i].HeaderText;
                    }
    
                    sw.WriteLine(str);
                    //写内容   
                    for (int j = 0; j < dataGridView1.Rows.Count; j++)
                    {
                        string tempStr = "";
                        for (int k = 0; k < dataGridView1.Columns.Count; k++)
                        {
                            if (k > 0)
                            {
                                tempStr += "\t";
                            }
                            tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();
                        }
                        sw.WriteLine(tempStr);
                    }
                    sw.Close();
                    myStream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    sw.Close();
                    myStream.Close();
                }
            }
    
        }
    
    
    }
  • 相关阅读:
    JavaScrip中构造函数、prototype原型对象、实例对象三者之间的关系
    (字符缓冲流)文本排序案例
    Annotation注解的应用(打印异常信息)
    Annotation(注解)
    Java关键技术强化
    基本数据类型与引用数据类型的区别
    EKT反射
    bootstrap的概念
    Servlet强化
    java数据库连接池
  • 原文地址:https://www.cnblogs.com/hfzsjz/p/3064231.html
Copyright © 2020-2023  润新知