乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 如何在winform中嵌入excel

如何在winform中嵌入excel

作者:乔山办公网日期:

返回目录:excel表格制作


winform带有webBrowser控件
它是调用系统的explorer, 相当于winform上的网页

你是要从excel中导入数据到winform吗?如果是这样,可以这样:引用office11.0组件后, Microsoft.Office.Interop.Excel.Application application; //这是一个客户端
Microsoft.Office.Interop.Excel.Workbooks workbooks; //所有工e68a84e799bee5baa6e79fa5e98193333作薄
Microsoft.Office.Interop.Excel.Worksheet worksheet;//工作表
Microsoft.Office.Interop.Excel.Workbook workbook; //所用到的工作表 void IsRunEX(){ OpenFileDialog openfilediaglog = new OpenFileDialog();
openfilediaglog.Filter = "xls文件|*.xls";
if (openfilediaglog.ShowDialog() == DialogResult.OK)
{
FieldName = openfilediaglog.FileName;
application = new Microsoft.Office.Interop.Excel.Application();
workbooks = application.Workbooks;
workbook = returnworkbook(FieldName, workbooks);
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1]; //选择第一个表

可以使用winform编程的方式实现,步骤如下:

  1. 读取这个excel文件然后生成Datable 。

  2. 在winform前台界面选择生成的Datable 。

  3. 然后填写以下代码:

    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.OleDb;

    namespace 读Excel文件
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            /// <summary>
            /// 选择文件按钮
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.textBox1.Text = this.openFileDialog1.FileName;
                }
            }
            /// <summary>
            /// 点击导e799bee5baa6e997aee7ad94e58685e5aeb9337出excel按钮
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button2_Click(object sender, EventArgs e)
            {
                string File = this.openFileDialog1.FileName;
                DataTable dt = ExcelUp(File);
                dataGridView1.AutoGenerateColumns = false;
                dataGridView1.DataSource = dt;

            }
            /// <summary>
            /// 读取指定excel表中的内容返回datatable
            /// </summary>
            /// <param name="fileName">文件地址</param>
            /// <returns>表中内容</returns>
            public DataTable ExcelUp(string fileName)
            {
                string filePath = fileName;//读取excel文件路径;

                DataTable dt = GetDataTable("Sheet1", filePath);

                return dt;

            }

            /// <summary>
            /// 读取excel指定页中的内容
            /// </summary>
            /// <param name="strSheetName">页名</param>
            /// <param name="strExcelFileName">excel路径</param>
            /// <returns></returns>
            protected DataTable GetDataTable(string strSheetName, string strExcelFileName)
            {
                //源的定义
                string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source={0};" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';", strExcelFileName);

                //Sql语句
                string strExcel = string.Format("select * from [{0}$]", strSheetName);

                //定义存放的数据表
                DataSet ds = new DataSet();

                //连接数据源
                OleDbConnection conn = new OleDbConnection(strConn);

                try
                {
                    conn.Open();
                    //适配到数据源
                    OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
                    adapter.Fill(ds, strSheetName);
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                    conn.Close();
                }
                return ds.Tables[strSheetName];
            }


        }
    }


你可以先用循环的方法来获得改datagrid中“性别”的值,接着自己定义个变量来存放这个“性别”的1或者0,然后根据这个值用条件来判断是显示“男性”还是女性,打个比方(有五列十行数据,比方第二列显示出“性别”):
Dim intRow As Integer
Dim intColumn As Integer
Dim Gender As Integer

For intRow = 0 To 9(DataGrid的行总和)

Gender = CInt(datagrid1.Item (intRow, 1).ToString )
If Gender = 0 Then
datagrid1.Item (intRow, 1)="男"
Else
datagrid1.Item (intRow, 1)="女"
Next
请采纳答案,支持我一下

相关阅读

  • 如何在winform中嵌入excel

  • 乔山办公网excel表格制作
  • winform带有webBrowser控件它是调用系统的explorer, 相当于winform上的网页你是要从excel中导入数据到winform吗?如果是这样,可以这样:引用office11.0组件后, Microsoft.Office.Interop.Excel.Application
关键词不能为空
极力推荐

聚合标签

ppt怎么做_excel表格制作_office365_word文档_365办公网