如何用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 = cmd.ExecuteNonQuery();
if (row > 0)
{
MessageBox.Show("保存成功");
}
else
{
MessageBox.Show("保存失败");
}
connection.Close();
}
}
}
运行时总是提示“无法更新,数据库或对象为只读”。
目前,使用B机器通过网络邻居已经可以浏览到A机器中的excel2003文件,也能够打开并修改成功。
A机器中的excel2003文件路径中的所有文件夹属性均已去掉只读属性,访问用户everyone已设成完全访问权限。
请高手帮我看看错在哪里,万分感谢! C# 远程读写excel2003
------解决方案--------------------
共享文件的权限不够额,还有就是现在远程打开的都是只读文件,只有点击编辑之后才可以去修改啊。
[解决办法]
楼主是否可以换个访求,先从另一台机下载文件文件到本地,再上传或copy 到另外一台机
[解决办法]
可能是你的excel所在的盘为ntfs格式 而这个格式中要求相应的帐户具有修改的权限 这样access才能够进行更新
[解决办法]
你手动打开B的EXCEL文件,能不能修改和保存,如果能,那程序里也应该能,如果不能,还是权限问题,如果A机是服务器,除了共享权限外,还要看目录及文件访问权限。