类库:
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++)
{
string _curCellData = _contentLine[i];
//如果是第一行,则直接填充
if (0 == i)
{
if (_tableCell.Elements
{
Paragraph _newParagraph = _tableCell.Elements
if (_newParagraph.Elements
{
ParagraphProperties _paragraphProper = _newParagraph.Elements
RunProperties _newRunProper = new RunProperties();
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);
}
}
}
///
Paragraph _paragraph = _tableCell.Elements
foreach (Run _run in _paragraph.Elements
{
_run.RemoveAllChildren();
}
}
///
_oxe.RemoveAllChildren
_oxe.AppendChild
break;
case "SdtRun":
SdtRun _sdtRun = (SdtRun)_oxe;
_sdtRun.SdtProperties.RemoveAllChildren
RunProperties _runPro = (RunProperties)_sdtRun.Elements
Text _runText = new Text(_content);
Run _runAnother = _sdtRun.SdtContentRun.Elements
_runAnother.RemoveAllChildren();
_runAnother.AppendChild(_runPro);
_runAnother.AppendChild(_runText);
break;
case "Run":
_oxe.RemoveAllChildren();
_oxe.AppendChild(new Text(_content));
break;
}
}
}
}
应用:
using System;
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;
namespace DocDemo
{
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;
try
{
_wordPoc = WordprocessingDocument.Open(Server.MapPath("./testDoc.docx"), true);
MainDocumentPart _mainDocumentPart = _wordPoc.MainDocumentPart;
OpenXmlElement _oxe = _mainDocumentPart.Document.Body;
//_oxe.RemoveAllChildren();
//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);
}
int i = 0;
foreach (TestData _test in _testDataList)
{
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.MainDocumentPart.Document.Save();
_wordPoc.Close();
GC.Collect();
}
}
}