using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;
namespace ClassLibrary3
{
class Class2
{
private static object unknow = Type.Missing;
//private static object isReadOnly = ;
static object oReadOnly = true;
//object oMissing = System.Reflection.Missing.Value;
public static void CreateWordDocument()
{
// 启动Word并使Word可见
Application wordApp = new Application() { Visible = false };
// 新建Word文档
wordApp.Documents.Add();
Document wordDoc = wordApp.ActiveDocument;
Paragraph para = wordDoc.Paragraphs.Add();
para.Range.Text = "欢迎你,进入Learning Hard博客";
// 保存文档
object filename = @"D:\learninghard.doc";
wordDoc.SaveAs2(filename);
// 关闭Word
wordDoc.Close();
wordApp.Application.Quit();
}
public static void ModifyWordDocument3()
{
Application wordApp = new Application() { Visible = true };
object filename = @"D:\abc.docx";
//Document doc = wordApp.Documents.Open(filename);
Document doc = wordApp.Documents.Open(filename,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow);
// 关闭Word
doc.Close();
wordApp.Application.Quit();
}
public static void ModifyWordDocument()
{
Application wordApp = new Application() { Visible = true };
object filename = @"D:\abc3.docx";
//Document doc = wordApp.Documents.Open(filename);
Document doc = wordApp.Documents.Open(filename,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow);
int c = ParaCount(doc);
for(int i = 1; i <= c; i ++)
{
try
{
if (doc.Paragraphs[i] == null)
{
continue;
}
Paragraph p = doc.Paragraphs[i];
string o = (string)GetTextFromPara(doc.Paragraphs[i]);
string s = "第" + (i.ToString());
s = s + "段\n" + o + "\n";
if (!(o == String.Empty || o == null))
{
object start = p.Range.Start;
object end = p.Range.End - 1;
if (p.Range.Tables.Count > 0)
{
end = end = p.Range.End - 1;
}
if (start == end) continue;
//p.Range.(start, end).Text = "------------" + i.ToString();
Range rang = doc.Range(ref start, ref end);
rang.Text = "------------" + i.ToString();
}
//p.Range.Text.Replace(o, "------------" + i.ToString() + "\n");
Write("D:\\csharp.log", s);
}
catch (Exception e) {
Write("D:\\csharp.log", e.Message.ToString());
}
doc.SaveAs2("d:\\abc4.docx");
}
// 关闭Word
doc.Close();
wordApp.Application.Quit();
}
private static int ParaCount(Document doc)
{
return doc.Paragraphs.Count;
}
private static object GetTextFromPara(Paragraph p)
{
string s = p.Range.Text.Trim();
s = s.Replace("\a", "");
return s;
}
public static void Write(string path, string content)
{
FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
//开始写入
sw.Write("Hello World!!!!\n");
sw.Write(content);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
}
public string HttpRequest(string postMethod, string postPar)
{
string s = "";
string cReferralUrl = "http://www.baidu.com";
try
{
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(cReferralUrl);
hwr.Accept = "text/html, application/xhtml+xml, */*";
hwr.ContentType = "application/x-www-form-urlencoded";
hwr.Method = "POST";
hwr.KeepAlive = true;
hwr.ReadWriteTimeout = 3000;
byte[] data = Encoding.GetEncoding("gb2312").GetBytes(postPar.ToString());
using (Stream stream = hwr.GetRequestStream())
{
stream.Write(data, 0, data.Length);
stream.Close();
}
Stream stre = hwr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(stre);//创建一个stream读取流
string msgs = sr.ReadToEnd();
sr.Close();
return msgs;
}
catch (Exception error)
{
throw new Exception(String.Format("请求方法{0}时出现异常:", cReferralUrl) + error.Message + Environment.NewLine + "入参:" + postPar);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Word;
namespace ClassLibrary3
{
class Class2
{
private static object unknow = Type.Missing;
//private static object isReadOnly = ;
static object oReadOnly = true;
//object oMissing = System.Reflection.Missing.Value;
public static void CreateWordDocument()
{
// 启动Word并使Word可见
Application wordApp = new Application() { Visible = false };
// 新建Word文档
wordApp.Documents.Add();
Document wordDoc = wordApp.ActiveDocument;
Paragraph para = wordDoc.Paragraphs.Add();
para.Range.Text = "欢迎你,进入Learning Hard博客";
// 保存文档
object filename = @"D:\learninghard.doc";
wordDoc.SaveAs2(filename);
// 关闭Word
wordDoc.Close();
wordApp.Application.Quit();
}
public static void ModifyWordDocument3()
{
Application wordApp = new Application() { Visible = true };
object filename = @"D:\abc.docx";
//Document doc = wordApp.Documents.Open(filename);
Document doc = wordApp.Documents.Open(filename,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow);
// 关闭Word
doc.Close();
wordApp.Application.Quit();
}
public static void ModifyWordDocument()
{
Application wordApp = new Application() { Visible = true };
object filename = @"D:\abc3.docx";
//Document doc = wordApp.Documents.Open(filename);
Document doc = wordApp.Documents.Open(filename,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow, ref unknow,
ref unknow, ref unknow, ref unknow);
int c = ParaCount(doc);
for(int i = 1; i <= c; i ++)
{
try
{
if (doc.Paragraphs[i] == null)
{
continue;
}
Paragraph p = doc.Paragraphs[i];
string o = (string)GetTextFromPara(doc.Paragraphs[i]);
string s = "第" + (i.ToString());
s = s + "段\n" + o + "\n";
if (!(o == String.Empty || o == null))
{
object start = p.Range.Start;
object end = p.Range.End - 1;
if (p.Range.Tables.Count > 0)
{
end = end = p.Range.End - 1;
}
if (start == end) continue;
//p.Range.(start, end).Text = "------------" + i.ToString();
Range rang = doc.Range(ref start, ref end);
rang.Text = "------------" + i.ToString();
}
//p.Range.Text.Replace(o, "------------" + i.ToString() + "\n");
Write("D:\\csharp.log", s);
}
catch (Exception e) {
Write("D:\\csharp.log", e.Message.ToString());
}
doc.SaveAs2("d:\\abc4.docx");
}
// 关闭Word
doc.Close();
wordApp.Application.Quit();
}
private static int ParaCount(Document doc)
{
return doc.Paragraphs.Count;
}
private static object GetTextFromPara(Paragraph p)
{
string s = p.Range.Text.Trim();
s = s.Replace("\a", "");
return s;
}
public static void Write(string path, string content)
{
FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter sw = new StreamWriter(fs);
//开始写入
sw.Write("Hello World!!!!\n");
sw.Write(content);
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
}
public string HttpRequest(string postMethod, string postPar)
{
string s = "";
string cReferralUrl = "http://www.baidu.com";
try
{
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(cReferralUrl);
hwr.Accept = "text/html, application/xhtml+xml, */*";
hwr.ContentType = "application/x-www-form-urlencoded";
hwr.Method = "POST";
hwr.KeepAlive = true;
hwr.ReadWriteTimeout = 3000;
byte[] data = Encoding.GetEncoding("gb2312").GetBytes(postPar.ToString());
using (Stream stream = hwr.GetRequestStream())
{
stream.Write(data, 0, data.Length);
stream.Close();
}
Stream stre = hwr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(stre);//创建一个stream读取流
string msgs = sr.ReadToEnd();
sr.Close();
return msgs;
}
catch (Exception error)
{
throw new Exception(String.Format("请求方法{0}时出现异常:", cReferralUrl) + error.Message + Environment.NewLine + "入参:" + postPar);
}
}
}
}