九色国产,午夜在线视频,新黄色网址,九九色综合,天天做夜夜做久久做狠狠,天天躁夜夜躁狠狠躁2021a,久久不卡一区二区三区

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
C#根據(jù)Word模版生成Word文件

本文實例為大家分享了C#根據(jù)Word模版生成Word文的具體代碼,供大家參考,具體內(nèi)容如下

1、指定的word模版

2、生成word類

添加com Microsoft word 11.0 Object Library 引用

using System;

using System.Collections.Generic;

using System.Data;

using System.Windows.Forms;

using Word = Microsoft.Office.Interop.Word;

using System.IO;

namespace Headfree.DefUI

{

public class WordUtility

{

private object tempFile = null;

private object saveFile = null;

private static Word._Document wDoc = null; //word文檔

private static Word._Application wApp = null; //word進(jìn)程

private object missing = System.Reflection.Missing.Value;

public WordUtility(string tempFile, string saveFile)

{

this.tempFile = Path.Combine(Application.StartupPath, @tempFile);

this.saveFile = Path.Combine(Application.StartupPath, @saveFile);

}

///

/// 模版包含頭部信息和表格,表格重復(fù)使用

///

/// 重復(fù)表格的數(shù)據(jù)

/// word中要替換的表達(dá)式和表格字段的對應(yīng)關(guān)系

/// 簡單的非重復(fù)型數(shù)據(jù)

public bool GenerateWord(DataTable dt, Dictionary expPairColumn, Dictionary simpleExpPairValue)

{

if (!File.Exists(tempFile.ToString()))

{

MessageBox.Show(string.Format("{0}模版文件不存在,請先設(shè)置模版文件。", tempFile.ToString()));

return false;

}

try

{

wApp = new Word.Application();

wApp.Visible = false;

wDoc = wApp.Documents.Add(ref tempFile, ref missing, ref missing, ref missing);

wDoc.Activate();// 當(dāng)前文檔置前

bool isGenerate = false;

if (simpleExpPairValue != null && simpleExpPairValue.Count > 0)

isGenerate = ReplaceAllRang(simpleExpPairValue);

// 表格有重復(fù)

if (dt != null && dt.Rows.Count > 0 && expPairColumn != null && expPairColumn.Count > 0)

isGenerate = GenerateTable(dt, expPairColumn);

if (isGenerate)

wDoc.SaveAs(ref saveFile, 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);

DisposeWord();

return true;

}

catch (Exception ex)

{

MessageBox.Show("生成失敗" + ex.Message);

return false;

}

}

///

/// 單個替換 模版沒有重復(fù)使用的表格

///

/// 要替換的

public bool GenerateWord(Dictionary dc)

{

return GenerateWord(null, null, dc);

}

private bool GenerateTable(DataTable dt, Dictionary expPairColumn)

{

try

{

int tableNums = dt.Rows.Count;

Word.Table tb = wDoc.Tables[1];

tb.Range.Copy();

Dictionary dc = new Dictionary();

for (int i = 0; i < tableNums; i++)

{

dc.Clear();

if (i == 0)

{

foreach (string key in expPairColumn.Keys)

{

string column = expPairColumn[key];

object value = null;

value = dt.Rows[i][column];

dc.Add(key, value);

}

ReplaceTableRang(wDoc.Tables[1], dc);

continue;

}

wDoc.Paragraphs.Last.Range.Paste();

foreach (string key in expPairColumn.Keys)

{

string column = expPairColumn[key];

object value = null;

value = dt.Rows[i][column];

dc.Add(key, value);

}

ReplaceTableRang(wDoc.Tables[1], dc);

}

return true;

}

catch (Exception ex)

{

DisposeWord();

MessageBox.Show("生成模版里的表格失敗。" + ex.Message);

return false;

}

}

private bool ReplaceTableRang(Word.Table table, Dictionary dc)

{

try

{

object replaceArea = Word.WdReplace.wdReplaceAll;

foreach (string item in dc.Keys)

{

object replaceKey = item;

object replaceValue = dc[item];

table.Range.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing,

ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing,

ref missing);

}

return true;

}

catch (Exception ex)

{

DisposeWord();

MessageBox.Show(string.Format("{0}模版中沒有找到指定的要替換的表達(dá)式。{1}", tempFile, ex.Message));

return false;

}

}

private bool ReplaceAllRang(Dictionary dc)

{

try

{

object replaceArea = Word.WdReplace.wdReplaceAll;

foreach (string item in dc.Keys)

{

object replaceKey = item;

object replaceValue = dc[item];

wApp.Selection.Find.Execute(ref replaceKey, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing,

ref replaceValue, ref replaceArea, ref missing, ref missing, ref missing,

ref missing);

}

return true;

}

catch (Exception ex)

{

MessageBox.Show(string.Format("{0}模版中沒有找到指定的要替換的表達(dá)式。{1}", tempFile, ex.Message));

return false;

}

}

private void DisposeWord()

{

object saveOption = Word.WdSaveOptions.wdSaveChanges;

wDoc.Close(ref saveOption, ref missing, ref missing);

saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;

wApp.Quit(ref saveOption, ref missing, ref missing); //關(guān)閉Word進(jìn)程

}

}

}

3、效果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
asp.net 填充word 模版標(biāo)簽并下載使用筆記
類似百度文庫在線預(yù)覽文檔flash版(支持word、excel、ppt、pdf)+在線預(yù)覽文檔html版
C#實現(xiàn) word、pdf、ppt 轉(zhuǎn)為圖片
webbrowser控件顯示word文檔
部分Office 2007文件格式轉(zhuǎn)換為xps和pdf代碼整理
簡單幾步提高PPt制作效率
更多類似文章 >>
生活服務(wù)
熱點新聞
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服