C# datagridview 转换为 word excel

来源:互联网 发布:女神联盟坐骑升阶数据 编辑:程序博客网 时间:2024/05/22 17:02
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.OracleClient;using System.Data.SqlClient;using Word = Microsoft.Office.Interop.Word;using Excel = Microsoft.Office.Interop.Excel;namespace TEST{    public partial class main : Form    {        public main()        {            InitializeComponent();        }        public bool ExportDataGridviewToWord(DataGridView dgv, bool isShowWord)        {            Word.Document mydoc = new Word.Document();//实例化Word文档对象            Word.Table mytable;//声明Word表格            Word.Selection mysel;//声明Word选区            Object myobj;            if (dgv.Rows.Count == 0)                return false;            //建立Word对象            Word.Application word = new Word.Application();            myobj = System.Reflection.Missing.Value;            mydoc = word.Documents.Add(ref myobj, ref myobj, ref myobj, ref myobj);            word.Visible = isShowWord;            mydoc.Select();            mysel = word.Selection;            //将数据生成Word表格文件            mytable = mydoc.Tables.Add(mysel.Range, dgv.RowCount, dgv.ColumnCount, ref myobj, ref myobj);            //设置列宽            mytable.Columns.SetWidth(80, Word.WdRulerStyle.wdAdjustNone);            //输出列标题数据            for (int i = 0; i < dgv.ColumnCount; i++)            {                mytable.Cell(1, i + 1).Range.InsertAfter(dgv.Columns[i].HeaderText);            }            //输出控件中的记录            for (int i = 0; i < dgv.RowCount - 1; i++)            {                for (int j = 0; j < dgv.ColumnCount; j++)                {                    mytable.Cell(i + 2, j + 1).Range.InsertAfter(dgv[j, i].Value.ToString());                }            }            return true;        }        public bool ExportDataGridviewToExcel(DataGridView dgv, bool isShowExcle)        {            if (dgv.Rows.Count == 0)                return false;            //建立Excel对象            Excel.Application excel = new Excel.Application();            excel.Application.Workbooks.Add(true);            excel.Visible = isShowExcle;            //生成字段名称            for (int i = 0; i < dgv.ColumnCount; i++)            {                excel.Cells[1, i + 1] = dgv.Columns[i].HeaderText;            }            //填充数据            for (int i = 0; i < dgv.RowCount - 1; i++)            {                for (int j = 0; j < dgv.ColumnCount; j++)                {                    if (dgv[j, i].ValueType == typeof(string))                    {                        excel.Cells[i + 2, j + 1] = "'" + dgv[j, i].Value.ToString();                    }                    else                    {                        excel.Cells[i + 2, j + 1] = dgv[j, i].Value.ToString();                    }                }            }            return true;        }        private void main_Load(object sender, EventArgs e)        {        }        private void button1_Click(object sender, EventArgs e)        {            //string connstring = "Data Source=ouc131;user=ouc;password=letu;";//写连接串            string connstring = "User Id=ouc;Password=letu;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=222.195.151.131)(PORT=1523))(CONNECT_DATA=(SID=ouc)));Unicode=True;";            OracleConnection conn = new OracleConnection(connstring);            try            {                conn.Open();                OracleDataAdapter AdapterSelect = new OracleDataAdapter("select * from text", conn);                DataTable dt = new DataTable();                AdapterSelect.Fill(dt);                dataGridView1.DataSource = dt.DefaultView;                conn.Close();            }            catch (Exception ee)            {                MessageBox.Show(ee.Message);            }            finally            {                conn.Close();            }        }        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)        {            using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))            {                e.Graphics.DrawString((e.RowIndex+1).ToString(),                e.InheritedRowStyle.Font,                b,                e.RowBounds.Location.X,                e.RowBounds.Location.Y);            }         }        private void comboBox1_DropDown(object sender, EventArgs e)        {            comboBox1.Items.Clear();            string connstring = "User Id=ouc;Password=letu;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=222.195.151.131)(PORT=1523))(CONNECT_DATA=(SID=ouc)));Unicode=True;";            OracleConnection conn = new OracleConnection(connstring);            try            {                conn.Open();                OracleCommand cmd = conn.CreateCommand();                cmd.CommandText = "select table_name from user_tables";                OracleDataReader odr = cmd.ExecuteReader();                while (odr.Read())                {                    comboBox1.Items.Add(odr.GetOracleString(0).ToString());                                    }                odr.Close();                conn.Close();            }            catch (Exception ee)            {                MessageBox.Show(ee.Message);            }            finally            {                conn.Close();            }        }        private void button2_Click(object sender, EventArgs e)        {            ExportDataGridviewToWord(dataGridView1, true);        }        private void button3_Click(object sender, EventArgs e)        {            ExportDataGridviewToExcel(dataGridView1, true);        }    }}

原创粉丝点击