C#操作Office杂谈
内容提示:
操作两个Access数据库
从Excel导入数据
从Word导入数据
一、 操作两个Access数据库
假设:在C盘根目录存在数据库A和数据库B。在A和B中都存在表table1。
1. 在B中查询A中的表
(1)无密码
在B中执行语句
select * from [c:/a.mdb;].table1
(2)有密码
在B中执行语句
select * from [c:/a.mdb;pwd=123;].table1
2. 将A中table1的数据插入B中的table1
(1) 无密码
在B中执行语句
insert into table1(id,name) select id,name from [c:/a.mdb].table1
在A中执行语句
insert into [c:/b.mdb;].table1(id,name) select id,name from table1
(2) 有密码
在B中执行语句
insert into table1(id,name) select id,name from [c:/a.mdb;pwd=123;].table1
在A中执行语句
insert into [c:/b.mdb;pwd=123;].table1(id,name) select id,name from table1
二、 从Excel导入数据
1、将Excel的sheet读到DataSet的DataTable中
FillDataSet()方法将Excel转换为DataSet返回。
方法在下面。
2、连接字符串的说明
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:/test.xls;Extended
Properties='Excel 8.0;HDR=Yes;IMEX=1;'"
“HDR=Yes;”指示第一行中包含列名,而不是数据;
“IMEX=1;”通知驱动程序始终将“互混”数据列作为文本读取(将数字当做文本读取)。
'Excel 8.0;HDR=Yes;IMEX=1;'必须要带引号。
3、其它说明
在HKEY_LOCAL_MACHINE/Software/Microsoft/Jet/4.0/Engines/Excel有一个TypeGuessRows值,
预设是8,表示会先读取前8行来决定该列的类型,
所以如果前8行的资料都是日期,到了第9列以后出现的数字资料都会变成日期,
所以如果要解决这个问题,只要把TypeGuessRows机码值改成0。
如excel中数据如下:
程序读出来结果如下:
修改注册表中TypeGuessRows值的命令(modify.reg文件)
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Jet/4.0/Engines/Excel]
"TypeGuessRows"=dword:00000000
执行该命令的批处理(init.bat)
reg import TypeGuessRows.reg
FillDataSet()方法
public static DataSet FillDataSet(string strFilePath)
{
if (!File.Exists(strFilePath))
{
throw new Exception("No Excel files!");
}
ArrayList TableList = new ArrayList();
TableList = GetExcelTables(strFilePath);
if (TableList.Count <= 0)
{
return null;
}
DataTable table;
DataSet ds = new DataSet();
OleDbConnection dbcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFilePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1;'");
try
{
if (dbcon.State == ConnectionState.Closed)
{
dbcon.Open();
}
for (int i = 0; i < TableList.Count; i++)
{
string dtname = TableList[i].ToString();
try
{
OleDbCommand cmd = new OleDbCommand("select * from [" + dtname + "$]", dbcon);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
table = new DataTable(dtname);
adapter.Fill(table);
ds.Tables.Add(table);
}
catch (Exception ee)
{
}
}
}
finally
{
if (dbcon.State == ConnectionState.Open)
{
dbcon.Close();
}
}
return ds;
}
三、 从Word导入数据
读取word中的表格
try
{
object fileName = @"C:/Documents and Settings/wang/Desktop /数据样式.doc";
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
Word.ApplicationClass oWordApp = new Word.ApplicationClass(); //开启应用程序WINWORD.EXE
//打开要操作的Word文档
Word.Document oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);
oWordDoc.Activate();
//int i = oWordDoc.Tables.Count; //文档中表格的数量
Word.Table nowTable = oWordDoc.Tables[1]; //文档中第一个表格
//从table中读取数据
string strName = Utility.Trim(nowTable.Cell(1, 1).Range.Text.ToString());
//其它操作
//关闭文档
oWordDoc.Close(ref missing, ref missing, ref missing); //关闭
oWordApp.Quit(ref missing, ref missing, ref missing); //关闭应用程序WINWORD.EXE
}
catch (Exception Ex)
{
}