最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#讀取多條數(shù)據(jù)記錄導(dǎo)出到Word之圖片輸出改造

 更新時(shí)間:2024年11月25日 09:20:43   作者:初九之潛龍勿用  
這篇文章主要為大家詳細(xì)介紹了C#讀取多條數(shù)據(jù)記錄并導(dǎo)出到Word標(biāo)簽?zāi)0逯械膱D片輸出問題,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下

應(yīng)用需求

在我的文章《C# 讀取多條數(shù)據(jù)記錄導(dǎo)出到 Word 標(biāo)簽?zāi)0?/a>》里,講述讀取多條數(shù)據(jù)記錄結(jié)合 WORD 標(biāo)簽?zāi)0遢敵鑫募墓δ?,原有輸出圖片的約定是在 WORD 模板文件中借助書簽設(shè)置進(jìn)行輸出,如下圖:

該書簽名稱代表了一條指令,格式為(輸出關(guān)鍵字_圖片寬度_圖片高度)。如圖中書簽名稱設(shè)置,當(dāng)系統(tǒng)遇到 ds_qz 關(guān)鍵字時(shí),輸出按后繼設(shè)置進(jìn)行,圖片寬度為100、圖片高度為 50。

現(xiàn)有一模板需求如下圖:

我們需要在考官評(píng)語和考官本人簽字位置輸出手寫圖片,原有的書簽?zāi)J捷敵鰰?huì)遇到一些問題:

(1)只能為嵌入式,位置輸出無法設(shè)置,無法進(jìn)一步調(diào)整到較優(yōu)呈現(xiàn)位置。

(2)只能插入式輸出,無法實(shí)現(xiàn)嵌入式如文字浮動(dòng)、環(huán)繞效果。

(3)需要在無關(guān)位置插入書簽,否則會(huì)在輸出關(guān)鍵字時(shí)被替換掉,而無法識(shí)別配置。

(4)書簽的名稱不能輸入一些特定字符,如%,減號(hào)等,給配置帶來一些麻煩。

(5)如果在文檔里真需要設(shè)置書簽,容易造成混亂。

設(shè)計(jì)

設(shè)計(jì)提供一個(gè) ArrayList 類型參數(shù)取代書簽?zāi)J降脑O(shè)計(jì),其格式為 (輸出關(guān)鍵字_圖片寬度_圖片高度_圖片 Left 相對值_圖片 Top 相對值 )。如添加 ds_qz_100_50_500_-20 字符串,當(dāng)系統(tǒng)遇到 ds_qz 關(guān)鍵字時(shí),輸出按后繼設(shè)置進(jìn)行,圖片寬度為100、圖片高度為 50、圖片 Left 為500、圖片 Top 為 -20。后四個(gè)參數(shù)如果設(shè)置null則表示忽略,ds_qz_null_null_500_-20 ,則表示忽略寬度和高度設(shè)置,即表示原始大小輸出。

定位到的圖片輸出,采取浮動(dòng)于文字下方的處理。

范例運(yùn)行環(huán)境

操作系統(tǒng): Windows Server 2019 DataCenter

操作系統(tǒng)上安裝 Office Word 2016

數(shù)據(jù)庫:Microsoft SQL Server 2016

.net版本: .netFramework4.7.2 或以上

開發(fā)工具:VS2019  C#

配置Office DCOM

配置方法可參照我的文章《C# 讀取Word表格到DataSet》進(jìn)行處理和配置。

實(shí)現(xiàn)代碼

組件庫引入

核心代碼

public string DataTableToWord(string _filename,string _repeatKey,object _dataset,ArrayList DataSetPictureConfig),該方法提供4個(gè)參數(shù),WORD模板文件名、自定義關(guān)鍵字、System.Data.DataSet,配置圖片用的 ArrayList 列表指令。

public void DataTableToWord(string _filename,string _repeatKey,object _dataset,ArrayList DataSetPictureConfig)
        {
            if (DataSetPictureConfig == null) { DataSetPictureConfig = new ArrayList(); }
 
            Object Nothing = System.Reflection.Missing.Value;
            object filename = _filename;
            //創(chuàng)建一個(gè)名為WordApp的組件對象
            Word.Application WordApp = new Word.Application();
            //創(chuàng)建一個(gè)名為WordDoc的文檔對象
            WordApp.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
 
            Word.Document WordDoc = WordApp.Documents.Open(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
 
            WordDoc.SpellingChecked = false;//關(guān)閉拼寫檢查
 
            WordDoc.ShowSpellingErrors = false;//關(guān)閉顯示拼寫錯(cuò)誤提示框
 
				WordApp.Selection.WholeStory();
				WordApp.Selection.Cut();
			 	DataSet ds=(DataSet)_dataset;
				System.Data.DataTable  dt=ds.Tables[0];
				for(int i=0;i<dt.Rows.Count;i++)
				{
					WordApp.Selection.Paste();
					for(int j=0;j<dt.Columns.Count;j++)
					{
						
						string _repKey=_repeatKey+dt.Columns[j].ColumnName.ToString();
						string _repValue=string.Format("{0}",dt.Rows[i][j].ToString());
 
						bool isPhoto=false;
						if(dt.Columns[j].DataType==typeof(System.Byte[]))
						{
							isPhoto=true;
							_repValue="";
						}
 
 
						
						WordApp.Options.ReplaceSelection=true;
						Word.Find fnd = WordApp.Selection.Find;
						fnd.ClearFormatting();
 
						Object findText = _repKey;
						Object matchCase = false;
						Object matchWholeWord = Type.Missing;
						Object matchWildcards = false;
						Object matchSoundsLike = false;
						Object matchAllWordForms = false;
						Object forward = true;
						Object wrap =Word.WdFindWrap.wdFindContinue;
						Object format = false;
						Object replaceWith ="";
						Object replace =Type.Missing;;
						Object matchKashida = Type.Missing;
						Object matchDiacritics = Type.Missing;
						Object matchAlefHamza = Type.Missing;
						Object matchControl = Type.Missing;
 
						
						
						while(fnd.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))
						{
 
							string r_f=WordApp.Selection.Font.Name.ToString();
							
								
							WordApp.Selection.Range.Text=_repValue;
							if(isPhoto==true)
							{
								string _jpgfile=_path+System.Guid.NewGuid()+".jpg";
                                if (dt.Rows[i][j] == System.DBNull.Value)
                                {
                                    continue;
                                }
								byte[] filedata = (byte[])dt.Rows[i][j];
								System.IO.MemoryStream ms = new MemoryStream(filedata);
								System.Drawing.Image img1 = System.Drawing.Image.FromStream(ms);
								img1.Save(@_jpgfile);
								ms.Close();
                                Word.InlineShape pic= WordApp.Selection.InlineShapes.AddPicture(@_jpgfile,false,true);
                                foreach (string bm in  DataSetPictureConfig)
								{
									string _findkey=_repKey+"_";
 
                                    int _f1 =bm.IndexOf(_findkey);
									if(_f1==0 && bm.Length>(_findkey.Length))
									{
                                        string[] _paras=bm.Substring(_findkey.Length,bm.Length-_findkey.Length).Split('_');
										if(_paras.GetLength(0)>1){
                                            if (_paras[0] != "null")
                                            {
                                                int def_width = int.Parse(_paras[0]);
                                                pic.Width = def_width;
                                            }
                                            if (_paras[1] != "null")
                                            {
                                                int def_height = int.Parse(_paras[1]);
                                                pic.Height = def_height;
                                            }
 
                                        }
                                    }
								}
                                Word.Shape pic2 = pic.ConvertToShape();
                                pic2.WrapFormat.Type = Word.WdWrapType.wdWrapThrough;
                                pic2.WrapFormat.Type = Word.WdWrapType.wdWrapBehind;
                                pic2.Left = 0;
                                pic2.Top = 0;
                                foreach (string bm in DataSetPictureConfig)
                                {
                                    string _findkey = _repKey + "_";
 
                                    int _f1 = bm.IndexOf(_findkey);
                                    if (_f1 == 0 && bm.Length > (_findkey.Length))
                                    {
                                        string[] _paras = bm.Substring(_findkey.Length, bm.Length - _findkey.Length).Split('_');
                                        if (_paras.GetLength(0) > 3)
                                        {
                                            if (_paras[2] != "null")
                                                pic2.Left = float.Parse(_paras[2]);
                                            if (_paras[3] != "null")
                                                pic2.Top =float.Parse(_paras[3]);
 
                                        }
                                    }
                                }
								File.Delete(@_jpgfile);
							}
					
						}
					}
					
					object dummy = System.Reflection.Missing.Value;
					object what = Word.WdGoToItem.wdGoToLine;
					object which = Word.WdGoToDirection.wdGoToLast;
					object count = System.Reflection.Missing.Value;
//					WordApp.Selection.GoTo(ref oGoToItem, ref oGoToLast, ref Nothing, ref Nothing);
					WordApp.Selection.GoTo(ref what, ref which, ref count, ref dummy);
					//default 表示每行記錄之間插入分頁符,最后一行時(shí)不再插入分頁符,以免造成多余一空白頁
    				if(i!=dt.Rows.Count-1)
						{
							object ib = Word.WdBreakType.wdPageBreak; 
							WordApp.Selection.InsertBreak(ref ib);
						}
					}
	
             }
			WordDoc.Save();
            WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
			//關(guān)閉WordApp組件對象
			WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
 
}

調(diào)用示例

示例代碼如下:

DataSet rv = GetDataSet("select * from tabble");
 
ArrayList picconfig = new ArrayList();   //可配置每個(gè)圖片字段的大小及位置
picconfig.Add("ds_kg_sign_100_50_500_-20");   //格式為: key_width_height_left_top
string ModuleFile = "d:\\bfile\\word\\面試評(píng)分表.docx";  //假設(shè)的模板文件
string _lastfile = DataTableToWord(ModuleFile,"ds_",ds,picconfig);
 
MessageBox.Show(string.Format("已成功導(dǎo)出文件:{0}", _lastfile));

小結(jié)

核心代碼中需要將 Word.InlineShape 轉(zhuǎn)換為 Word.Shape 后,可以進(jìn)行環(huán)繞文字的操作,如下:

Word.Shape pic2 = pic.ConvertToShape();
pic2.WrapFormat.Type = Word.WdWrapType.wdWrapThrough;
pic2.WrapFormat.Type = Word.WdWrapType.wdWrapBehind;
pic2.Left = 0;
pic2.Top = 0;

使用 ConvertToShape() 方法轉(zhuǎn)換為 pic2 , pic2.WrapFormat.Type = Word.WdWrapType.wdWrapBehind;  可以浮動(dòng)于文字下方,然后初始圖片的 Left 和 Top ,否則有可能無法正常顯示位置。

我們可以根據(jù)自己的實(shí)際情況設(shè)置環(huán)繞格式

以上就是C#讀取多條數(shù)據(jù)記錄導(dǎo)出到Word之圖片輸出改造的詳細(xì)內(nèi)容,更多關(guān)于C#讀取數(shù)據(jù)并導(dǎo)出到Word的資料請關(guān)注腳本之家其它相關(guān)文章! 

相關(guān)文章

  • C#之協(xié)程的使用方式

    C#之協(xié)程的使用方式

    C#協(xié)程通過yield關(guān)鍵字實(shí)現(xiàn),用于簡化異步操作、迭代和狀態(tài)機(jī),優(yōu)點(diǎn)包括簡化異步編程、迭代和實(shí)現(xiàn)簡單狀態(tài)機(jī),但存在局限性和性能問題
    2026-02-02
  • C# Stack堆棧的使用方法

    C# Stack堆棧的使用方法

    最近打算學(xué)習(xí)下C# Stack堆棧的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2009-01-01
  • C#訪問SqlServer設(shè)置鏈接超時(shí)的方法

    C#訪問SqlServer設(shè)置鏈接超時(shí)的方法

    這篇文章主要介紹了C#訪問SqlServer設(shè)置鏈接超時(shí)的方法,涉及CommandTimeout屬性的相關(guān)設(shè)置技巧,非常簡單實(shí)用,需要的朋友可以參考下
    2015-06-06
  • C#操作Excel相關(guān)方法總結(jié)

    C#操作Excel相關(guān)方法總結(jié)

    這篇文章主要介紹了C#操作Excel相關(guān)方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • C#監(jiān)控文件夾并自動(dòng)給圖片文件打水印的方法

    C#監(jiān)控文件夾并自動(dòng)給圖片文件打水印的方法

    這篇文章主要介紹了C#監(jiān)控文件夾并自動(dòng)給圖片文件打水印的方法,涉及C#針對文件夾及圖片操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-05-05
  • C# 預(yù)處理指令(# 指令)的具體使用

    C# 預(yù)處理指令(# 指令)的具體使用

    本文主要介紹了C# 預(yù)處理指令(# 指令)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-12-12
  • C# 以MDF文件鏈接數(shù)據(jù)庫的示例代碼

    C# 以MDF文件鏈接數(shù)據(jù)庫的示例代碼

    本篇文章主要介紹了C# 以MDF文件鏈接數(shù)據(jù)庫的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • C#中的IDisposable模式用法詳解

    C#中的IDisposable模式用法詳解

    這篇文章主要介紹了C#中的IDisposable模式用法,講述了垃圾資源回收機(jī)制的實(shí)現(xiàn),并對比分析了Dispose()方法、~DisposableClass()析構(gòu)函數(shù)、虛方法Dispose(bool disposing)的原理,需要的朋友可以參考下
    2014-09-09
  • Winform窗體縮放下使用剪切板功能出現(xiàn)頁面閃動(dòng)解決分析

    Winform窗體縮放下使用剪切板功能出現(xiàn)頁面閃動(dòng)解決分析

    這篇文章主要介紹了Winform窗體縮放下使用剪切板功能出現(xiàn)頁面閃動(dòng)解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • c#線程同步使用詳解示例

    c#線程同步使用詳解示例

    這篇文章主要介紹了c#線程同步使用方法,介紹幾種常用的C#進(jìn)行線程同步的方式,需要的朋友可以參考下
    2014-02-02

最新評(píng)論

新巴尔虎左旗| 佛坪县| 万荣县| 涞源县| 内丘县| 勐海县| 马公市| 芒康县| 建昌县| 江华| 洪湖市| 株洲县| 固镇县| 孝昌县| 桑日县| 盐边县| 新丰县| 佛冈县| 湘潭市| 东方市| 广宁县| 容城县| 河南省| 五河县| 连南| 阜阳市| 那曲县| 平顶山市| 浮山县| 舒城县| 云阳县| 托克逊县| 伽师县| 龙里县| 固原市| 怀仁县| 泰安市| 南京市| 体育| 铁岭市| 明光市|