/// <summary> /// 获取样式名称 /// </summary> /// <param name="source"></param> /// <returns></returns> public static string GetCssName(this ColumnOffset source) { var result = new StringBuilder(); foreach (var item in ColumnOffsetFlags.From(source)) { if ((item & source) == item) { if (result.Length > 0) { result.Append(" "); } result.Append("col-"); result.Append(item.ToString().Substring(0, 2).ToLower()); result.Append("-offset-"); result.Append(item.ToString().Substring(2)); } } return(result.ToString()); }
/// <summary> /// 移除样式 /// </summary> /// <param name="source"></param> /// <param name="column"></param> /// <returns></returns> public static IHtmlElement RemoveClass(this IHtmlElement source, ColumnOffset column) { source.RemoveClass(column.GetCssName()); return(source); }
private void PlaceImageOnCell(Worksheet worksheet, Bitmap image, int Col, int Row, double colWid, double rowHeight, string type, string imgName = "", string imgDesc = "")//, float? W, float? H) { Dictionary <string, ImagePartType> mimeToImagePartType = new Dictionary <string, ImagePartType>() { { "image/bmp", ImagePartType.Bmp }, { "image/gif", ImagePartType.Gif }, { "image/jpg", ImagePartType.Jpeg }, { "image/jpeg", ImagePartType.Jpeg }, { "image/png", ImagePartType.Png }, { "image/tiff", ImagePartType.Tiff } }; try { ImagePart imagePart; ImagePartType ip; if (!mimeToImagePartType.TryGetValue(type, out ip)) { ip = ImagePartType.Jpeg; } WorksheetDrawing wsd; DrawingsPart dp; WorksheetPart worksheetPart = worksheet.WorksheetPart; double imgMargin = 5; //string sImagePath = "D:/Documents/Source/Repos/DarrylSite/DarrylSite/Images/DC.png"; if (worksheetPart.DrawingsPart == null) { dp = worksheetPart.AddNewPart <DrawingsPart>(); imagePart = dp.AddImagePart(ip, worksheetPart.GetIdOfPart(dp)); wsd = new WorksheetDrawing(); } else { dp = worksheetPart.DrawingsPart; imagePart = dp.AddImagePart(ip); dp.CreateRelationshipToPart(imagePart); wsd = dp.WorksheetDrawing; } NonVisualDrawingProperties nvdp = new NonVisualDrawingProperties(); nvdp.Id = GetNextImageId(); nvdp.Name = imgName; nvdp.Description = imgDesc; DocumentFormat.OpenXml.Drawing.PictureLocks picLocks = new DocumentFormat.OpenXml.Drawing.PictureLocks(); picLocks.NoChangeAspect = true; picLocks.NoChangeArrowheads = true; NonVisualPictureDrawingProperties nvpdp = new NonVisualPictureDrawingProperties(); nvpdp.PictureLocks = picLocks; NonVisualPictureProperties nvpp = new NonVisualPictureProperties(); nvpp.NonVisualDrawingProperties = nvdp; nvpp.NonVisualPictureDrawingProperties = nvpdp; DocumentFormat.OpenXml.Drawing.Stretch stretch = new DocumentFormat.OpenXml.Drawing.Stretch(); stretch.FillRectangle = new DocumentFormat.OpenXml.Drawing.FillRectangle(); BlipFill blipFill = new BlipFill(); DocumentFormat.OpenXml.Drawing.Blip blip = new DocumentFormat.OpenXml.Drawing.Blip(); blip.Embed = dp.GetIdOfPart(imagePart); blip.CompressionState = DocumentFormat.OpenXml.Drawing.BlipCompressionValues.Email; /* * string outerXml = * "<a:ext uri=\"{28A0092B-C50C-407E-A947-70E740481C1C}\">" + * "<a14:useLocalDpi xmlns:a14 = \"http://schemas.microsoft.com/office/drawing/2010/main\"/>" + * "</a:ext> "; * * string outerXml = "uri=\"{28A0092B-C50C-407E-A947-70E740481C1C}\">"; */ A14.UseLocalDpi localDpi = new A14.UseLocalDpi(); /* * ExtensionList extLst = new ExtensionList(); * Extension extsn = new Extension(localDpi); * extsn.Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}"; * extLst.Append(extsn); * blip.Append(extLst); */ A.BlipExtensionList blipExtLst = new A.BlipExtensionList(); A.BlipExtension blipExt = new A.BlipExtension() { Uri = "{28A0092B-C50C-407E-A947-70E740481C1C}" }; localDpi.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main"); blipExt.Append(localDpi); blipExtLst.Append(blipExt); blip.Append(blipExtLst); blip.Append(); blipFill.Blip = blip; blipFill.SourceRectangle = new DocumentFormat.OpenXml.Drawing.SourceRectangle(); blipFill.Append(stretch); DocumentFormat.OpenXml.Drawing.Transform2D t2d = new DocumentFormat.OpenXml.Drawing.Transform2D(); DocumentFormat.OpenXml.Drawing.Offset offset = new DocumentFormat.OpenXml.Drawing.Offset(); offset.X = 0; offset.Y = 0; t2d.Offset = offset; //Bitmap bm = new Bitmap(sImagePath); //http://en.wikipedia.org/wiki/English_Metric_Unit#DrawingML //http://stackoverflow.com/questions/1341930/pixel-to-centimeter //http://stackoverflow.com/questions/139655/how-to-convert-pixels-to-points-px-to-pt-in-net-c DocumentFormat.OpenXml.Drawing.Extents extents = new DocumentFormat.OpenXml.Drawing.Extents(); //extents.Cy = (long)image.Height * (long)((float)914400 / image.VerticalResolution); //extents.Cy = (int)(rowHeight * 72 / 96 * (100 - (2 * imgMargin)) / 100) * 12700; extents.Cy = (int)((rowHeight * 72 / 96 * 12700) * (((100 - (2 * imgMargin)) / 100))); //extents.Cx = (int)(((((colWid - 1) * 7) + 12) * 12700) * (.5 * image.Height / image.Width)); //extents.Cx = (int)(((((colWid - 1) * 7) + 12) * 12700) * (.5 * extents.Cy / image.Width)); extents.Cx = image.Width * extents.Cy / image.Height; ; t2d.Extents = extents; ShapeProperties sp = new ShapeProperties(); sp.BlackWhiteMode = DocumentFormat.OpenXml.Drawing.BlackWhiteModeValues.Auto; sp.Transform2D = t2d; DocumentFormat.OpenXml.Drawing.PresetGeometry prstGeom = new DocumentFormat.OpenXml.Drawing.PresetGeometry(); prstGeom.Preset = DocumentFormat.OpenXml.Drawing.ShapeTypeValues.Rectangle; prstGeom.AdjustValueList = new DocumentFormat.OpenXml.Drawing.AdjustValueList(); sp.Append(prstGeom); sp.Append(new DocumentFormat.OpenXml.Drawing.NoFill()); DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture picture = new DocumentFormat.OpenXml.Drawing.Spreadsheet.Picture(); picture.NonVisualPictureProperties = nvpp; picture.BlipFill = blipFill; picture.ShapeProperties = sp; Xdr.FromMarker fromMarker = new Xdr.FromMarker(); Xdr.ToMarker toMarker = new Xdr.ToMarker(); // From ColumnId columnId1 = new ColumnId(); columnId1.Text = Col.ToString(); ColumnOffset columnOffset1 = new ColumnOffset(); //columnOffset1.Text = "228600"; double colloff = ((((colWid - 1) * 7) + 12) * 12700) * 72 / 96; colloff -= extents.Cx; colloff /= 2; colloff = (int)colloff; columnOffset1.Text = colloff.ToString(); RowId rowId1 = new RowId(); rowId1.Text = Row.ToString(); RowOffset rowOffset1 = new RowOffset(); double rowoff = (int)((rowHeight * 72 / 96 * 12700) * (imgMargin / 100)); rowOffset1.Text = (rowoff).ToString(); fromMarker.Append(columnId1); fromMarker.Append(columnOffset1); fromMarker.Append(rowId1); fromMarker.Append(rowOffset1); // To ColumnId columnId2 = new ColumnId(); ColumnOffset columnOffset2 = new ColumnOffset(); RowId rowId2 = new RowId(); RowOffset rowOffset2 = new RowOffset(); columnId2.Text = (Col).ToString(); columnOffset2.Text = "0";// "152381"; // Margin is accounted for, so take it off the width columnOffset2.Text = (extents.Cx + colloff).ToString(); rowId2.Text = (Row).ToString(); rowOffset2.Text = "4572000"; // 4572000 = 12700 (#EMUs/pixel) * 45 (Olivia's cell height in Excel Cell units (aka 60 pixels)) * 8 (???) rowOffset2.Text = (extents.Cy + rowoff).ToString(); // toMarker.Append(columnId2); toMarker.Append(columnOffset2); toMarker.Append(rowId2); toMarker.Append(rowOffset2); //Position pos = new Position(); pos.X = Row; pos.Y = Col; Extent ext = new Extent(); ext.Cx = extents.Cx; ext.Cy = extents.Cy; TwoCellAnchor anchor = new TwoCellAnchor(); //OneCellAnchor anchor = new OneCellAnchor(); anchor.Append(fromMarker); anchor.Append(toMarker); //anchor.Extent = ext; //anchor.Append(toMarker); anchor.Append(picture); anchor.Append(new ClientData()); wsd.Append(anchor); { MemoryStream imgStream = new MemoryStream(); ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo codec = codecs.First(c => c.MimeType == type); Guid imgGuid = codec.FormatID; ImageFormat imf = new ImageFormat(imgGuid); float h = image.HorizontalResolution; float v = image.VerticalResolution; Bitmap compressedImage = new Bitmap(64, 64);//, gr); Rectangle rect = new Rectangle(0, 0, 64, 64); Graphics gr = Graphics.FromImage(compressedImage); gr.DrawImage(image, rect); compressedImage.SetResolution(48, 48); compressedImage.Save(imgStream, imf); gr.Dispose(); compressedImage.Dispose(); imgStream.Position = 0; imagePart.FeedData(imgStream); imgStream.Dispose(); } if (drawing == null) { drawing = new Drawing(); drawing.Id = dp.GetIdOfPart(imagePart); worksheet.Append(drawing); } wsd.Save(dp); worksheetPart.Worksheet.Save(); /* Sheets sheets = new Sheets();Sheet sheet = new Sheet();sheet.Name = "Sheet1";sheet.SheetId = 1;sheet.Id = wbp.GetIdOfPart(wsp);sheets.Append(sheet);wb.Append(sheets); */ } catch (Exception e) { ModelState.AddModelError("Caught fatal exception placing image", e); Trace.TraceError($"OJException: Could not place image {e.Message}"); } }