WordReportDocX

public static void ReplaceWithImage(DocX document, string tagBody, string replacement, int width, int height)
{

foreach (var paragraph in document.Paragraphs)
{
paragraph.FindAll(tagBody).ForEach(index =>
{
paragraph.RemoveText(index, tagBody.Length - 1);
try
{
Image img = document.AddImage(replacement);
var picture = img.CreatePicture(height, width);
paragraph.InsertPicture(picture, index + 1);
}
catch
{
}
paragraph.RemoveText(index, 1);
});
}

Header oddHeader = document.Headers.odd;
if (oddHeader != null)
{
foreach (var paragraph in oddHeader.Paragraphs)
{
paragraph.FindAll(tagBody).ForEach(index =>
{
paragraph.RemoveText(index, tagBody.Length - 1);
try
{
Image img = document.AddImage(replacement);
var picture = img.CreatePicture(height, width);
paragraph.InsertPicture(picture, index + 1);
}
catch
{
}
paragraph.RemoveText(index, 1);
});
}
}
Header evenHeader = document.Headers.even;
if (evenHeader != null)
{
foreach (var paragraph in evenHeader.Paragraphs)
{
paragraph.FindAll(tagBody).ForEach(index =>
{
paragraph.RemoveText(index, tagBody.Length - 1);
try
{
Image img = document.AddImage(replacement);
var picture = img.CreatePicture(height, width);
paragraph.InsertPicture(picture, index + 1);
}
catch
{
}
paragraph.RemoveText(index, 1);
});
}
}


Footer oddFooter = document.Footers.odd;
if (oddFooter != null)
{
foreach (var paragraph in oddFooter.Paragraphs)
{
paragraph.FindAll(tagBody).ForEach(index =>
{
paragraph.RemoveText(index, tagBody.Length - 1);
try
{
Image img = document.AddImage(replacement);
var picture = img.CreatePicture(height, width);
paragraph.InsertPicture(picture, index + 1);
}
catch
{
}
paragraph.RemoveText(index, 1);
});
}
}

Footer evenFooter = document.Footers.even;
if (evenFooter != null)
{
foreach (var paragraph in evenFooter.Paragraphs)
{
paragraph.FindAll(tagBody).ForEach(index =>
{
paragraph.RemoveText(index, tagBody.Length - 1);
try
{
Image img = document.AddImage(replacement);
var picture = img.CreatePicture(height, width);
paragraph.InsertPicture(picture, index + 1);
}
catch
{
}
paragraph.RemoveText(index, 1);
});
}
}
}

public static Table ReplaceWithTable(DocX document, string tagBody, int row, int col)
{
Table tb = null;
foreach (var paragraph in document.Paragraphs)
{
paragraph.FindAll(tagBody).ForEach(index =>
{
paragraph.RemoveText(index, tagBody.Length - 1);
try
{
tb = paragraph.InsertTableAfterSelf(document.AddTable(row, col));
}
catch
{
}
paragraph.RemoveText(index, 1);
});
}
return tb;

}
//通过模板创建新文档
public static DocX CreateTemplateDocument(string filePath, string TemplateFile)
{
DocX wordDoc = DocX.Create(filePath, DocumentTypes.Document);
try
{
using (FileStream file = new FileStream(TemplateFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
wordDoc.ApplyTemplate(file);
file.Close();
return wordDoc;
}
}
catch
{
return null;
}
}
//通过模板创建新文档
public static DocX LoadTemplateDocument(string TemplateFile)
{

try
{
using (FileStream file = new FileStream(TemplateFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
DocX wordDoc = DocX.Load(file);
file.Close();
return wordDoc;
}
}
catch
{
return null;
}
}
//通过模板创建新文档
public static DocX CreateDocument(string filePath)
{
try
{
DocX wordDoc = DocX.Create(filePath, DocumentTypes.Document);
return wordDoc;
}
catch
{
return null;
}
}
//保存新文件
public static void SaveDocument(DocX wordDoc)
{
try
{
wordDoc.Save();
}
catch
{
}
}


//插入表格,bookmark书签
public static Table InsertTable(DocX wordDoc, string bookmark, int rows, int columns, float width)
{
try
{
return ReplaceWithTable(wordDoc, bookmark, rows, columns);

}
catch
{
return null;
}
}

//给表格插入rows行,n为表格的序号
public static void AddRow(DocX wordDoc, int n, int rows)
{
try
{
n = n - 1;
if (n < 0)
{
return;
}
if (n >= wordDoc.Tables.Count)
{
return;
}
for (int i = 0; i < rows; i++)
{
try
{
wordDoc.Tables[n].InsertRow(wordDoc.Tables[n].Rows[1]);
}
catch
{
wordDoc.Tables[n].InsertRow();
}
}
}
catch
{
}
}
//给表格插入rows行,n为表格的序号
public static void AddRow(DocX wordDoc, int n, int start, int rows)
{
try
{
n = n - 1;
if (n < 0)
{
return;
}
if (n >= wordDoc.Tables.Count)
{
return;
}
for (int i = 0; i < rows; i++)
{
wordDoc.Tables[n].InsertRow(wordDoc.Tables[n].Rows[start-1], start);
}
}
catch
{
}
}

//给表格中单元格插入元素,table所在表格,row行号,column列号,value插入的元素
public static void InsertCell(DocX wordDoc, int n, int row, int column, string value, string bgcolor, string color)
{
try
{
n = n - 1;
if (n < 0)
{
return;
}
if (n >= wordDoc.Tables.Count)
{
return;
}

wordDoc.Tables[n].Rows[row - 1].Cells[column - 1].RemoveParagraphAt(0);
Paragraph p = wordDoc.Tables[n].Rows[row - 1].Cells[column - 1].InsertParagraph(value);
}
catch
{
}
}

//给表格中单元格插入元素,n表格的序号从1开始记,row行号,column列号,value插入的元素
public static void InsertCell(DocX wordDoc, int n, int row, int column, string value)
{
try
{
n = n - 1;
if (n < 0)
{
return;
}
if (n >= wordDoc.Tables.Count)
{
return;
}

wordDoc.Tables[n].Rows[row - 1].Cells[column - 1].RemoveParagraphAt(0);
Paragraph p = wordDoc.Tables[n].Rows[row - 1].Cells[column - 1].InsertParagraph(value);
}
catch
{
}
}

/// <summary>
/// 表格内居中Alignmentcenter
/// </summary>
/// <param name="wordDoc"></param>
/// <param name="n"></param>
/// <param name="row"></param>
/// <param name="column"></param>
/// <param name="value"></param>
public static void InsertCellAC(DocX wordDoc, int n, int row, int column, string value)
{
try
{
n = n - 1;
if (n < 0)
{
return;
}
if (n >= wordDoc.Tables.Count)
{
return;
}

Cell c = wordDoc.Tables[n].Rows[row - 1].Cells[column - 1];

c.RemoveParagraphAt(0);
c.VerticalAlignment = VerticalAlignment.Center;
c.InsertParagraph(value);
c.Paragraphs[0].Alignment = Alignment.center;

}
catch
{
}
}

//给表格插入rows行,n为表格的序号
public static void RemoveRow(DocX wordDoc, int n, int rows)
{
try
{
n = n - 1;
if (n < 0)
{
return;
}
if (n >= wordDoc.Tables.Count)
{
return;
}

wordDoc.Tables[n].RemoveRow(rows);
}
catch
{
}
}
//插入图片
public static void InsertPicture(DocX wordDoc, string bookmark, string picturePath, int width, int hight)
{
try
{
ReplaceWithImage(wordDoc, bookmark, picturePath, width, hight);
}
catch
{
}

}

//插入一段文字,text为文字内容
public static void InsertText(DocX wordDoc, string bookmark, string text)
{
try
{
wordDoc.ReplaceText(bookmark, text);
}
catch
{

}
}

//插入一段文字,text为文字内容
public static void InsertWordFile(DocX wordDoc, string bookmark, string WordFileName, string outFilePath)
{
try
{
wordDoc.InsertParagraph().InsertPageBreakAfterSelf();
wordDoc.InsertDocument( DocX.Load( WordFileName));
}
catch
{

}
}

//插入一段文字,text为文字内容
public static void InsertWordFile(DocX wordDoc, DocX InsertDoc)
{
try
{
wordDoc.InsertDocument(InsertDoc);
// wordDoc.InsertParagraph().InsertPageBreakAfterSelf();
}
catch
{

}
}
/// <summary>
/// 提供下载
/// </summary>
/// <param name="path"></param>
/// <param name="page"></param>
/// <param name="isDelete"></param>
public static void UploadWord(string path, System.Web.UI.Page page, bool isDelete)
{
try
{
System.IO.FileInfo file = new System.IO.FileInfo(path);
page.Response.Clear();
page.Response.Charset = "GB2312";
page.Response.ContentEncoding = System.Text.Encoding.UTF8;
// 添加头信息,为"文件下载/另存为"对话框指定默认文件名
page.Response.AddHeader("Content-Disposition", "attachment; filename=" + page.Server.UrlEncode(file.Name));
// 添加头信息,指定文件大小,让浏览器能够显示下载进度
page.Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一个不能被客户端读取的流,必须被下载
page.Response.ContentType = "application/ms-excel";
// 把文件流发送到客户端
page.Response.WriteFile(file.FullName);
page.Response.Flush();

if (isDelete)
{
System.IO.File.Delete(path);
}
// 停止页面的执行
page.Response.End();
}
catch
{
}
}

原文地址:https://www.cnblogs.com/sanshengshitouhua/p/14354931.html