public static int DecimalToU_Int32(decimal argument) { object Int32Value; object UInt32Value; // Convert the argument to an int value. try { Int32Value = (int)argument; } catch (Exception ex) { Int32Value = GetExceptionType(ex); } // Convert the argument to a uint value. try { UInt32Value = (uint)argument; } catch (Exception ex) { UInt32Value = GetExceptionType(ex); } return(Int32.Parse(UInt32Value.ToString())); }
//..................................................................... /// <summary> /// /// </summary> /// <param name="txtline"></param> /// <returns></returns> private TableCell MakeTableCell(string txtline, int widthMM, int align = 1) { UInt32Value uint32ww = OpenDocxBase.MM2Uint32V(widthMM); //............................................. TableCellWidth zellwidth = new TableCellWidth( ) { Width = uint32ww.ToString(), /*widthMM.ToString( ),*/ Type = TableWidthUnitValues.Dxa }; TableCellVerticalAlignment zellVertAlign = new TableCellVerticalAlignment( ) { Val = TableVerticalAlignmentValues.Center }; TableCellProperties zellProperties = new TableCellProperties( ); zellProperties.Append(zellwidth); zellProperties.Append(zellVertAlign); //............................................. Justification justRIGHT = new Justification( ) { Val = JustificationValues.Right }; Justification justCENTER = new Justification( ) { Val = JustificationValues.Center }; ParagraphProperties paraProperties = new ParagraphProperties( ); if (align == 2) { paraProperties.Append(justCENTER); } if (align == 3) { paraProperties.Append(justRIGHT); } //............................................. RunFonts runFonts = new RunFonts( ) { Hint = FontTypeHintValues.EastAsia }; RunProperties textRunProperties = new RunProperties( ); textRunProperties.Append(runFonts); Run runOBJ = new Run( ); runOBJ.Append(textRunProperties); runOBJ.Append(new Text( ) { Text = txtline }); //............................................. Paragraph paragraph = new Paragraph( ) { RsidParagraphAddition = "005823AE", RsidRunAdditionDefault = "005823AE" }; paragraph.Append(paraProperties); paragraph.Append(runOBJ); //............................................. TableCell tableZELL = new TableCell( ); tableZELL.Append(zellProperties); tableZELL.Append(paragraph); return(tableZELL); }
public static StringValue GetCellReference(Cell cell, UInt32Value rowIndex) { var cellValue = cell.CellReference.Value; return(new StringValue(cellValue.Replace(Regex.Replace(cellValue, @"[^\d]+", ""), rowIndex.ToString()))); }
// Generates content of worksheetPart1. private void GenerateWorksheetPart1Content <T>(WorksheetPart worksheetPart1, IEnumerable <T> data) { Worksheet worksheet1 = new Worksheet(); SheetData sheetData1 = new SheetData(); var props = (typeof(T)).GetProperties().Where(f => !f.IsDefined(typeof(ScaffoldColumnAttribute), true) || ((ScaffoldColumnAttribute)f.GetCustomAttributes(typeof(ScaffoldColumnAttribute), true).First()).Scaffold == true ).OrderBy(f => { if (f.IsDefined(typeof(DisplayAttribute), true)) { return(((DisplayAttribute)f.GetCustomAttributes(typeof(DisplayAttribute), true).First()).GetOrder()); } else { return(default(int)); } }); DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row(); UInt32Value rowIndex = 1; headerRow.RowIndex = rowIndex++; List <String> columns = new List <string>(); foreach (var prop in props) { var headerName = prop.Name; if (prop.IsDefined(typeof(DisplayNameAttribute), true)) { headerName = ((DisplayNameAttribute)prop.GetCustomAttributes(typeof(DisplayNameAttribute), true).First()).DisplayName; } if (prop.IsDefined(typeof(DisplayAttribute), true)) { var da = ((DisplayAttribute)prop.GetCustomAttributes(typeof(DisplayAttribute), true).First()); headerName = da.GetShortName() ?? da.GetName() ?? prop.Name; } columns.Add(prop.Name); DocumentFormat.OpenXml.Spreadsheet.Cell cell = headerRow.Create(); cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String; cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(headerName); headerRow.AppendChild(cell); } sheetData1.AppendChild(headerRow); foreach (var item in data) { int cellIndex = 0; var row = sheetData1.Create(); foreach (var prop in props) { var val = prop.GetValue(item, null); string str = null; if (val == null) { str = null; } else if ((prop.PropertyType == typeof(DateTime) || prop.PropertyType == typeof(Nullable <DateTime>)) && prop.IsDefined(typeof(DisplayFormatAttribute), true)) { var df = ((DisplayFormatAttribute)prop.GetCustomAttributes(typeof(DisplayFormatAttribute), true).First()); if (df != null) { var date = (val as DateTime?).Value; str = date.ToString(df.DataFormatString); } } else { str = val.ToString(); } var cell = new Cell() { CellReference = XlsxExtensions.Column(cellIndex++) + rowIndex.ToString() }; if (prop.PropertyType == typeof(int) || prop.PropertyType == typeof(Nullable <int>) || prop.PropertyType == typeof(decimal) || prop.PropertyType == typeof(Nullable <decimal>)) { cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.Number; } else { cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String; } cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(str); row.AppendChild(cell); } sheetData1.AppendChild(row); rowIndex++; } worksheet1.Append(sheetData1); worksheetPart1.Worksheet = worksheet1; }
internal static Picture AddPicture(this Slide slide, Shape referingShape, string imageFile) { Picture picture = new Picture(); string embedId = string.Empty; UInt32Value picId = 10001U; string name = string.Empty; if (slide.Elements <Picture>().Count() > 0) { picId = ++slide.Elements <Picture>().ToList().Last().NonVisualPictureProperties.NonVisualDrawingProperties.Id; } name = "image" + picId.ToString(); embedId = "rId" + (slide.Elements <Picture>().Count() + 915).ToString(); // some value NonVisualPictureProperties nonVisualPictureProperties = new NonVisualPictureProperties() { NonVisualDrawingProperties = new NonVisualDrawingProperties() { Name = name, Id = picId, Title = name }, NonVisualPictureDrawingProperties = new NonVisualPictureDrawingProperties() { PictureLocks = new Drawing.PictureLocks() { NoChangeAspect = true } }, ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties() { UserDrawn = true } }; BlipFill blipFill = new BlipFill() { Blip = new Drawing.Blip() { Embed = embedId } }; Drawing.Stretch stretch = new Drawing.Stretch() { FillRectangle = new Drawing.FillRectangle() }; blipFill.Append(stretch); ShapeProperties shapeProperties = new ShapeProperties() { Transform2D = new Drawing.Transform2D() { Offset = new Drawing.Offset() { X = 1554691, Y = 1600200 }, Extents = new Drawing.Extents() { Cx = 6034617, Cy = 4525963 } } }; Drawing.PresetGeometry presetGeometry = new Drawing.PresetGeometry() { Preset = Drawing.ShapeTypeValues.Rectangle }; Drawing.AdjustValueList adjustValueList = new Drawing.AdjustValueList(); presetGeometry.Append(adjustValueList); shapeProperties.Append(presetGeometry); picture.Append(nonVisualPictureProperties); picture.Append(blipFill); picture.Append(shapeProperties); slide.CommonSlideData.ShapeTree.Append(picture); // Add Image part slide.AddImagePart(embedId, imageFile); slide.Save(); return(picture); }