word模板生成word報表文檔

主要功能為根據word模板生成word報表文檔,注意引用Interop.Word.dll;
首先要生成word程序對象
Word.Application app = new Word.Application();
根據模板文件生成新文件框架
File.Copy(TemplateFile, FileName);
生成documnet對象
ord.Document doc = new Word.Document();
??????? 打開新文擋
??????? doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
??????????? ref missing, ref missing, ref missing, ref missing,
??????????? ref missing, ref missing, ref missing, ref Visible,
??????????? ref missing, ref missing, ref missing,
??????????? ref missing);
??????? doc.Activate();
將光標定位到新的書簽(模板中定義了書簽的位置),下面代碼為在光標位置輸出一行,然后回車
??????? //光標轉到書簽
??????? for (int bookIndex = 0; bookIndex < 5; bookIndex++)
??????? {
??????????? object BookMarkName = "BookMark" + bookIndex.ToString();
??????????? object what = Word.WdGoToItem.wdGoToBookmark;
??????????? doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
??????????? doc.ActiveWindow.Selection.TypeText("文明單位" + bookIndex.ToString() + "zaddd??? 25????? 大學");
??????????? doc.ActiveWindow.Selection.TypeParagraph();
??????? }
輸出完畢后,最后關閉doc對象
??????? object IsSave = true;
??????? doc.Close(ref IsSave, ref missing, ref missing);

完整事例代碼如下:

using?System;
using?System.IO;
using?System.Data;
using?System.Configuration;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Web.UI.HtmlControls;

public?partial?class?_Default?:?System.Web.UI.Page?
{
????
protected?void?Page_Load(object?sender,?EventArgs?e)
????{

????}
????
protected?void?Button1_Click(object?sender,?EventArgs?e)
????{
????????Word.Application?app?
=?new?Word.Application();
????????
//模板文件
????????string?TemplateFile?=?@"D:MyworkExcelReportsServerReportServerTempalteSmallList.doc";
????????
//生成的具有模板樣式的新文件
????????string?FileName?=?@"C:Documents?and?SettingsAdministrator桌面"?+?DateTime.Now.ToString("yyyyMMddHHmmssfffffff")+".doc";
????????
//模板文件拷貝到新文件
????????File.Copy(TemplateFile,?FileName);
????????Word.Document?doc?
=?new?Word.Document();
????????
object?Obj_FileName?=?FileName;
????????
object?Visible?=?false;
????????
object?ReadOnly?=?false;
????????
object?missing?=?System.Reflection.Missing.Value;
????????
//打開文件
????????doc?=?app.Documents.Open(ref?Obj_FileName,?ref?missing,?ref?ReadOnly,?ref?missing,
????????????
ref?missing,?ref?missing,?ref?missing,?ref?missing,
????????????
ref?missing,?ref?missing,?ref?missing,?ref?Visible,
????????????
ref?missing,?ref?missing,?ref?missing,
????????????
ref?missing);
????????doc.Activate();

????????
//光標轉到書簽
????????for?(int?bookIndex?=?0;?bookIndex?<?5;?bookIndex++)
????????{
????????????
object?BookMarkName?=?"BookMark"?+?bookIndex.ToString();
????????????
object?what?=?Word.WdGoToItem.wdGoToBookmark;
????????????doc.ActiveWindow.Selection.GoTo(
ref?what,?ref?missing,?ref?missing,?ref?BookMarkName);
????????????doc.ActiveWindow.Selection.TypeText(
"文明單位"?+?bookIndex.ToString()?+?"zaddd????25??????大學");
????????????doc.ActiveWindow.Selection.TypeParagraph();
????????}
????????
object?IsSave?=?true;
????????doc.Close(
ref?IsSave,?ref?missing,?ref?missing);


????????Response.Write(
"<script?language='javascript'>alert('生成模板成功!')</script>");
????}
}

?

附:
光標到 書簽Title 的位置
object BookMarkName="Title";
object what =Word.WdGoToItem.wdGoToBookmark;
Doc.ActiveWindow.Selection.GoTo(ref what ,ref missing,ref missing,ref BookMarkName);???????????????????????
在當前的光標寫文本
Doc.ActiveWindow.Selection.TypeText("變更通知");

當前的光標換行
Doc.ActiveWindow.Selection.TypeParagraph();

當前的光標設置格式(舉例 對齊方式)????????????????????????????????????????????????????????????? Doc.ActiveWindow.Selection.ParagraphFormat.Alignment=Word.WdParagraphAlignment.wdAlignParagraphRight;

注意 ParagraphFormat 是設置字體的格式的地方

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388919.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388919.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388919.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

isql 測試mysql連接_[libco] 協程庫學習,測試連接 mysql

歷史原因&#xff0c;一直使用 libev 作為服務底層&#xff1b;異步框架雖然性能比較高&#xff0c;但新人學習和使用門檻非常高&#xff0c;而且串行的邏輯被打散為狀態機&#xff0c;這也會嚴重影響生產效率。用同步方式實現異步功能&#xff0c;既保證了異步性能優勢&#x…

什么是數據倉庫,何時以及為什么要考慮一個

The term “Data Warehouse” is widely used in the data analytics world, however, it’s quite common for people who are new with data analytics to ask the above question.術語“數據倉庫”在數據分析領域中被廣泛使用&#xff0c;但是&#xff0c;對于數據分析新手來…

安裝好MongoDB,但服務中沒有MongoDB服務的解決辦法

以管理員身份打開CMD&#xff0c;添加路徑添加服務即可 winX 然后再選Amongod -dbpath "D:\MongoDB\Server\3.6\data\db" -logpath "D:\MongoDB\Server\3.6\data\log\mongo.log" -install -serviceName "MongoDB"轉載于:https://www.cnblogs.com…

DRF數據驗證+數據存儲

1.驗證數據的自定義類 class BooksDRFt(serializers.ModelSerializer):class Meta:model Bookfields __all__#要驗證的字段author serializers.CharField(requiredFalse)#要驗證的字段name serializers.CharField(min_length2, error_messages{required: 不能為空, min_len…

mysql變量 exec_MySQL slave_exec_mode 參數說明

背景&#xff1a;今天無意當中看到參數slave_exec_mode&#xff0c;從手冊里的說明看出該參數和MySQL復制相關&#xff0c;是可以動態修改的變量&#xff0c;默認是STRICT模式(嚴格模式)&#xff0c;可選值有IDEMPOTENT模式(冪等模式)。設置成IDEMPOTENT模式可以讓從庫避免1032…

C#word

主要功能為根據word模板生成word報表文檔,注意引用Interop.Word.dll;首先要生成word程序對象Word.Application app new Word.Application();根據模板文件生成新文件框架File.Copy(TemplateFile, FileName);生成documnet對象ord.Document doc new Word.Document(); 打開…

機器學習kaggle競賽實戰-泰坦尼克號

數據展示 首先登kaggle 下載泰坦尼克訓練相關數據 import pandas as pd import numpy as np data pd.read_csv(train.csv) print(data.shape) print(data.head) train data[:800] test data[800:] print(train.shape) print(test.shape)選擇特征 selected_features [Pcl…

上海大都會 H.A Simple Problem with Integers

題目描述 You have N integers A1, A2, ... , AN. You are asked to write a program to receive and execute two kinds of instructions: C a b means performing Ai (Ai2 mod 2018) for all Ai such that a ≤ i ≤ b.Q a b means query the sum of Aa, Aa1, ..., Ab. Note…

探索性數據分析入門_入門指南:R中的探索性數據分析

探索性數據分析入門When I started on my journey to learn data science, I read through multiple articles that stressed the importance of understanding your data. It didn’t make sense to me. I was naive enough to think that we are handed over data which we p…

用Javascript代碼實現瀏覽器菜單命令(以下代碼在 Windows XP下的瀏覽器中調試通過

每當我們看到別人網頁上的打開、打印、前進、另存為、后退、關閉本窗口、禁用右鍵等實現瀏覽器命令的鏈接&#xff0c;而自己苦于不能實現時&#xff0c;是不是感到很遺憾&#xff1f;是不是也想實現&#xff1f;如果能在網頁上能實現瀏覽器的命令&#xff0c;將是多么有意思的…

mysql程序設計教程_MySQL教程_編程入門教程_牛客網

MySQL 索引MySQL索引的建立對于MySQL的高效運行是很重要的&#xff0c;索引可以大大提高MySQL的檢索速度。打個比方&#xff0c;如果合理的設計且使用索引的MySQL是一輛蘭博基尼的話&#xff0c;那么沒有設計和使用索引的MySQL就是一個人力三輪車。拿漢語字典的目錄頁(索引)打比…

學習筆記整理之StringBuffer與StringBulider的線程安全與線程不安全

關于線程和線程不安全&#xff1a; 概述 編輯 如果你的代碼所在的進程中有多個線程在同時運行&#xff0c;而這些線程可能會同時運行這段代碼。如果每次運行結果和單線程運行的結果是一樣的&#xff0c;而且其他的變量的值也和預期的是一樣的&#xff0c;就是線程安全的。或者說…

python web應用_為您的應用選擇最佳的Python Web爬網庫

python web應用Living in today’s world, we are surrounded by different data all around us. The ability to collect and use this data in our projects is a must-have skill for every data scientist.生活在當今世界中&#xff0c;我們周圍遍布著不同的數據。 在我們的…

NDK-r14b + FFmpeg-release-3.4 linux下編譯FFmpeg

下載資源 官網下載完NDK14b 和 FFmpeg 下載之后&#xff0c;更改FFmpeg 目錄下configure問價如下&#xff1a; SLIBNAME_WITH_MAJOR$(SLIBPREF)$(FULLNAME)-$(LIBMAJOR)$(SLIBSUF) LIB_INSTALL_EXTRA_CMD$$(RANLIB)"$(LIBDIR)/$(LIBNAME)" SLIB_INSTALL_NAME$(SLI…

C# WebBrowser自動填表與提交

C# WebBrowser自動填表與提交 默認分類 2007-04-18 15:47:17 閱讀57 評論0 字號&#xff1a;大中小 訂閱 要使我們的WebBrowser具有自動填表、甚至自動提交的功能&#xff0c;并不困難。   假設有一個最簡單的登錄頁面&#xff0c;輸入用戶名密碼&#xff0c;點“登錄”…

html中列表導航怎么和圖片對齊_HTML實戰篇:html仿百度首頁

本篇文章主要給大家介紹一下如何使用htmlcss來制作百度首頁頁面。1)制作頁面所用的知識點我們首先來分析一下百度首頁的頁面效果圖百度首頁由頭部的一個文字導航&#xff0c;中間的一個按鈕和一個輸入框以及下邊的文字簡介和導航組成。我們這里主要用到的知識點就是列表標簽(ul…

C# 依賴注入那些事兒

原文地址&#xff1a;http://www.cnblogs.com/leoo2sk/archive/2009/06/17/1504693.html 里面有一個例子差了些代碼&#xff0c;補全后貼上。 3.1.3 依賴獲取 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml;//定義…

asp.net core Serilog的使用

先貼上關于使用這個日志組件的一些使用方法&#xff0c;等有時間了在吧官方的文檔翻譯一下吧&#xff0c;現在真是沒時間。 Serilog在使用上主要分為兩大塊&#xff1a; 第一塊是主庫&#xff0c;包括Serilog以及Serilog.AspNetCore&#xff0c;如果導入后一個的話會自動導入前…

在FAANG面試中破解堆算法

In FAANG company interview, Candidates always come across heap problems. There is one question they do like to ask — Top K. Because these companies have a huge dataset and they can’t always go through all the data. Finding tope data is always a good opti…

android webView的緩存機制和資源預加載

android 原生使用WebView嵌入H5頁面 Hybrid開發 一、性能問題 android webview 里H5加載速度慢網絡流量大 1、H5頁面加載速度慢 渲染速度慢 js解析效率 js本身的解析過程復雜、解析速度不快&#xff0c;前端頁面設計較多的js代碼文件 手機硬件設備的性能 機型多&#xff0c;…