天天看點

C# datagridview 轉換為 word 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.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);
        }
    }
}
           

繼續閱讀