2012/4/26——第一次项目小记(一)

本文介绍了如何使用C#语言通过Microsoft.Office.Interop.Word库来实现对Word文档的基本操作,包括打开、读取和写入等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    PS:写这篇文章的时候,我负责的项目模块基本上已经完工了,除了个别难点的地方还没写完。。。(留着扔给老温解决去吧~~!哇哈哈~!)总之。。。收获良多啊。。基本上已经能熟练的用C#编点小东西了。对于用惯了VC++6.0的小菜鸟VS2010真是能让人抹泪的东西。。。

   

   

//--需要解决的问题

*1.c#打开word文件,并进行简单的读取和写入操作

//--test1.cs要求解决该问题

//--解决时间 2012/4/18凌晨120

 

//********************************

//--项目名称:mytest

//--保存路径:C:\Users\Administrator\Desktop\项目库\work\text

//--作者:小狼

//--最后一次修改时间:2012/4/18

//********************************

 

Ps:第一次用windows form来做东西。。。蹒跚学步的东西也蛮好玩的。


1.1 添加引用

操作word 和操作excel的原理是差不多的

首先是在引用的类库中添加 Microsoft.Office.Interop.Word

   

1.2 添加命名空间

//--然后在代码中添加命名空间

using Word = Microsoft.Office.Interop.Word;


1.3 基本对象定义

using Word = Microsoft.Office.Interop.Word;


//--需要用的时候,定义

Word.Application m_objWord =new Word.Application();

//------则生成应用        

 

//--定义文件对象

 Word.Document oDocument1_key ;

//------------则生成文件对象


1.4 应用代码

 

1.4.1 文件打开

//--以下代码用于文件的打开


   public  void CreateWordDocument(string FileName, ref Word.Document wDoc, ref Word.Application WApp)
         {
             if (FileName == "") return;
             Word.Document thisDocument = null;
             Word.FormFields formFields = null;
             Word.Application thisApplication = new Word.ApplicationClass();
             thisApplication.Visible = true;
             thisApplication.Caption = "";
             thisApplication.Options.CheckSpellingAsYouType = false;
             thisApplication.Options.CheckGrammarAsYouType = false;

             Object filename = FileName;
             Object ConfirmConversions = false;
             Object ReadOnly = true;
             Object AddToRecentFiles = false;

             Object PasswordDocument = System.Type.Missing;
             Object PasswordTemplate = System.Type.Missing;
             Object Revert = System.Type.Missing;
             Object WritePasswordDocument = System.Type.Missing;
             Object WritePasswordTemplate = System.Type.Missing;
             Object Format = System.Type.Missing;
             Object Encoding = System.Type.Missing;
             Object Visible = System.Type.Missing;
             Object OpenAndRepair = System.Type.Missing;
             Object DocumentDirection = System.Type.Missing;
             Object NoEncodingDialog = System.Type.Missing;
             Object XMLTransform = System.Type.Missing;

             try
             {
                 Word.Document wordDoc =
                 thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
                 ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
                 ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
                 ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
                 ref NoEncodingDialog, ref XMLTransform);

                 thisDocument = wordDoc;
                 wDoc = wordDoc;
                 WApp = thisApplication;
                 formFields = wordDoc.FormFields;
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }

         } 







接入第三方登录是让用户方便快捷地使用已有账号登录你的网站或应用程序,提高用户体验的种方式。本文将介绍如何使用 PHP 实现微信公众号第三方登录。 1. 获取微信授权 首先,需要获取微信用户的授权。具体步骤如下: 1)引导用户打开微信授权页面: ```php $appid = 'your_appid'; $redirect_uri = urlencode('http://yourdomain.com/callback.php'); $scope = 'snsapi_userinfo'; $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=$scope&state=STATE#wechat_redirect"; header("Location: $url"); ``` 其中,`$appid` 是你的微信公众号的 AppID,`$redirect_uri` 是授权后回调的 URL,`$scope` 是授权作用域,可以是 `snsapi_base` 或 `snsapi_userinfo`,`$state` 是自定义参数,用于防止 CSRF 攻击。 2)获取授权码: 用户同意授权后,会重定向到 `$redirect_uri` 指定的 URL,带上授权码 `code` 和 `state` 参数。 ```php $code = $_GET['code']; $state = $_GET['state']; ``` 3)获取 access_token 和 openid: 使用授权码 `code` 获取 `access_token` 和 `openid`。 ```php $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code"; $response = file_get_contents($access_token_url); $result = json_decode($response, true); $access_token = $result['access_token']; $openid = $result['openid']; ``` 其中,`$secret` 是你的微信公众号的 AppSecret。 2. 获取用户信息 获取到 `access_token` 和 `openid` 后,可以使用以下代码获取用户信息: ```php $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN"; $response = file_get_contents($userinfo_url); $userinfo = json_decode($response, true); ``` 其中,`$userinfo` 包含用户的昵称、头像等信息。 3. 将用户信息保存到数据库 最后,将获取到的用户信息保存到数据库中,以便下次使用时快速登录。 ```php // 连接数据库 $con = mysqli_connect('localhost', 'username', 'password', 'database'); mysqli_set_charset($con, "utf8"); // 查询用户是否已存在 $sql = "SELECT * FROM users WHERE openid='$openid'"; $result = mysqli_query($con, $sql); if (mysqli_num_rows($result) == 0) { // 用户不存在,插入新用户信息 $nickname = mysqli_real_escape_string($con, $userinfo['nickname']); $headimgurl = mysqli_real_escape_string($con, $userinfo['headimgurl']); $sql = "INSERT INTO users (openid, nickname, headimgurl) VALUES ('$openid', '$nickname', '$headimgurl')"; mysqli_query($con, $sql); } // 保存用户登录状态 $_SESSION['openid'] = $openid; ``` 以上就是使用 PHP 实现微信公众号第三方登录的步骤。需要注意的是,为了确保安全性,应该对用户输入的数据进行过滤和验证,防止 SQL 注入和 XSS 攻击等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值