C#操作Office杂谈

C#操作Office杂谈

内容提示:

操作两个Access数据库

Excel导入数据

Word导入数据

 

一、        操作两个Access数据库

假设:在C盘根目录存在数据库A和数据库B。在AB中都存在表table1

1.        B中查询A中的表

1)无密码

B中执行语句

select * from [c:/a.mdb;].table1

2)有密码

B中执行语句

select * from [c:/a.mdb;pwd=123;].table1

 

2.        Atable1的数据插入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、将Excelsheet读到DataSetDataTable

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)

{

   

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值