在Excel中插入数据 [英] Inserting of data in Excel

查看:85
本文介绍了在Excel中插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数据插入到我创建的空白excel工作表中.为此,我使用了下面的代码---但是错误显示excel文件已经打开了,但没有打开.该怎么办...?

I want to insert data into blank excel sheet which I created..for that I used the below code---but the error is showing that the excel file is already open which is not.What to do...?

using System;
using System.Drawing;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel; 
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                System.Data.OleDb.OleDbConnection MyConnection ;
                System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
                string sql = null;
                MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source=''c:\\csharp.net-informations.xls'';Extended Properties=Excel 8.0;");
                MyConnection.Open();
                myCommand.Connection = MyConnection;
                sql = "Insert into [Sheet1$] (id,name) values(''5'',''e'')";
                myCommand.CommandText = sql;
                myCommand.ExecuteNonQuery();
                MyConnection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show (ex.ToString());
            }
        }
   }
}

推荐答案

(id,name)values(''5'',''e''); myCommand.CommandText = sql; myCommand.ExecuteNonQuery(); MyConnection.Close(); } 抓住(前例外) { MessageBox.Show(ex.ToString()); } } } }
(id,name) values(''5'',''e'')"; myCommand.CommandText = sql; myCommand.ExecuteNonQuery(); MyConnection.Close(); } catch (Exception ex) { MessageBox.Show (ex.ToString()); } } } }


如果您在浏览器中打开了Excel工作表,请关闭它.
确保处理此错误消息,以使用户意识到可以在其计算机上打开excel工作表.
Close the excel sheet if you have it open in explorer.
Make sure you handle this error message so that users are made aware that the excel sheet may be open on their machines.


这篇关于在Excel中插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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