Exemplo n.º 1
0
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Invalid number of arguments");
                Console.WriteLine(@"Usage: Converter C:\FilePath\fileName.xlsm C:\filePath\fileNewName.xlsx");
                Environment.Exit(1);
            }

            string fPath    = args[0];
            string fNewFile = args[1];

            if (!File.Exists(fPath))
            {
                Console.WriteLine("File: " + fPath + " not found");
                Environment.Exit(1);
            }

            try
            {
                byte[] byteArray = File.ReadAllBytes(fPath);
                using (MemoryStream stream = new MemoryStream())
                {
                    stream.Write(byteArray, 0, (int)byteArray.Length);
                    using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Open(stream, true))
                    {
                        spreadsheetDoc.DeletePartsRecursivelyOfType <VbaDataPart>();
                        spreadsheetDoc.DeletePartsRecursivelyOfType <VbaProjectPart>();

                        //Change from template type to workbook type
                        spreadsheetDoc.ChangeDocumentType(SpreadsheetDocumentType.Workbook);
                    }
                    File.WriteAllBytes(fNewFile, stream.ToArray());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.Message);
                Environment.Exit(1);
            }

            Environment.Exit(0);
        }
Exemplo n.º 2
0
        public static void Run([BlobTrigger("files-stage/xlsm/{name}.xlsm")] Stream xlsmBlob, string name, ILogger log, [Blob("files-stage/files/{name}.xlsx", FileAccess.Write)] Stream xlsxBlob)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {xlsmBlob.Length} Bytes");

            using (MemoryStream interim = new MemoryStream())
            {
                xlsmBlob.CopyTo(interim);

                using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Open(interim, true))
                {
                    spreadsheetDoc.DeletePartsRecursivelyOfType <VbaDataPart>();
                    spreadsheetDoc.DeletePartsRecursivelyOfType <VbaProjectPart>();

                    // Change from template type to workbook type
                    spreadsheetDoc.ChangeDocumentType(SpreadsheetDocumentType.Workbook);
                }

                var byteArray = interim.ToArray();

                xlsxBlob.Write(byteArray, 0, byteArray.Length);
            }
        }