zoukankan      html  css  js  c++  java
  • 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();
                }
            }
    
        }
    
    
    }
  • 相关阅读:
    注册、登录、忘记密码实战
    python3错误:format() takes at most 2 arguments
    Charles手机抓包简要步骤
    VARCHAR2(N CHAR)与VARCHAR2(N)的区别
    关于VI一些常用的操作
    LINUX下 基于 Socket 的 UDP 和 TCP 编程具体实现
    VC++6.0实现文本格式的转换保存
    crt的sftp使用用于Windows与Linux之间的通讯
    pl/sql 导出脚本与使用
    在oracle10g下启动服务报 Permission denied错误解决方法
  • 原文地址:https://www.cnblogs.com/hfzsjz/p/3064231.html
Copyright © 2011-2022 走看看