类库:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Drawing.Wordprocessing;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace ReportDoc for (int i = 0; i < _contentLine.Length; i++) RunProperties _newRunProper = new RunProperties(); Paragraph _paragraph = _tableCell.Elements _oxe.RemoveAllChildren RunProperties _runPro = (RunProperties)_sdtRun.Elements Run _runAnother = _sdtRun.SdtContentRun.Elements using System; namespace DocDemo try //_oxe.RemoveAllChildren(); int i = 0; foreach (TestData _test in _testDataList) } _wordPoc.MainDocumentPart.Document.Save(); GC.Collect();
{
///
/// 作者:HaoZhenHu
///
public class Report
{
///
/// 用于获取内容控件中指定的文档表
///
/// 内容控件
/// 标签
///
public static Table DOC_TAB_GetTable(ref OpenXmlElement _oxe, string _tag)
{
SdtBlock _tBlock = _oxe.Descendants
return _tBlock.Descendants().Single
();
}
///
/// 获取内容控件表中的行数
///
/// 文档表对象
///
public static int DOC_TAB_GetTableRowCount(Table _tb)
{
return _tb.Elements
}
///
/// 删除指定索引的行
///
/// 文档表对象
/// 删除行的索引
///
public static bool DOC_TAB_DeleteTableRow(ref Table _tb, int _index)
{
if (!(_index >= 0 && _index < _tb.Elements
{
return false;
}
try
{
_tb.RemoveChild(_tb.Elements
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return true;
}
///
/// 删除指定行 (重载)
///
/// 文档表
/// 文档表行
///
public static bool DOC_TAB_DeleteTableRow(ref Table _tb, ref TableRow _tableRow)
{
try
{
_tb.RemoveChild(_tableRow);
return true;
}
catch
{
return false;
}
}
///
/// 返回指定索引的行
///
/// 文档表对象
/// 指定行的索引
///
public static TableRow DOC_TAB_GetTableRow(ref Table _tb, int _index)
{
try
{
return _tb.Elements
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
///
/// 返回复制后的文档表行
///
/// 要被复制的文档表行
///
public static TableRow DOC_TAB_CloneTableRow(TableRow _tableRow)
{
return (TableRow)_tableRow.Clone();
}
///
/// 返回复制后的文档表行 (重载)
///
/// 文档表
/// 行所在索引
///
public TableRow DOC_TAB_CloneTableRow(Table _tb, int _index)
{
return (TableRow)_tb.Elements
}
///
/// 向文档中指定的单元格添加数据
///
/// 要添加的数据
/// 文档行
/// 单元格的索引
public static void DOC_TAB_FillCellData(string _content, ref TableRow _tableRow, int _index)
{
TableCell _tableCell = _tableRow.Elements
string[] _contentLine = System.Text.RegularExpressions.Regex.Split(_content, @"/r/n");
DOC_TAB_ClearTextTableCell(ref _tableCell);
{
string _curCellData = _contentLine[i];
//如果是第一行,则直接填充
if (0 == i)
{
if (_tableCell.Elements
{
Paragraph _newParagraph = _tableCell.Elements
if (_newParagraph.Elements
{
ParagraphProperties _paragraphProper = _newParagraph.Elements
if (_paragraphProper.Elements
{
ParagraphMarkRunProperties _paraMarkRunProper = _paragraphProper.Elements
for (int j = 0; j < _paraMarkRunProper.Elements().Count(); j++)
{
_newRunProper.Append(_paraMarkRunProper.Elements().ElementAt(j).CloneNode(true));
}
}
else
{
_newRunProper.Append(new RunFonts() { Hint = FontTypeHintValues.EastAsia });
}
_newParagraph.Append(new Run(_newRunProper, new Text(_curCellData) { Space = "preserve" }));
}
else
{
_newParagraph.Append(new ParagraphMarkRunProperties(new RunFonts(){ Hint = FontTypeHintValues.EastAsia }));
_newParagraph.Append(new Run(new RunProperties(new RunFonts() { Hint = FontTypeHintValues.EastAsia }), new Text(_curCellData) { Space = "preserve" }));
}
}
else
{
Paragraph _newParagraph = new Paragraph();
_newParagraph.Append(new ParagraphMarkRunProperties(new RunFonts().EastAsia));
_newParagraph.Append(new DocumentFormat.OpenXml.Wordprocessing.Run(new RunProperties(new RunFonts() { Hint = FontTypeHintValues.EastAsia }), new Text(_curCellData) { Space = "preserve" }));
}
}
else
{
Paragraph _paragraph = _tableCell.Elements
Paragraph _newParagraph = (Paragraph)_paragraph.Clone();
DocumentFormat.OpenXml.Wordprocessing.Run _newRun = _newParagraph.Elements
DocumentFormat.OpenXml.Wordprocessing.Text _newText = _newRun.Elements
_newText.Text = _curCellData;
_tableCell.Append(_newParagraph);
}
}
}
///
/// 在指定行后面添加新行
///
/// 新行
/// 文档表
/// 行索引
public static void DOC_TAB_InsertRowAfter(TableRow _newRow, ref Table _tb, int _index)
{
TableRow _tableRow = _tb.Elements
_tb.InsertAfter
}
///
/// 在指定行前面添加新行
///
/// 新行
/// 文档表
/// 行索引
public static void DOC_TAB_InsertRowBefore(TableRow _newRow, ref Table _tb, int _index)
{
TableRow _tableRow = _tb.Elements
_tb.InsertBefore
}
///
/// 清除单元格中的内容,但保留其格式
///
/// 单元格
public static void DOC_TAB_ClearTextTableCell(ref TableCell _tableCell)
{
for (int i = _tableCell.Elements
{
_tableCell.Elements
}
foreach (Run _run in _paragraph.Elements
{
_run.RemoveAllChildren();
}
}
///
/// 用于插入文本内容
///
/// 要填充内容的节点
/// 填充的内容
public static void DOC_TEXT_FillData(ref OpenXmlElement _oxe, string _content)
{
string _eType = _oxe.GetType().Name;
switch (_eType)
{
case "SdtBlock":
Paragraph _paragraph = (Paragraph)_oxe.Descendants
Run _run = (Run)_paragraph.Elements
_run.RemoveAllChildren
_run.AppendChild
_paragraph.AppendChild
_oxe.AppendChild
break;
case "SdtRun":
SdtRun _sdtRun = (SdtRun)_oxe;
_sdtRun.SdtProperties.RemoveAllChildren
Text _runText = new Text(_content);
_runAnother.RemoveAllChildren();
_runAnother.AppendChild(_runPro);
_runAnother.AppendChild(_runText);
break;
case "Run":
_oxe.RemoveAllChildren();
_oxe.AppendChild(new Text(_content));
break;
}
}
}
}
应用:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ReportDoc;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;
{
public partial class _Default : System.Web.UI.Page
{
public struct TestData
{
public int ID;
public string Name;
}
protected void Page_Load(object sender, EventArgs e)
{
List
TestData _myData;
_myData.ID = 1;
_myData.Name = "xiaohulove/r/ny";
_testDataList.Add(_myData);
_myData.ID = 2;
_myData.Name = "/r/nisxiaohu'shoney";
_testDataList.Add(_myData);
WordprocessingDocument _wordPoc = null;
{
_wordPoc = WordprocessingDocument.Open(Server.MapPath("./testDoc.docx"), true);
MainDocumentPart _mainDocumentPart = _wordPoc.MainDocumentPart;
OpenXmlElement _oxe = _mainDocumentPart.Document.Body;
//Paragraph _paragraph = new Paragraph();
//Run _newRun = new Run();
//_paragraph.AppendChild
//OpenXmlElement _element = _paragraph.Elements
//Report.DOC_TEXT_FillData(ref _element, "aaaaaaa");
//_oxe.AppendChild
DocumentFormat.OpenXml.Wordprocessing.Table _myTable = Report.DOC_TAB_GetTable(ref _oxe, "vs");
//int count = _myTable.Elements
int count = Report.DOC_TAB_GetTableRowCount(_myTable);
DocumentFormat.OpenXml.Wordprocessing.TableRow _templateTRow = Report.DOC_TAB_GetTableRow(ref _myTable, 1);
for (int j = count - 1; j > 0; j--)
{
//_myTable.RemoveChild(_myTable.Elements
Report.DOC_TAB_DeleteTableRow(ref _myTable, j);
}
{
DocumentFormat.OpenXml.Wordprocessing.TableRow _myRow = Report.DOC_TAB_CloneTableRow(_templateTRow);
Report.DOC_TAB_FillCellData(_test.ID.ToString(), ref _myRow, 0);
Report.DOC_TAB_FillCellData(_test.Name, ref _myRow, 1);
Report.DOC_TAB_InsertRowAfter(_myRow, ref _myTable, i);
i++;
}
catch
{
_wordPoc.Close();
GC.Collect();
Page.RegisterStartupScript("aaaaaaa", "");
return;
}
_wordPoc.Close();
}
}
}你可能感兴趣的:(.Net)