You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

346 lines
17 KiB

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Word;
namespace OT.Web.Ap_Code
{
public class WordReport
{
private _Application wordApp = null;
private _Document wordDoc = null;
public _Application Application
{
get { return wordApp; }
set
{ wordApp = value; }
}
public _Document Document
{
get
{
return wordDoc;
}
set
{
wordDoc = value;
}
}
public enum Alignment
{
left,
center,
right
}
//通過模板創建新文檔
public void CreateNewDocument(string filePath)
{
killWinWordProcess();
wordApp = new ApplicationClass();
wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
wordApp.Visible = false;
object missing = System.Reflection.Missing.Value;
object templateName = filePath;
wordDoc = wordApp.Documents.Open(ref templateName, 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);
}
//保存新文件 
public void SaveDocument(string filePath)
{
object fileName = filePath;
object format = WdSaveFormat.wdFormatDocument;
//保存格式             
object miss = System.Reflection.Missing.Value;
wordDoc.SaveAs(ref fileName, ref format, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss, ref miss, ref miss, ref miss,
ref miss);
//關閉wordDoc,wordApp對象 
object SaveChanges = WdSaveOptions.wdSaveChanges;
object OriginalFormat = WdOriginalFormat.wdOriginalDocumentFormat;
object RouteDocument = false;
wordDoc.Close(ref SaveChanges, ref OriginalFormat, ref RouteDocument); wordApp.Quit(ref SaveChanges, ref OriginalFormat, ref RouteDocument);
}
//在書籤處插入值
public bool InsertValue(string bookmark, string value)
{
object bkObj = bookmark;
if (wordApp.ActiveDocument.Bookmarks.Exists(bookmark))
{
wordApp.ActiveDocument.Bookmarks.get_Item(ref bkObj).Select();
wordApp.Selection.TypeText(value);
return true;
}
return false;
}
//插入表格,bookmark書籤
public Table InsertTable(string bookmark, int rows, int columns, float width)
{
object miss = System.Reflection.Missing.Value;
object oStart = bookmark;
Range range = wordDoc.Bookmarks.get_Item(ref oStart).Range;
//表格插入位置
Table newTable = wordDoc.Tables.Add(range, rows, columns, ref miss, ref miss);
//設置表的格式
newTable.Borders.Enable = 1;
//允許有邊框,默認沒有邊框(為0時報錯,1為實線邊框,2、3為虛線邊框,以後的數字沒試過)
newTable.Borders.OutsideLineWidth = WdLineWidth.wdLineWidth050pt;
//邊框寬度
if (width != 0)
{
newTable.PreferredWidth = width;
//表格寬度
}
newTable.AllowPageBreaks = false;
return newTable;
}
//合併單元格 表名,開始行號,開始列號,結束行號,結束列號
public void MergeCell(Microsoft.Office.Interop.Word.Table table, int row1, int column1, int row2, int column2)
{
table.Cell(row1, column1).Merge(table.Cell(row2, column2));
}
//設置表格內容對齊方式 Align水平方向,Vertical垂直方向(左對齊,居中對齊,右對齊分別對應Align和Vertical的值為-1,0,1)
public void SetParagraph_Table(Microsoft.Office.Interop.Word.Table table, int Align, int Vertical)
{
switch (Align)
{
case -1: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
break;
//左對齊
case 0: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
break;
//水平居中
case 1: table.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphRight; break;
//右對齊
}
switch (Vertical)
{
case -1: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalTop;
break;
//頂端對齊
case 0: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalCenter;
break;
//垂直居中
case 1: table.Range.Cells.VerticalAlignment = WdCellVerticalAlignment.wdCellAlignVerticalBottom;
break;
//底端對齊
}
}
//設置表格字體
public void SetFont_Table(Microsoft.Office.Interop.Word.Table table, string fontName, double size)
{
if (size != 0)
{
table.Range.Font.Size = Convert.ToSingle(size);
}
if (fontName != "")
{
table.Range.Font.Name = fontName;
}
}
//是否使用邊框,n表格的序號,use是或否
public void UseBorder(int n, bool use)
{
if (use)
{
wordDoc.Content.Tables[n].Borders.Enable = 1;
//允許有邊框,默認沒有邊框(為0時報錯,1為實線邊框,2、3為虛線邊框,以後的數字沒試過)
}
else
{
wordDoc.Content.Tables[n].Borders.Enable = 2;
//允許有邊框,默認沒有邊框(為0時報錯,1為實線邊框,2、3為虛線邊框,以後的數字沒試過)
}
}
//給表格插入一行,n表格的序號從1開始記
public void AddRow(int n)
{
object miss = System.Reflection.Missing.Value;
wordDoc.Content.Tables[n].Rows.Add(ref miss);
}
//給表格添加一行
public void AddRow(Microsoft.Office.Interop.Word.Table table)
{
object miss = System.Reflection.Missing.Value;
table.Rows.Add(ref miss);
}
//給表格插入rows行,n為表格的序號         
public void AddRow(int n, int rows)
{
object miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n];
for (int i = 0; i < rows; i++)
{
table.Rows.Add(ref miss);
}
}
//給表格中單元格插入元素,table所在表格,row行號,column列號,value插入的元素 
public void InsertCell(Microsoft.Office.Interop.Word.Table table, int row, int column, string value)
{
table.Cell(row, column).Range.Text = value;
}
//給表格中單元格插入元素,n表格的序號從1開始記,row行號,column列號,value插入的元素         
public void InsertCell(int n, int row, int column, string value)
{
wordDoc.Content.Tables[n].Cell(row, column).Range.Text = value;
}
//定制,加上ALIGN控制
public void InsertCell(int n, int row, int column, string value,Alignment align)
{
if (align == Alignment.left)
{
wordDoc.Content.Tables[n].Cell(row, column).Range.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
wordDoc.Content.Tables[n].Cell(row, column).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
wordDoc.Content.Tables[n].Cell(row, column).Range.Text = value;
}
if (align == Alignment.center)
{
wordDoc.Content.Tables[n].Cell(row, column).Range.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
wordDoc.Content.Tables[n].Cell(row, column).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
wordDoc.Content.Tables[n].Cell(row, column).Range.Text = value;
}
if (align == Alignment.right)
{
wordDoc.Content.Tables[n].Cell(row, column).Range.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
wordDoc.Content.Tables[n].Cell(row, column).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
wordDoc.Content.Tables[n].Cell(row, column).Range.Text = value;
}
}
//給表格插入一行數據,n為表格的序號,row行號,columns列數,values插入的值
public void InsertCell(int n, int row, int columns, string[] values)
{
Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n];
for (int i = 0; i < columns; i++)
{
table.Cell(row, i + 1).Range.Text = values[i];
}
}
//訂制報價單格式
public void InsertCellForQuotationRptHead(int n, int row, int columns, string[] values)
{
Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n];
for (int i = 0; i < columns; i++)
{
if (i == 0 || i == 3)
{
table.Cell(row, i + 1).Range.Cells.Width = 38f;
}
if (i == 1 || i==5)
{
table.Cell(row, i + 1).Range.Cells.Width = 144f;
}
if (i == 2 || i==4)
{
table.Cell(row, i + 1).Range.Cells.Width = 64f;
}
table.Cell(row, i + 1).Range.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
table.Cell(row, i + 1).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
table.Cell(row, i + 1).Range.Text = values[i];
}
}
public void InsertCellForQuotationRptContent(int n, int row, int columns, string[] values)
{
Microsoft.Office.Interop.Word.Table table = wordDoc.Content.Tables[n];
for (int i = 0; i < columns; i++)
{
if (i == 0 || i == 3)
{
table.Cell(row, i + 1).Range.Cells.Width = 38f;
table.Cell(row, i + 1).Range.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
table.Cell(row, i + 1).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
table.Cell(row, i + 1).Range.Text = values[i];
}
if (i == 1 || i == 5)
{
table.Cell(row, i + 1).Range.Cells.Width = 144f;
table.Cell(row, i + 1).Range.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
table.Cell(row, i + 1).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
table.Cell(row, i + 1).Range.Text = values[i];
}
if (i == 2)
{
if (values[i].ToString().IndexOf("\r\n") != -1)
{
table.Cell(row, i + 1).Range.Cells.Width = 64f;
table.Cell(row, i + 1).Range.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
table.Cell(row, i + 1).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
string[] arrContent = values[i].ToString().Replace("\r\n", "@").Split('@');
//table.Cell(row, i + 1).Range.Text = arrContent[0];
table.Cell(row, i + 1).Range.Select();
object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
object i1 = arrContent[1].Length;
object extend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
wordApp.Selection.TypeText("\r\n" + arrContent[1]);
wordApp.Selection.HomeKey(ref unit, ref extend);
wordApp.Selection.Font.StrikeThrough = 0;
table.Cell(row, i + 1).Range.Text += "";
wordApp.Selection.TypeText(arrContent[0]);
wordApp.Selection.HomeKey(ref unit, ref extend);
wordApp.Selection.Font.StrikeThrough = 1;
}
else
{
table.Cell(row, i + 1).Range.Cells.Width = 64f;
table.Cell(row, i + 1).Range.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
table.Cell(row, i + 1).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
table.Cell(row, i + 1).Range.Text = values[i];
}
}
if (i == 4)
{
table.Cell(row, i + 1).Range.Cells.Width = 64f;
table.Cell(row, i + 1).Range.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;
table.Cell(row, i + 1).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
table.Cell(row, i + 1).Range.Text = values[i];
}
}
}
//插入圖片
public void InsertPicture(string bookmark, string picturePath, float width, float hight)
{
object miss = System.Reflection.Missing.Value;
object oStart = bookmark;
Object linkToFile = false;
//圖片是否為外部鏈接
Object saveWithDocument = true;
//圖片是否隨文檔一起保存
object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;
//圖片插入位置
wordDoc.InlineShapes.AddPicture(picturePath, ref linkToFile, ref saveWithDocument, ref range);
wordDoc.Application.ActiveDocument.InlineShapes[1].Width = width;
//設置圖片寬度
wordDoc.Application.ActiveDocument.InlineShapes[1].Height = hight;
//設置圖片高度
}
//插入一段文字,text為文字內容
public void InsertText(string bookmark, string text)
{
object oStart = bookmark;
object range = wordDoc.Bookmarks.get_Item(ref oStart).Range;
Paragraph wp = wordDoc.Content.Paragraphs.Add(ref range);
wp.Format.SpaceBefore = 6;
wp.Range.Text = text;
wp.Format.SpaceAfter = 24;
wp.Range.InsertParagraphAfter();
wordDoc.Paragraphs.Last.Range.Text = "\n";
}
// 殺掉winword.exe進程
public void killWinWordProcess()
{
System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName("WINWORD");
foreach (System.Diagnostics.Process process in processes)
{
bool b = process.MainWindowTitle == "";
if (process.MainWindowTitle == "")
{
process.Kill();
}
}
}
}
}