我从 OpenXML sdk 获取代码,而不是SpreadsheetDocument.Create
,我使用了SpreadsheetDocument.Open
此代码正常工作,并在将图像添加到文件后在.xlsx
中添加图像。我打开显示的文件 - >
文件已损坏且无法打开
如果您想了解更多详情请!让我知道。
参考网址 - > https://code.msdn.microsoft.com/office/How-to-insert-image-into-93964561
感谢您的帮助!
/// <summary>
/// add sheet in xlsx then add image into it.
/// </summary>
/// <param name="sFile"></param>
/// <param name="imageFileName"></param>
public void InsertimginExcel(string sFile, string imageFileName)
{
try
{
// Create a spreadsheet document by supplying the filepath.
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
Open(sFile, true))
{
// Add a WorkbookPart to the document.
//WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
//workbookpart.Workbook = new Workbook();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = spreadsheetDocument.WorkbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets to the Workbook.
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
AppendChild<Sheets>(new Sheets());
// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet()
{
Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "mySheet"
};
sheets.Append(sheet);
var drawingsPart = worksheetPart.AddNewPart<DrawingsPart>();
if (!worksheetPart.Worksheet.ChildElements.OfType<DocumentFormat.OpenXml.Spreadsheet.Drawing>().Any())
{
worksheetPart.Worksheet.Append(new DocumentFormat.OpenXml.Spreadsheet.Drawing { Id = worksheetPart.GetIdOfPart(drawingsPart) });
}
if (drawingsPart.WorksheetDrawing == null)
{
drawingsPart.WorksheetDrawing = new WorksheetDrawing();
}
var worksheetDrawing = drawingsPart.WorksheetDrawing;
var imagePart = drawingsPart.AddImagePart(ImagePartType.Jpeg);
using (var stream = new FileStream(imageFileName, FileMode.Open))
{
imagePart.FeedData(stream);
}
Bitmap bm = new Bitmap(imageFileName);
DocumentFormat.OpenXml.Drawing.Extents extents = new DocumentFormat.OpenXml.Drawing.Extents();
var extentsCx = (long)bm.Width * (long)((float)914400 / bm.HorizontalResolution);
var extentsCy = (long)bm.Height * (long)((float)914400 / bm.VerticalResolution);
bm.Dispose();
var colOffset = 0;
var rowOffset = 0;
int colNumber = 5;
int rowNumber = 10;
var nvps = worksheetDrawing.Descendants<Xdr.NonVisualDrawingProperties>();
var nvpId = nvps.Count() > 0 ?
(UInt32Value)worksheetDrawing.Descendants<Xdr.NonVisualDrawingProperties>().Max(p => p.Id.Value) + 1 :
1U;
var oneCellAnchor = new Xdr.OneCellAnchor(
new Xdr.FromMarker
{
ColumnId = new Xdr.ColumnId((colNumber - 1).ToString()),
RowId = new Xdr.RowId((rowNumber - 1).ToString()),
ColumnOffset = new Xdr.ColumnOffset(colOffset.ToString()),
RowOffset = new Xdr.RowOffset(rowOffset.ToString())
},
new Xdr.Extent { Cx = extentsCx, Cy = extentsCy },
new Xdr.Picture(
new Xdr.NonVisualPictureProperties(
new Xdr.NonVisualDrawingProperties { Id = nvpId, Name = "Picture " + nvpId, Description = imageFileName },
new Xdr.NonVisualPictureDrawingProperties(new A.PictureLocks { NoChangeAspect = true })
),
new Xdr.BlipFill(
new A.Blip { Embed = drawingsPart.GetIdOfPart(imagePart), CompressionState = A.BlipCompressionValues.Print },
new A.Stretch(new A.FillRectangle())
),
new Xdr.ShapeProperties(
new A.Transform2D(
new A.Offset { X = 0, Y = 0 },
new A.Extents { Cx = extentsCx, Cy = extentsCy }
),
new A.PresetGeometry { Preset = A.ShapeTypeValues.Rectangle }
)
),
new Xdr.ClientData()
);
worksheetDrawing.Append(oneCellAnchor);
//workbookpart.Workbook.Save();
// Close the document.
spreadsheetDocument.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//log.Error("error occur while creating sheet and adding image --> " + ex.Message.ToString());
}
}
答案 0 :(得分:1)
不确定xlsx文件可以处理多少个工作簿部分和工作表部分,以及SheetId
是否必须是所有表格的文档唯一。尝试将ID更改为例如5如果没有帮助:不要为工作表创建新的工作簿部分,如果可能,请使用existig。
答案 1 :(得分:0)
文件已损坏且无法打开
取消选中受保护的视图下的所有选项,然后按确定确认。
重新启动Excel并尝试打开损坏的Excel文档。
如果这不起作用,通常在您创建XSLX文件并且出现错误时,您会看到:
您可以检查您的Internet临时文件以获取日志。日志文件应该包含有关格式错误的信息。请使用任何其他信息更新您的问题。