如何将图像从excel文件导入数据网格视图。 [英] how can import images from excel file into data grid view.

查看:114
本文介绍了如何将图像从excel文件导入数据网格视图。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我有一个excel文件。这个文件内容一些数据和图像,现在我创建一个窗口应用程序,这个应用程序在窗口的窗体数据网格视图中捕获所有记录但图像不导入,请告诉我如何从excel文件导入数据网格视图中的图像?下面是我的编码:

 命名空间 ExceltoWindow 
{
public partial class Form1:Form
{
private string Excel03ConString = Provider = Microsoft.Jet.OLEDB.4.0; Data Source = {0}; Extended Properties ='Excel 8.0; HDR = {1}';
private string Excel07ConString = Provider = Microsoft.ACE.OLEDB.12.0; Data Source = {0}; Extended Properties ='Excel 8.0; HDR = {1}';
public Form1()
{
InitializeComponent();
}
私有 void Form1_Load( object sender,EventArgs e)
{
}
private void button1_Click( object sender,EventArgs e)
{
openFileDialog1.ShowDialog();
}
private void openFileDialog1_FileOk( object sender,CancelEventArgs e)
{
string filePath = openFileDialog1.FileName;
string extension = Path.GetExtension(filePath);
string header = rbHeaderYes.Checked? YES NO;
string conStr,sheetName;
conStr = string .Empty;
switch (扩展名)
{
案例 。xls // Excel 97-03
conStr = string .Format(Excel03ConString,filePath,header);
break ;
case 。xlsx // Excel 07
conStr = string .Format(Excel07ConString,filePath,header);
break ;
}
// 获取第一张表的名称。
使用(OleDbConnection con = new OleDbConnection(conStr))
{
< span class =code-keyword> using
(OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection =精读;
con.Open();
DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null );
sheetName = dtExcelSchema.Rows [ 0 ] [ TABLE_NAME]的ToString();
con.Close();
}
}
// 从第一张表中读取数据。
使用(OleDbConnection con = new OleDbConnection(conStr))
{
使用(OleDbCommand cmd = new OleDbCommand())
{
使用(OleDbDataAdapter oda = new OleDbDataAdapter())
{
DataTable dt = new DataTable();
cmd.CommandText = SELECT * From [ + sheetName + ];
cmd.Connection = con;
con.Open();
oda.SelectCommand = cmd;
oda.Fill(dt);
con.Close();
// 填充DataGridView。
dataGridView1.DataSource = dt;
}
}
}
}
}
}

解决方案

您无法使用OLE DB从Excel文件中获取图像。获得它们的一种方法是使用Excel Interop。



看看 Microsoft.Office.Interop.Excel命名空间 [ ^ ]



和一些样本如何:使用COM Interop创建Excel电子表格(C#编程指南) [ ^ ]

hello
i have a excel file. this file content some data and image, now i create a window application and this application fatch all the record in window's form data grid view but image are not imported, please tell me how can i also import image in data grid view from excel file?below is my coding:

namespace ExceltoWindow
{
    public partial class Form1 : Form
    {
        private string Excel03ConString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";
        private string Excel07ConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }
        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            string filePath = openFileDialog1.FileName;
            string extension = Path.GetExtension(filePath);
            string header = rbHeaderYes.Checked ? "YES" : "NO";
            string conStr, sheetName;
            conStr = string.Empty;
            switch (extension)
            {
                case ".xls": //Excel 97-03
                    conStr = string.Format(Excel03ConString, filePath, header);
                    break;
                case ".xlsx": //Excel 07
                    conStr = string.Format(Excel07ConString, filePath, header);
                    break;
            }
            //Get the name of the First Sheet.
            using (OleDbConnection con = new OleDbConnection(conStr))
            {
                using (OleDbCommand cmd = new OleDbCommand())
                {
                    cmd.Connection = con;
                    con.Open();
                    DataTable dtExcelSchema = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    sheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
                    con.Close();
                }
            }
            //Read Data from the First Sheet.
            using (OleDbConnection con = new OleDbConnection(conStr))
            {
                using (OleDbCommand cmd = new OleDbCommand())
                {
                    using (OleDbDataAdapter oda = new OleDbDataAdapter())
                    {
                        DataTable dt = new DataTable();
                        cmd.CommandText = "SELECT * From [" + sheetName + "]";
                        cmd.Connection = con;
                        con.Open();
                        oda.SelectCommand = cmd;
                        oda.Fill(dt);
                        con.Close();
                        //Populate DataGridView.
                        dataGridView1.DataSource = dt;
                    }
                }
            }
        }
    }
}

解决方案

You can't fetch images from an Excel file using OLE DB. One way to get them could be using Excel Interop.

Have a look at Microsoft.Office.Interop.Excel namespace[^]

and some sample How to: Use COM Interop to Create an Excel Spreadsheet (C# Programming Guide)[^]


这篇关于如何将图像从excel文件导入数据网格视图。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆