我创建了一个工作簿
Document doc = new Document();
var builder = new DocumentBuilder(doc);
Workbook workbook = new Workbook();
int i = workbook.Worksheets.Add();
Worksheet sheet = workbook.Worksheets[i];
然后创建我的单元格,然后通过insertOleObject
方法将其插入到我的文档中:
MemoryStream memorystream = new MemoryStream();
workbook.Save(memorystream, Aspose.Cells.SaveFormat.Xlsx);
byte[] bytes = memorystream.ToArray();
Shape oleObject = builder.InsertOleObject(memorystream, "Excel.Sheet.2", false, null);
我希望我在工作簿中构建的工作表可以显示为表格但不会发生。
答案 0 :(得分:3)
请添加一行代码,如我在此处所示,它应该可以解决您的问题。请告诉我们您的反馈意见。
<强> C#强>
MemoryStream memorystream = new MemoryStream();
workbook.Save(memorystream, Aspose.Cells.SaveFormat.Xlsx);
//Add this line
memorystream.Position = 0;
以下是完整的可运行代码供您参考。
标题:将Microsoft Excel Ole对象插入Microsoft Word文档
<强> C#强>
Document doc = new Document();
var builder = new DocumentBuilder(doc);
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
sheet.Cells["D3"].PutValue("This is sample data.");
sheet.Cells["J20"].PutValue("End");
MemoryStream memorystream = new MemoryStream();
workbook.Save(memorystream, Aspose.Cells.SaveFormat.Xlsx);
memorystream.Position = 0;
Aspose.Words.Drawing.Shape shp = builder.InsertOleObject(memorystream, "Excel.Sheet.12", false, null);
doc.Save("Output.docx");
注意: 我在Aspose担任开发人员传播者
答案 1 :(得分:1)
我修改了代码,正如谢尔克在评论中所说,他的答案我完成了:
MemoryStream memorystream = new MemoryStream();
workbook.Save(memorystream, Aspose.Cells.SaveFormat.Xlsx);
byte[] bytes = memorystream.ToArray();
memorystream.Position = 0;
// Apply different Image and Print options
var options = new Aspose.Cells.Rendering.ImageOrPrintOptions
{
HorizontalResolution = 200,
VerticalResolution = 200
};
// Set Horizontal Resolution
// Set Vertical Resolution
var sr = new SheetRender(sheet, options);
MemoryStream imageStream = new MemoryStream();
sr.ToImage(0, imageStream);
System.Drawing.Image image = Image.FromStream(imageStream);
Shape oleObject = builder.InsertOleObject(memorystream, "Excel.Sheet.12", false, image);