如何用oledb的方式远程读写excel2003文件数据?
有两台机器A和B,A上有以建好的excel2003文件,需要从B上读写A的excel2003文件,从网上抄的代码改后:
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;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private OleDbConnection connection = null;
private OleDbCommand cmd = null;
private OleDbDataAdapter dataAdapter = null;
private DataSet dataSet = null;
private string connStr = "provider=microsoft.jet.oledb.4.0;data source=\\\\192.168.1.2\\c:\\data\\a.xls;extended properties='Excel 8.0;HDR=yes;IMEX=2'";
private string selectStr = "select * from [Sheet1$]";
private string cmdStr = null;
private void button1_Click(object sender, EventArgs e)//读按钮
{
connection = new OleDbConnection(connStr);
connection.Open();
dataAdapter = new OleDbDataAdapter(selectStr, connection);
dataSet = new DataSet();
dataAdapter.Fill(dataSet);
this.dataGridView1.DataSource = dataSet.Tables[0];
connection.Close();
}
private void button2_Click(object sender, EventArgs e)//写按钮
{
connection = new OleDbConnection(connStr);
connection.Open();
cmdStr = "insert into [Sheet1$] values(" + textBox1.Text + "," + textBox2.Text + "," + textBox3.Text + ")";
cmd = new OleDbCommand(cmdStr, connection);
int row = c