gpt4 book ai didi

c# - '文件已损坏,无法打开' OpenXML

转载 作者:行者123 更新时间:2023-11-30 12:38:47 24 4
gpt4 key购买 nike

我从 OpenXML sdk 获得了该代码,而不是 SpreadsheetDocument.Create,我使用了 SpreadsheetDocument.Open

此代码有效,在将图像添加到文件后,在 .xlsx 中添加图像。我打开显示的文件 ->

The file is corrupt and cannot be opened

如果您想了解更多详情,请!让我知道。

引用网址 -> 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());
}
}

最佳答案

不确定一个 xlsx 文件可以处理多少个工作簿部分和工作表部分,以及 SheetId 是否必须是所有工作表的唯一文档。尝试将 Id 更改为例如5 如果这没有帮助:不要为工作表创建新的工作簿部分,如果可能,使用现有的工作簿部分。

关于c# - '文件已损坏,无法打开' OpenXML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49727901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com