本文整理汇总了C#中DocumentFormat.Elements方法的典型用法代码示例。如果您正苦于以下问题:C# DocumentFormat.Elements方法的具体用法?C# DocumentFormat.Elements怎么用?C# DocumentFormat.Elements使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DocumentFormat
的用法示例。
在下文中一共展示了DocumentFormat.Elements方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetCellValue
/// <summary>
/// Sets a cell value. The row and the cell are created if they do not exist. If the cell exists, the contents of the cell is overwritten
/// </summary>
/// <param name="spreadsheet">Spreadsheet to use</param>
/// <param name="worksheet">Worksheet to use</param>
/// <param name="columnIndex">Index of the column</param>
/// <param name="rowIndex">Index of the row</param>
/// <param name="valueType">Type of the value</param>
/// <param name="value">The actual value</param>
/// <param name="styleIndex">Index of the style to use. Null if no style is to be defined</param>
/// <param name="save">Save the worksheet?</param>
/// <returns>True if succesful</returns>
private static bool SetCellValue(DocumentFormat.OpenXml.Packaging.SpreadsheetDocument spreadsheet, DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet, uint columnIndex, uint rowIndex, DocumentFormat.OpenXml.Spreadsheet.CellValues valueType, string value, uint? styleIndex, bool save = true)
{
DocumentFormat.OpenXml.Spreadsheet.SheetData sheetData = worksheet.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.SheetData>();
DocumentFormat.OpenXml.Spreadsheet.Row row;
DocumentFormat.OpenXml.Spreadsheet.Row previousRow = null;
DocumentFormat.OpenXml.Spreadsheet.Cell cell;
DocumentFormat.OpenXml.Spreadsheet.Cell previousCell = null;
DocumentFormat.OpenXml.Spreadsheet.Columns columns;
DocumentFormat.OpenXml.Spreadsheet.Column previousColumn = null;
string cellAddress = Excel.ColumnNameFromIndex(columnIndex) + rowIndex;
// Check if the row exists, create if necessary
if (sheetData.Elements<DocumentFormat.OpenXml.Spreadsheet.Row>().Where(item => item.RowIndex == rowIndex).Count() != 0)
{
row = sheetData.Elements<DocumentFormat.OpenXml.Spreadsheet.Row>().Where(item => item.RowIndex == rowIndex).First();
}
else
{
row = new DocumentFormat.OpenXml.Spreadsheet.Row() { RowIndex = rowIndex };
//sheetData.Append(row);
for (uint counter = rowIndex - 1; counter > 0; counter--)
{
previousRow = sheetData.Elements<DocumentFormat.OpenXml.Spreadsheet.Row>().Where(item => item.RowIndex == counter).FirstOrDefault();
if (previousRow != null)
{
break;
}
}
sheetData.InsertAfter(row, previousRow);
}
// Check if the cell exists, create if necessary
if (row.Elements<DocumentFormat.OpenXml.Spreadsheet.Cell>().Where(item => item.CellReference.Value == cellAddress).Count() > 0)
{
cell = row.Elements<DocumentFormat.OpenXml.Spreadsheet.Cell>().Where(item => item.CellReference.Value == cellAddress).First();
}
else
{
// Find the previous existing cell in the row
for (uint counter = columnIndex - 1; counter > 0; counter--)
{
previousCell = row.Elements<DocumentFormat.OpenXml.Spreadsheet.Cell>().Where(item => item.CellReference.Value == Excel.ColumnNameFromIndex(counter) + rowIndex).FirstOrDefault();
if (previousCell != null)
{
break;
}
}
cell = new DocumentFormat.OpenXml.Spreadsheet.Cell() { CellReference = cellAddress };
row.InsertAfter(cell, previousCell);
}
// Check if the column collection exists
columns = worksheet.Elements<DocumentFormat.OpenXml.Spreadsheet.Columns>().FirstOrDefault();
if (columns == null)
{
columns = worksheet.InsertAt(new DocumentFormat.OpenXml.Spreadsheet.Columns(), 0);
}
// Check if the column exists
if (columns.Elements<DocumentFormat.OpenXml.Spreadsheet.Column>().Where(item => item.Min == columnIndex).Count() == 0)
{
// Find the previous existing column in the columns
for (uint counter = columnIndex - 1; counter > 0; counter--)
{
previousColumn = columns.Elements<DocumentFormat.OpenXml.Spreadsheet.Column>().Where(item => item.Min == counter).FirstOrDefault();
if (previousColumn != null)
{
break;
}
}
columns.InsertAfter(
new DocumentFormat.OpenXml.Spreadsheet.Column()
{
Min = columnIndex,
Max = columnIndex,
CustomWidth = true,
Width = 9
}, previousColumn);
}
// Add the value
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(value);
if (styleIndex != null)
{
cell.StyleIndex = styleIndex;
}
if (valueType != DocumentFormat.OpenXml.Spreadsheet.CellValues.Date)
{
cell.DataType = new DocumentFormat.OpenXml.EnumValue<DocumentFormat.OpenXml.Spreadsheet.CellValues>(valueType);
//.........这里部分代码省略.........
示例2: AddCommentOnParagraph
//Добавление комментария
public void AddCommentOnParagraph(DocumentFormat.OpenXml.Wordprocessing.Paragraph comPar, string comment)
{
DocumentFormat.OpenXml.Wordprocessing.Comments comments = null;
string id = "0";
// Verify that the document contains a
// WordProcessingCommentsPart part; if not, add a new one.
if (document.MainDocumentPart.GetPartsCountOfType<WordprocessingCommentsPart>() > 0)
{
comments =
document.MainDocumentPart.WordprocessingCommentsPart.Comments;
if (comments.HasChildren == true)
{
// Obtain an unused ID.
id = comments.Descendants<DocumentFormat.OpenXml.Wordprocessing.Comment>().Select(e => e.Id.Value).Max() + 1;
}
}
else
{
// No WordprocessingCommentsPart part exists, so add one to the package.
WordprocessingCommentsPart commentPart = document.MainDocumentPart.AddNewPart<WordprocessingCommentsPart>();
commentPart.Comments = new DocumentFormat.OpenXml.Wordprocessing.Comments();
comments = commentPart.Comments;
}
// Compose a new Comment and add it to the Comments part.
DocumentFormat.OpenXml.Wordprocessing.Paragraph p = new DocumentFormat.OpenXml.Wordprocessing.Paragraph(new DocumentFormat.OpenXml.Wordprocessing.Run(new Text(comment)));
DocumentFormat.OpenXml.Wordprocessing.Comment cmt =
new DocumentFormat.OpenXml.Wordprocessing.Comment()
{
Id = id,
Author = "FRChecking System",
Date = DateTime.Now
};
cmt.AppendChild(p);
comments.AppendChild(cmt);
comments.Save();
// Specify the text range for the Comment.
// Insert the new CommentRangeStart before the first run of paragraph.
comPar.InsertBefore(new CommentRangeStart() { Id = id }, comPar.GetFirstChild<DocumentFormat.OpenXml.Wordprocessing.Run>());
// Insert the new CommentRangeEnd after last run of paragraph.
var cmtEnd = comPar.InsertAfter(new CommentRangeEnd() { Id = id }, comPar.Elements<DocumentFormat.OpenXml.Wordprocessing.Run>().Last());
// Compose a run with CommentReference and insert it.
comPar.InsertAfter(new DocumentFormat.OpenXml.Wordprocessing.Run(new CommentReference() { Id = id }), cmtEnd);
}
示例3: AddTextBox
/// <summary>
/// Add text box
/// </summary>
/// <param name="textBox"></param>
/// <returns></returns>
private string AddTextBox(DocumentFormat.OpenXml.Vml.TextBox textBox)
{
foreach (OpenXmlElement element in textBox.Elements())
if (element is TextBoxContent)
return AddTextBoxContent((TextBoxContent)element);
return "";
}
示例4: SetColumnWidth
/// <summary>
/// Sets the column width
/// </summary>
/// <param name="worksheet">Worksheet to use</param>
/// <param name="columnIndex">Index of the column</param>
/// <param name="width">Width to set</param>
/// <returns>True if succesful</returns>
public static bool SetColumnWidth(DocumentFormat.OpenXml.Spreadsheet.Worksheet worksheet, int columnIndex, int width)
{
DocumentFormat.OpenXml.Spreadsheet.Columns columns;
DocumentFormat.OpenXml.Spreadsheet.Column column;
// Get the column collection exists
columns = worksheet.Elements<DocumentFormat.OpenXml.Spreadsheet.Columns>().FirstOrDefault();
if (columns == null)
{
return false;
}
// Get the column
column = columns.Elements<DocumentFormat.OpenXml.Spreadsheet.Column>().Where(item => item.Min == columnIndex).FirstOrDefault();
if (columns == null)
{
return false;
}
column.Width = width;
column.CustomWidth = true;
worksheet.Save();
return true;
}
示例5: DrawShape
/// <summary>
/// Draw shape
/// </summary>
/// <param name="shape"></param>
/// <returns></returns>
private string DrawShape(DocumentFormat.OpenXml.Vml.Shape shape)
{
string style = shape.GetAttributes().Where(x => x.LocalName == "style").FirstOrDefault().Value;
string position = GetValueOfProperty("position", style);
string styleLeft = GetValueOfProperty("left", style);
int marginLeft = ConvertToPixel(GetValueOfProperty("margin-left", style));
int marginTop = ConvertToPixel(GetValueOfProperty("margin-top", style));
int width = ConvertToPixel(GetValueOfProperty("width", style));
int height = ConvertToPixel(GetValueOfProperty("height", style));
string graphicName = shape.Id;
foreach (OpenXmlElement element in shape.Elements())
if (element is DocumentFormat.OpenXml.Vml.ImageData)
return DrawImageData(position, marginLeft, marginTop, width, height, (DocumentFormat.OpenXml.Vml.ImageData)element);
else if (element is DocumentFormat.OpenXml.Vml.TextBox)
return AddTextBox((DocumentFormat.OpenXml.Vml.TextBox)element);
return "";
}
示例6: DrawGroup
/// <summary>
/// Draw group
/// </summary>
/// <param name="group"></param>
/// <returns></returns>
private string DrawGroup(DocumentFormat.OpenXml.Vml.Group group)
{
string result = "";
foreach (OpenXmlElement element in group.Elements())
if (element is DocumentFormat.OpenXml.Vml.Shape)
result += DrawShape((DocumentFormat.OpenXml.Vml.Shape)element);
else if (element is DocumentFormat.OpenXml.Vml.Line)
result += DrawLine((DocumentFormat.OpenXml.Vml.Line)element);
return result;
}
示例7: AddPicture
/// <summary>
/// Add picture
/// </summary>
/// <param name="picture"></param>
/// <returns></returns>
private string AddPicture(DocumentFormat.OpenXml.Drawing.Pictures.Picture picture)
{
foreach (OpenXmlElement element in picture.Elements())
{
if (element is DocumentFormat.OpenXml.Drawing.Pictures.BlipFill)
{
DocumentFormat.OpenXml.Drawing.Blip blip = ((DocumentFormat.OpenXml.Drawing.Pictures.BlipFill)element).Blip;
if (blip != null)
{
OpenXmlPart image = document.MainDocumentPart.GetPartById(blip.Embed.Value);
string fileName = Path.Combine(imageDirectory, Path.GetFileName(image.Uri.ToString()));
fileName = Util.StreamToFile(image.GetStream(), fileName, FileMode.CreateNew, null);
return fileName;
}
}
}
return "";
}
示例8: AddGraphicData
/// <summary>
/// Add graphic data
/// </summary>
/// <param name="graphicData"></param>
/// <returns></returns>
private string AddGraphicData(DocumentFormat.OpenXml.Drawing.GraphicData graphicData)
{
foreach (OpenXmlElement element in graphicData.Elements())
if (element is DocumentFormat.OpenXml.Drawing.Pictures.Picture)
return AddPicture((DocumentFormat.OpenXml.Drawing.Pictures.Picture)element);
return "";
}
示例9: AddGraphic
/// <summary>
/// Add graphic
/// </summary>
/// <param name="graphic"></param>
/// <returns></returns>
private string AddGraphic(DocumentFormat.OpenXml.Drawing.Graphic graphic)
{
foreach (OpenXmlElement element in graphic.Elements())
if (element is DocumentFormat.OpenXml.Drawing.GraphicData)
return AddGraphicData((DocumentFormat.OpenXml.Drawing.GraphicData)element);
return "";
}