how to embed ole object directly. without saving any file in disk - c#

I already created a docx file by aspose.word and I need to embed an worksheet to this file thus I created a workbook and worksheet .
Document doc = new Document();
Workbook workbook = new Workbook();
int i = workbook.Worksheets.Add();
Worksheet sheet = workbook.Worksheets[i];
and after filling cells in the sheet I save my excel file in storage and embedded it to my docx by using insertOleObject and pass file address to this method by using file stream.
workbook.Save(dir+"output.xlx");
Stream memStream = File.OpenRead(dir+"output.xlx");
Shape oleObject = builder.InsertOleObject(memStream, "Excel.Sheet.2",false,null);
but I want to embed a worksheet(or workbook) directly without using workbook.Save method and Stream object.

As bassfader shared, first save your Workbook to Memory Stream. Besides, you can convert your Memory Stream into Byte Array. Then you can use the same code as you have shown to insert Workbook (which is now in form of memory stream or byte array) as Ole Object in your MS-Word Document.
Note: I am working as Developer Evangelist at Aspose

Related

Open workbook in Interop.Excel from byte array

I want to open Excel from byte[] because my file is encrypted and I want to open after decrypt but without write in a file.
The office has "restricted access" and I want to open my file with this protection but without saving the decrypted content in a file.
myApp.Workbooks.Open only supports a path.
Is it possible?
As an alternative to OpenXml there's also ExcelDataReader which from my experience is a lot faster in processing data compared to Interop.Excel(around 3 times+).
It can also open encrypted Excel files directly(stackoverflow)
The github page for ExcelDataReader has some great examples on how to use it. The only thing you'd have to do is:
This:
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
Becomes this:
using (var stream = new MemoryStream(yourByte[])
And if you just want to open the password protected excel file you'd do this:
var conf = new ExcelReaderConfiguration { Password = "yourPassword" }; //Add this
excelReader = ExcelReaderFactory.CreateReader(stream, conf); //change the excel Reader to this
Make sure to check the Github page for more info!
It is not possible because the interop is actually an interface for programs to run and operate existing excel on the computer.
I think you need to use openxml created by Microsoft to work with excel word and PowerPoint.
DocumentFormat.OpenXml
Then you can use:
ExcelPackage excelPackage = new ExcelPackage(stream)
or
var pck = new OfficeOpenXml.ExcelPackage();
pck.Load(File.OpenRead(path));
pck.Load(Stream) can use any stream as input not only from a file.
It depends on your needs.

Save Excel as PDF

I open an Excel file in c#, make some changes and I want to save it as pdf file.
I have searched about it and find this:
Microsoft.Office.Interop.Excel._Workbook oWB;
oWB.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, "D:\\xxxxx.pdf");
but this code sometimes opens a form and a printer must be selected! I don't know why?!
Is there any other way for exporting PDF from Excel?
I saw that Workbook.saveas() has a Fileformat object. How can we use it?
Check out Spire.Xls, below is the code for converting Excel to PDF.
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
//If you want to make the excel content fit to pdf page
//workbook.ConverterSetting.SheetFitToPage = true;
workbook.SaveToFile("result.pdf", Spire.Xls.FileFormat.PDF);
It has a free version which you can use without charge:
https://www.e-iceblue.com/Introduce/free-xls-component.html (Also available on NuGet)
Use iTextSharp. It is native .NET code. Doesn't require any Excel interop -
https://www.nuget.org/packages/itextsharp/
If you're looking for an alternative approach, then check out GemBox.Spreadsheet:
https://www.nuget.org/packages/GemBox.Spreadsheet/
Here is how you can save Excel to PDF with it:
ExcelFile excel = ExcelFile.Load("D:\\xxxxx.xlsx");
excel.Save("D:\\xxxxx.pdf");
Or you can write it as following:
XlsxLoadOptions loadOptions = new XlsxLoadOptions();
ExcelFile excel = ExcelFile.Load("D:\\xxxxx.xlsx", loadOptions);
PdfSaveOptions saveOptions = new PdfSaveOptions();
excel.Save("D:\\xxxxx.pdf", saveOptions);
You can do this using this API. Please see documentation for further detail.
http://cdn.bytescout.com/help/BytescoutPDFExtractorSDK/html/55590148-5bef-4338-ac16-1de4056a952b.htm

How to create Excel file using OpenXML without creating a local file?

Is it possible to create and edit an excel document using OpenXML SDK without creating a local file?
As per the documentation the Create method demands for a filepath, which creates a local copy of the file.
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);
I'm referring the MSDN article here:
https://msdn.microsoft.com/en-us/library/office/ff478153.aspx
My requirement is to create the file, and it should be downloaded by the browser on clicking a button.
Any suggestion?
You could use the overload of SpreadsheetDocument.Create that takes a Stream and pass it a MemoryStream:
MemoryStream memoryStream = new MemoryStream();
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(memoryStream, SpreadsheetDocumentType.Workbook);
//add the excel contents...
//reset the position to the start of the stream
memoryStream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
Note that as per this StackOverflow question you don't need to dispose the Stream as it will be done for you by the FileStreamResult class.
Adding another answer as I spent way too much time searching for this: Another way to save a SpreadsheetDocument to a stream is by calling SpreadsheetDocument.Clone(Stream s)
It has the benefit of letting you save to a stream even if you created the document from a file or a stream you don't want to save to.

How to convert byte array of word doc into byte array of pdf document in C#

I have a sequence of bytes as follows (Word format) exported from crystal report
Stream stream = cryRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.WordForWindows);
I would like to convert that stream to a PDF stream to be stored in a varbinary column in sql server database.
There is a barcode in the document and i am unable to embed it when I export to pdf. Exporting to word works though so I want to try from Word to PDF
Can anyone assist me in getting this PDF byte[]?
You can do that using the Microsoft.Office.Interop.Word NuGet Package. Once you added it on your application you can flush your Byte Array to a temporary file, then open the temp file with Interop.Word, work with it, save the result and read the result back into a Byte Array.
Here's a sample code snippet that does just that:
// byte[] fileBytes = getFileBytesFromDB();
var tmpFile = Path.GetTempFileName();
File.WriteAllBytes(tmpFile, fileBytes);
Application app = new word.Application();
Document doc = app.Documents.Open(filePath);
// Save DOCX into a PDF
var pdfPath = "path-to-pdf-file.pdf";
doc.SaveAs2(pdfPath, word.WdSaveFormat.wdFormatPDF);
doc.Close();
app.Quit(); // VERY IMPORTANT: do this to close the MS Word instance
byte[] pdfFileBytes = File.ReadAllBytes(pdfPath);
File.Delete(tmpFile);
For more info regarding the topic you can also read this post on my blog.

open excel workbook from memorystream

I am trying to open an excel workbook from a memorystream. I have a web url of the excel file, what I do is I download the data from the url, then save it into a memorystream, but I am not sure how to open the workbook from the stream, here is how my code works so far.
WebClient wc = new WebClient();
byte[] fileArray = wc.DownloadData("url is inserted here");
MemoryStream ms = new MemoryStream(fileArray);
But from here I'm not sure how to go about reading the data from the stream to create the workbook, it doesn't seem like the spreadsheet document from http://msdn.microsoft.com/en-us/library/ff478410 works the way I want it to, any assistance or pointers would be appreciated
SpreadSheetDocument has an Open static method overload that takes stream as param for document source, just add to your code :
var doc = SpreadSheetDocument.Open(ms, isEditable);

Categories