using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
using Microsoft.VisualBasic;
using System.IO;
namespace Ks5uCms.Web2.Library.core
{
public class CCWordApp
{
private Microsoft.Office.Interop.Word.ApplicationClass oWordApplic; // a reference to Word application
private Microsoft.Office.Interop.Word.Document oDoc; // a reference to the document
private object missing = System.Reflection.Missing.Value;
public CCWordApp()
{
//
// TODO: 在此处添加构造函数逻辑
//
oWordApplic = new Microsoft.Office.Interop.Word.ApplicationClass();
}
// Open a file (the file must exists) and activate it
public void Open(string strFileName)
{
object fileName = strFileName;
object readOnly = false;
object isVisible = true;
oDoc = oWordApplic.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);
oDoc.Activate();
}
/// <summary>
/// Open a new with template
/// </summary>
/// <param name="strFileName"></param>
public void OpenWithTemplate(string strFileName)
{
object fileName = strFileName;
oDoc = oWordApplic.Documents.Add(ref fileName, ref missing, ref missing, ref missing);
oDoc.Activate();
}
/// <summary>
/// Open a new document
/// </summary>
public void Open()
{
oDoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);
oDoc.Activate();
}
/// <summary>
/// 释放
/// </summary>
public void Quit()
{
oDoc.Close(ref missing, ref missing, ref missing);
oWordApplic.Quit(ref missing, ref missing, ref missing);
}
public void Save()
{
oDoc.Save();
}
/// <summary>
/// 保存文件
/// </summary>
/// <param name="strFileName"></param>
public void SaveAs(string strFileName)
{
object fileName = strFileName;
oDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
/// <summary>
/// 添加内容
/// </summary>
/// <param name="strText"></param>
public void InsertText(string strText)
{
oWordApplic.Selection.TypeText(strText);
}
/// <summary>
/// 换行
/// </summary>
public void InsertLineBreak()
{
oWordApplic.Selection.TypeParagraph();
}
/// <summary>
/// 换多行
/// </summary>
/// <param name="nline"></param>
public void InsertLineBreak(int nline)
{
for (int i = 0; i < nline; i++)
oWordApplic.Selection.TypeParagraph();
}
/// <summary>
/// Change the paragraph alignement
/// 改变段对准
/// </summary>
/// <param name="strType"></param>
public void SetAlignment(string strType)
{
switch (strType)
{
case "Center ":
oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
break;
case "Left ":
oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
break;
case "Right ":
oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
break;
case "Justify ":
oWordApplic.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;
break;
}
}
// if you use thif function to change the font you should call it again with
// no parameter in order to set the font without a particular format
/// <summary>
/// 字形特别格式
/// </summary>
/// <param name="strType"></param>
public void SetFont(string strType)
{
switch (strType)
{
case "Bold ":
oWordApplic.Selection.Font.Bold = 1;
break;
case "Italic ":
oWordApplic.Selection.Font.Italic = 1;
break;
case "Underlined ":
oWordApplic.Selection.Font.Subscript = 0;
break;
}
}
/// <summary>
/// disable all the style
/// 设置字形
/// </summary>
public void SetFont()
{
oWordApplic.Selection.Font.Bold = 0;
oWordApplic.Selection.Font.Italic = 0;
oWordApplic.Selection.Font.Subscript = 0;
}
/// <summary>
/// 设置字体
/// </summary>
/// <param name="strType"></param>
public void SetFontName(string strType)
{
//VB:Selection.Font.Name = "黑体 "
oWordApplic.Selection.Font.Name = strType;
}
/// <summary>
/// 设置字体大小
/// </summary>
/// <param name="nSize"></param>
public void SetFontSize(int nSize)
{
//VB:Selection.Font.Size = 22
oWordApplic.Selection.Font.Size = nSize;
}
public void PasteAndFormat()
{
//Selection.PasteAndFormat (wdPasteDefault)
oWordApplic.Selection.PasteAndFormat(Microsoft.Office.Interop.Word.WdRecoveryType.wdPasteDefault);
}
/// <summary>
/// 插入页
/// </summary>
public void InsertPagebreak()
{
// VB : Selection.InsertBreak Type:=wdPageBreak
object pBreak = (int)Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
oWordApplic.Selection.InsertBreak(ref pBreak);
}
public void InsertBackspace()
{
// VB : Selection.TypeBackspace
oWordApplic.Selection.TypeBackspace();
}
/// <summary>
/// 插入书签
/// </summary>
/// <param name="BookMark"></param>
public void InsertBookMark(string BookMark)
{
/* VB : With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:= "dq "
.DefaultSorting = wdSortByName
.ShowHidden = False
End With*/
object Range = oWordApplic.Selection.Range;
oWordApplic.ActiveDocument.Bookmarks.Add(BookMark, ref Range);
}
/// <summary>
/// 插入图片
/// </summary>
/// <param name="FileName"></param>
public void InsertPicture(string FileName)
{
/* VB : Selection.InlineShapes.AddPicture FileName:=zp, LinkToFile:=False, SaveWithDocument:=True*/
object LinkToFile = false;
object SaveWithDocument = true;
oWordApplic.Selection.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref missing);
}
// Go to a predefined bookmark, if the bookmark doesn 't exists the application will raise an error
public void GotoBookMark(string strBookMarkName)
{
// VB : Selection.GoTo What:=wdGoToBookmark, Name:= "nome "
object Bookmark = (int)Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
object NameBookMark = strBookMarkName;
oWordApplic.Selection.GoTo(ref Bookmark, ref missing, ref missing, ref NameBookMark);
}
public void GoToTheEnd()
{
// VB : Selection.EndKey Unit:=wdStory
object unit;
unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
oWordApplic.Selection.EndKey(ref unit, ref missing);
}
public void GoToTheBeginning()
{
// VB : Selection.HomeKey Unit:=wdStory
object unit;
unit = Microsoft.Office.Interop.Word.WdUnits.wdStory;
oWordApplic.Selection.HomeKey(ref unit, ref missing);
}
public void GoToTheTable(int ntable)
{
// Selection.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1, Name:= " "
// Selection.Find.ClearFormatting
// With Selection.Find
// .Text = " "
// .Replacement.Text = " "
// .Forward = True
// .Wrap = wdFindContinue
// .Format = False
// .MatchCase = False
// .MatchWholeWord = False
// .MatchWildcards = False
// .MatchSoundsLike = False
// .MatchAllWordForms = False
// End With
object what;
what = Microsoft.Office.Interop.Word.WdUnits.wdTable;
object which;
which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
object count;
count = 1;
oWordApplic.Selection.GoTo(ref what, ref which, ref count, ref missing);
oWordApplic.Selection.Find.ClearFormatting();
oWordApplic.Selection.Text = " ";
}
public void GoToRightCharacter(int Count)
{
// Selection.MoveRight Unit:=wdCharacter
object count = Count;
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdCharacter;
oWordApplic.Selection.MoveRight(ref direction, ref count, ref missing);
}
public void GoToRightCharacterAndSelected(int Count, string Extend)
{
// Selection.MoveDown Unit:=wdLine, Count:=32, Extend:=wdExtend
object count = Count;
object extend = Extend;
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;
oWordApplic.Selection.MoveRight(ref direction, ref count, ref extend);
}
public void CopyNewTable()
{
/*VB: Selection.WholeStory
Selection.Copy*/
//object Extend=Word.WdMovementType.wdExtend;
//object direction = Word.WdUnits.wdLine;
//oWordApplic.Selection.MoveDown(ref direction,ref count,ref Extend);
oWordApplic.Selection.WholeStory();
oWordApplic.Selection.Copy();
GoToTheBeginning();
}
public void GoToRightCell()
{
// Selection.MoveRight Unit:=wdCell
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;
oWordApplic.Selection.MoveRight(ref direction, ref missing, ref missing);
}
public void GoToLeftCell()
{
// Selection.MoveRight Unit:=wdCell
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdCell;
oWordApplic.Selection.MoveLeft(ref direction, ref missing, ref missing);
}
public void GoToDownCell()
{
// Selection.MoveRight Unit:=wdCell
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;
oWordApplic.Selection.MoveDown(ref direction, ref missing, ref missing);
}
public void GoToUpCell()
{
// Selection.MoveRight Unit:=wdCell
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;
oWordApplic.Selection.MoveUp(ref direction, ref missing, ref missing);
}
public void GoToUpCell(int Count)
{
// Selection.MoveRight Unit:=wdCell
object count = Count;
object direction;
direction = Microsoft.Office.Interop.Word.WdUnits.wdLine;
oWordApplic.Selection.MoveUp(ref direction, ref count, ref missing);
}
// this function doesn 't work
public void InsertPageNumber(string strType, bool bHeader)
{
object alignment;
object bFirstPage = false;
object bF = true;
//if (bHeader == true)
//WordApplic.Selection.HeaderFooter.PageNumbers.ShowFirstPageNumber = bF;
switch (strType)
{
case "Center ":
alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
//WordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment,ref bFirstPage);
//Word.Selection objSelection = WordApplic.pSelection;
// oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
break;
case "Right ":
alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberRight;
// oWordApplic.Selection.HeaderFooter.PageNumbers.Item(1).Alignment = Word.WdPageNumberAlignment.wdAlignPageNumberRight;
break;
case "Left ":
alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberLeft;
oWordApplic.Selection.HeaderFooter.PageNumbers.Add(ref alignment, ref bFirstPage);
break;
}
}
///<summary>
/// 在word 中查找一个字符串直接替换所需要的文本
/// </summary>
/// <param name="strOldText">原文本</param>
/// <param name="strNewText">新文本</param>
/// <returns></returns>
public bool Replace(string strOldText, string strNewText)
{
this.oWordApplic.Selection.Find.Text = strOldText;
object FindText, ReplaceWith, Replace;//
object MissingValue = Type.Missing;
FindText = strOldText;//要查找的文本
ReplaceWith = strNewText;//替换文本
Replace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;/**//*wdReplaceAll - 替换找到的所有项。
* wdReplaceNone - 不替换找到的任何项。
* wdReplaceOne - 替换找到的第一项。
* */
this.oWordApplic.Selection.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置
if (this.oWordApplic.Selection.Find.Execute(
ref FindText, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue, ref MissingValue,
ref ReplaceWith, ref Replace,
ref MissingValue, ref MissingValue,
ref MissingValue, ref MissingValue))
{
return true;
}
return false;
}
public bool SearchReplace(string strOldText, string strNewText)
{
object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
//首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。
oWordApplic.Selection.Find.ClearFormatting();
oWordApplic.Selection.Find.Text = strOldText;
oWordApplic.Selection.Find.Replacement.ClearFormatting();
oWordApplic.Selection.Find.Replacement.Text = strNewText;
if (oWordApplic.Selection.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing))
{
return true;
}
return false;
}
/// <summary>
/// 获取书签的位置
/// </summary>
/// <param name="word">文档对象</param>
/// <returns></returns>
private int[,] GetPosition(Document word)
{
int bmcount = word.Bookmarks.Count;
int[,] result = new int[bmcount, 2];
for (int i = 1; i <= bmcount; i++)
{
object index = i;
Bookmark bm = word.Bookmarks.get_Item(ref index);
if (bm.Name == "【结束】" + Convert.ToString(i + 1))
{
result[i, 0] = bm.Start;
result[i, 1] = bm.End;
}
}
return result;
}
public void InsertParagraphBeforeByParagraph( Microsoft.Office.Interop.Word.Paragraph paragraph,int nLine )
{
for ( int i = 0; i < nLine; i++ )
paragraph.Range.InsertParagraphBefore();
}
public void InsertParagraphAfterByParagraph( Microsoft.Office.Interop.Word.Paragraph paragraph )
{
paragraph.Range.InsertParagraphAfter();
}
public void InsertParagraphAfterByParagraph( Microsoft.Office.Interop.Word.Paragraph paragraph, int nLine )
{
for ( int i = 0; i < nLine; i++ )
paragraph.Range.InsertParagraphAfter();
}
/** <summary>
/// 段落的对齐方式
/// 例如:word.SetAlignment(Word.WdParagraphAlignment.wdAlignParagraphCenter)
/// </summary>
/// <param name="alignment"></param>
public void SetAlignment( Microsoft.Office.Interop.Word.WdParagraphAlignment alignment )
{
oWordApplic.Selection.ParagraphFormat.Alignment = alignment;
}
/** <summary>
/// 首行缩进
/// </summary>
/// <param name="fltCount">float类型的数值</param>
public void SetFirstLineIndent( float fltCount )
{
oWordApplic.Selection.ParagraphFormat.FirstLineIndent = fltCount;
}
/** <summary>
/// 左缩进
/// </summary>
/// <param name="fltCount">float类型的数值</param>
public void SetLeftIndent( float fltCount )
{
oWordApplic.Selection.ParagraphFormat.LeftIndent = fltCount;
}
/** <summary>
/// 右缩进
/// </summary>
/// <param name="fltCount">float类型的数值</param>
public void SetRightIndent(float fltCount)
{
oWordApplic.Selection.ParagraphFormat.RightIndent = fltCount;
}
/** <summary>
/// 设置是否有粗体,0->否 ,1->是
/// </summary>
/// <param name="intBoldBi"></param>
public void setBoldBi( int intBoldBi )
{
oWordApplic.Selection.Font.BoldBi = intBoldBi;
//oWordApplic.Selection.Font.Bold = intBoldBi;
}
public void SetBoldSize( int intBold )
{
oWordApplic.Selection.Font.Bold = intBold;
}
public void SetUnderLine( Microsoft.Office.Interop.Word.WdUnderline underLine )
{
oWordApplic.Selection.Font.Underline = underLine;
}
public void SetUnderLineColor( Microsoft.Office.Interop.Word.WdColor Color )
{
oWordApplic.Selection.Font.UnderlineColor = Color;
}
/** <summary>
/// 向后定位到指定段落
/// </summary>
/// <param name="para"></param>
/// <param name="count"></param>
public void GoToNextParagraph(ref Microsoft.Office.Interop.Word.Paragraph para,ref object count)
{
para.Next(ref count) ;
}
/** <summary>
/// 向前定位到指定段落
/// </summary>
/// <param name="para"></param>
/// <param name="count"></param>
public void GoToPreviousParagraph( ref Microsoft.Office.Interop.Word.Paragraph para, ref object count )
{
para.Previous( ref count ) ;
}
public Boolean ExecuteReplace( Microsoft.Office.Interop.Word.Find find, Object replaceOption )
{
// Simple wrapper around Find.Execute:
Object findText = Type.Missing;
Object matchCase = Type.Missing;
Object matchWholeWord = Type.Missing;
Object matchWildcards = Type.Missing;
Object matchSoundsLike = Type.Missing;
Object matchAllWordForms = Type.Missing;
Object forward = Type.Missing;
Object wrap = Type.Missing;
Object format = Type.Missing;
Object replaceWith = Type.Missing;
Object replace = replaceOption;
Object matchKashida = Type.Missing;
Object matchDiacritics = Type.Missing;
Object matchAlefHamza = Type.Missing;
Object matchControl = Type.Missing;
return find.Execute( ref findText, ref matchCase, ref matchWholeWord,
ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms,
ref forward, ref wrap, ref format, ref replaceWith, ref replace,
ref matchKashida, ref matchDiacritics, ref matchAlefHamza,
ref matchControl );
}
public Boolean ExecuteFind( Microsoft.Office.Interop.Word.Find find )
{
return ExecuteFind( find, find.Text, Type.Missing, Type.Missing );
}
public Boolean ExecuteFind( Microsoft.Office.Interop.Word.Find find, string strFindText )
{
return ExecuteFind( find, strFindText, Type.Missing, Type.Missing );
}
public Boolean ExecuteFind(
Microsoft.Office.Interop.Word.Find find, string strFindText, Object wrapFind, Object forwardFind )
{
// Simple wrapper around Find.Execute:
Object findText ;
Object matchCase = Type.Missing;
Object matchWholeWord = Type.Missing;
Object matchWildcards = Type.Missing;
Object matchSoundsLike = Type.Missing;
Object matchAllWordForms = Type.Missing;
Object forward = forwardFind;
Object wrap = wrapFind;
Object format = Type.Missing;
Object replaceWith = Type.Missing;
Object replace = Type.Missing;
Object matchKashida = Type.Missing;
Object matchDiacritics = Type.Missing;
Object matchAlefHamza = Type.Missing;
Object matchControl = Type.Missing;
if ( ( strFindText == "" )||( strFindText == string.Empty ) )
findText = find.Text;
else
findText = strFindText;
find.ClearFormatting();
return find.Execute( ref findText, ref matchCase, ref matchWholeWord,
ref matchWildcards, ref matchSoundsLike, ref matchAllWordForms,
ref forward, ref wrap, ref format, ref replaceWith, ref replace,
ref matchKashida, ref matchDiacritics, ref matchAlefHamza,
ref matchControl );
}
public Boolean FindInSelection( Microsoft.Office.Interop.Word.Selection Selection, string strFindText )
{
return this.ExecuteFind( Selection.Find,strFindText,System.Type.Missing,System.Type.Missing );
}
public Boolean FindInSelection( Microsoft.Office.Interop.Word.Selection Selection, string strFindText, Object wrapFind, Object forwardFind )
{
return this.ExecuteFind( Selection.Find, strFindText, wrapFind, forwardFind );
}
public Boolean FindInRange( Microsoft.Office.Interop.Word.Range range,string strFindText )
{
Boolean blnReturn =this.ExecuteFind( range.Find, strFindText, Type.Missing, Type.Missing );
range.Select();
return blnReturn;
}
/// <summary>
/// 根据文本批量拆分word
/// </summary>
/// <param name="strFindText">拆分文字</param>
public System.Collections.ArrayList FindInAllDocument( string strFindText )
{
System.Collections.ArrayList list = new System.Collections.ArrayList();
Microsoft.Office.Interop.Word.Find fnd = oWordApplic.Selection.Find;
fnd.Text = strFindText;
fnd.Replacement.Text = "";
fnd.Application.ScreenUpdating = false;
fnd.Forward = true;
fnd.Wrap = WdFindWrap.wdFindStop;
fnd.Format = false;
fnd.MatchCase = false;
fnd.MatchWholeWord = false;
fnd.MatchByte = false;
fnd.MatchAllWordForms = false;
fnd.MatchSoundsLike = false;
fnd.MatchWildcards = true;
Object nStart, nEnd;
Boolean fContinue ;
fContinue = true;
Object wdStory= WdUnits.wdStory;
oWordApplic.Selection.StartOf(ref wdStory,ref missing);
while (fContinue)
{
fnd.ClearFormatting();
nStart = oWordApplic.Selection.Start;
if (ExecuteFind(fnd))
{
Object dwLine=Microsoft.Office.Interop.Word.WdUnits.wdLine;
nEnd = oWordApplic.Selection.End;
}
else
{
nEnd = oDoc.Content.End;
fContinue = false;
}
object wdCollapseEnd = WdCollapseDirection.wdCollapseEnd;
oWordApplic.Selection.Collapse(ref wdCollapseEnd);
oDoc.Range(ref nStart,ref nEnd).Copy();
Document newdoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);
newdoc.Content.Paste();
string path= System.Web.HttpContext.Current.Server.MapPath("word");
//要保存的文件夹
string follter = DateTime.Now.ToShortDateString();
if (!System.IO.Directory.Exists(path + "/" + follter))
{
System.IO.Directory.CreateDirectory(path + "/" + follter);
}
object filename = follter+"/"+Ks5uCMS.Common.Rand.Str(5) + "-" + Ks5uCMS.Common.Rand.Str(5) + "-" + DateTime.Now.Ticks + ".doc";
object newFileName = path + "/" + filename;
newdoc.SaveAs(ref newFileName, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
newdoc.Close(ref missing, ref missing, ref missing);
list.Add(filename);
}
return list;
}
/// <summary>
/// 从字符开始到结束的word
/// </summary>
/// <param name="startStr"></param>
/// <param name="endStr"></param>
/// <returns></returns>
public string FindTitleAllDocument(string startStr,string endStr)
{
this.Replace("【解析】","");
Microsoft.Office.Interop.Word.Find fnd = oWordApplic.Selection.Find;
fnd.Text = startStr;
fnd.Replacement.Text = "";
fnd.Application.ScreenUpdating = false;
fnd.Forward = true;
fnd.Wrap = WdFindWrap.wdFindStop;
fnd.Format = false;
fnd.MatchCase = false;
fnd.MatchWholeWord = false;
fnd.MatchByte = false;
fnd.MatchAllWordForms = false;
fnd.MatchSoundsLike = false;
fnd.MatchWildcards = true;
Object nStart, nEnd;
Object wdStory = WdUnits.wdStory;
oWordApplic.Selection.StartOf(ref wdStory, ref missing);
fnd.ClearFormatting();
if (ExecuteFind(fnd))
nStart = oWordApplic.Selection.End;
else
nStart = oWordApplic.Selection.Start;
fnd.Text = endStr;
oWordApplic.Selection.StartOf(ref wdStory, ref missing);
fnd.ClearFormatting();
if (ExecuteFind(fnd))
nEnd = oWordApplic.Selection.Start;
else
nEnd = oDoc.Content.End;
object wdCollapseEnd = WdCollapseDirection.wdCollapseEnd;
oWordApplic.Selection.Collapse(ref wdCollapseEnd);
oDoc.Range(ref nStart, ref nEnd).Copy();
Document newdoc = oWordApplic.Documents.Add(ref missing, ref missing, ref missing, ref missing);
newdoc.Content.Paste();
string path = System.Web.HttpContext.Current.Server.MapPath("word");
//要保存的文件夹
string follter = DateTime.Now.ToShortDateString();
if (!System.IO.Directory.Exists(path + "/" + follter))
{
System.IO.Directory.CreateDirectory(path + "/" + follter);
}
object filename = follter + "/" + Ks5uCMS.Common.Rand.Str(5) + "-" + Ks5uCMS.Common.Rand.Str(5) + "-" + DateTime.Now.Ticks + ".doc";
object newFileName = path + "/" + filename;
newdoc.SaveAs(ref newFileName, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
newdoc.Close(ref missing, ref missing, ref missing);
return filename.ToString();
}
/// <summary>
/// 保存成HTML
/// </summary>
/// <param name="strFileName"></param>
public void SaveAsHtml(string strFileName)
{
Type wordType = oWordApplic.GetType();
object missing = System.Reflection.Missing.Value;
object fileName = strFileName;
object Format = (int)Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
oDoc.SaveAs(ref fileName, ref Format, ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
}
}
}
使用代码:
一开始位置到结束位置截取内容
Ks5uCms.Web2.Library.core.uploadWorldpassHTML uploadword = new Ks5uCms.Web2.Library.core.uploadWorldpassHTML();
Ks5uCms.Web2.Library.core.CCWordApp wp = new Ks5uCms.Web2.Library.core.CCWordApp();
wp.Open(path);
string path1 = wp.FindTitleAllDocument("【题文】", "【答案】");
string path2 = wp.FindTitleAllDocument("【答案】", "【标题】");
wp.Quit();
二、根据结束标记批量拆分word
Ks5uCms.Web2.Library.core.CCWordApp wp = new Ks5uCms.Web2.Library.core.CCWordApp();
wp.Open(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + filename));
System.Collections.ArrayList arraylist = wp.FindInAllDocument("^13【结束】^13");
wp.Quit();