||
1、新建Word文檔
CreateWordFile
新建Word文檔#region 新建Word文檔
/**//// <summary>
/// 動態(tài)生成Word文檔并填充內(nèi)容
/// </summary>
/// <param name="dir">文檔目錄</param>
/// <param name="fileName">文檔名</param>
/// <returns>返回自定義信息</returns>
public static bool CreateWordFile(string dir, string fileName)
{
try
{
Object oMissing = System.Reflection.Missing.Value;
if (!Directory.Exists(dir))
{
//創(chuàng)建文件所在目錄
Directory.CreateDirectory(dir);
}
//創(chuàng)建Word文檔(Microsoft.Office.Interop.Word)
Microsoft.Office.Interop.Word._Application WordApp = new Application();
WordApp.Visible = true;
Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Add(
ref oMissing, ref oMissing, ref oMissing, ref oMissing);
//此處插入你想寫的文字,公式,標(biāo)格,圖片,對象等等。
//保存
object filename = dir + fileName;
WordDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
return false;
}
}
#endregion 新建Word文檔
首先看看Word菜單插入/域:域類別=等式和公式;域名=Eq;(創(chuàng)建科學(xué)公式);域選項(開關(guān))如下表:
開關(guān) |
說明 |
\A() |
用任何編號的參數(shù)繪制二維矩陣 |
\B() |
用適合元素大小的括弧來括住單個的元素 |
\D() |
精確控制下一個字符的水平位置 |
\F(,) |
創(chuàng)建在分?jǐn)?shù)線上下居中的分?jǐn)?shù) |
\I(,,) |
利用上限、下限和被積函數(shù)建立一個積分 |
\L() |
創(chuàng)建數(shù)值列表 |
\O() |
將每一個連續(xù)的元素重疊在其前一個元素上 |
\R() |
繪制根式 |
\S() |
將元素設(shè)置為上標(biāo)或下標(biāo) |
\X() |
在括號內(nèi)文字的周圍創(chuàng)建一個方框 |
下面看幾個比較實用的例子:
“EQ \i(-1,1,3x+7)”表示“3x+7”從-1到1的積分,開關(guān)\i(,,)能使用指定的符號或默認(rèn)符號及三個元素創(chuàng)建積分。
“EQ \r(2,3m-2)”表示3m-2的平方根,(開關(guān)\r(,)能使用一個或兩個元素繪制根號。
“EQ \f(7,8)”表示八分之七 ,(開關(guān)\f(,)能創(chuàng)建分?jǐn)?shù))。
“EQ \a \ac \co2 \vs3 \hs3(Ax,By,C,D)”表示一個二維數(shù)組。
“EQ \b \bc\{ (\r(3,x+y))” ,表示用方括號括住單個元素。
4.打入一個表格
public static void in_xls(string fileName)
{
object type = string.Format(@"Excel.Sheet.08");//插入的excel 格式,這里我用的是excel 2010,所以是.12
object filename = fileName;//插入的excel的位置
//移動光標(biāo)文檔末尾
object count = WordDoc.Paragraphs.Count;
object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdParagraph;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref oMissing);//移動焦點
WordApp.Selection.TypeParagraph();//插入段落
object LinkToFile = false;
object SaveWithDocument = true;
object Anchor = WordDoc.Application.Selection.Range;
WordApp.Selection.InlineShapes.AddOLEObject(ref type, ref filename, ref oMissing, ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref Anchor);//執(zhí)行插入操作
para.Range.InsertParagraphAfter();
}
效果式樣:
這樣,就可以利用如上的這些東西自動生成計算說明書了。具體的排版可以再參照其他的具體細(xì)節(jié)。大致列出了如何操作word的基本方法。
重要提示:建議使用NPOI庫進(jìn)行操作,excel我查了一些方法,只能讀取沒法對一些復(fù)雜的表格進(jìn)行寫入,會造成表格丟失。
Archiver|手機版|小黑屋|機械社區(qū) ( 京ICP備10217105號-1,京ICP證050210號,浙公網(wǎng)安備33038202004372號 )
GMT+8, 2025-8-18 18:11 , Processed in 0.049088 second(s), 18 queries , Gzip On.
Powered by Discuz! X3.5 Licensed
© 2001-2025 Discuz! Team.