Writing a byte array to an Excel file












0















I've been given the task of writing automated tests that check our own API. Part of this process involves testing an end point that generates an Excel template that the recipient is then supposed to fill back out and submit back to us.



From the looks of things this template gets sent back to the user from within the browser using a FileContentResult object that also specifies the content type (application/vnd.ms-excel.sheet.macroEnabled.12; the intended file format is .xlsm).



The problem I have is this: whilst getting the file in terms of a byte array works without issue with regards to the call to the end point is concerned I have yet to successfully take that byte array returned and use it for anything useful. Creating an excel file from that seems to be problematic. Just using File.WriteAllBytes() doesn't seem to work for example nor does using a BinaryWriter.



Does anybody have any idea how to achieve this from within C# code that isn't running as part of a website?










share|improve this question




















  • 1





    If the byte array is supposed to be an excel file then File.WriteAllBytes() would be the way to create it on disk, you say it doesn't work, what happens? - The first thing to do would be to open the created file in a text editor and see if contains something unexpected - i.e plaintext.

    – Alex K.
    Nov 14 '18 at 16:30






  • 1





    I believe I've found the answer: it was a fundamental misunderstanding on my part. I thought what I had was a string representation of a byte array. It turns out it was base64 encoded and that also needed to be dealt with. As soon as I added the call to Convert.FromBase64String() instead of just straight up taking the value and converting it to a byte array it worked fine.

    – PatrickA
    Nov 14 '18 at 16:47
















0















I've been given the task of writing automated tests that check our own API. Part of this process involves testing an end point that generates an Excel template that the recipient is then supposed to fill back out and submit back to us.



From the looks of things this template gets sent back to the user from within the browser using a FileContentResult object that also specifies the content type (application/vnd.ms-excel.sheet.macroEnabled.12; the intended file format is .xlsm).



The problem I have is this: whilst getting the file in terms of a byte array works without issue with regards to the call to the end point is concerned I have yet to successfully take that byte array returned and use it for anything useful. Creating an excel file from that seems to be problematic. Just using File.WriteAllBytes() doesn't seem to work for example nor does using a BinaryWriter.



Does anybody have any idea how to achieve this from within C# code that isn't running as part of a website?










share|improve this question




















  • 1





    If the byte array is supposed to be an excel file then File.WriteAllBytes() would be the way to create it on disk, you say it doesn't work, what happens? - The first thing to do would be to open the created file in a text editor and see if contains something unexpected - i.e plaintext.

    – Alex K.
    Nov 14 '18 at 16:30






  • 1





    I believe I've found the answer: it was a fundamental misunderstanding on my part. I thought what I had was a string representation of a byte array. It turns out it was base64 encoded and that also needed to be dealt with. As soon as I added the call to Convert.FromBase64String() instead of just straight up taking the value and converting it to a byte array it worked fine.

    – PatrickA
    Nov 14 '18 at 16:47














0












0








0








I've been given the task of writing automated tests that check our own API. Part of this process involves testing an end point that generates an Excel template that the recipient is then supposed to fill back out and submit back to us.



From the looks of things this template gets sent back to the user from within the browser using a FileContentResult object that also specifies the content type (application/vnd.ms-excel.sheet.macroEnabled.12; the intended file format is .xlsm).



The problem I have is this: whilst getting the file in terms of a byte array works without issue with regards to the call to the end point is concerned I have yet to successfully take that byte array returned and use it for anything useful. Creating an excel file from that seems to be problematic. Just using File.WriteAllBytes() doesn't seem to work for example nor does using a BinaryWriter.



Does anybody have any idea how to achieve this from within C# code that isn't running as part of a website?










share|improve this question
















I've been given the task of writing automated tests that check our own API. Part of this process involves testing an end point that generates an Excel template that the recipient is then supposed to fill back out and submit back to us.



From the looks of things this template gets sent back to the user from within the browser using a FileContentResult object that also specifies the content type (application/vnd.ms-excel.sheet.macroEnabled.12; the intended file format is .xlsm).



The problem I have is this: whilst getting the file in terms of a byte array works without issue with regards to the call to the end point is concerned I have yet to successfully take that byte array returned and use it for anything useful. Creating an excel file from that seems to be problematic. Just using File.WriteAllBytes() doesn't seem to work for example nor does using a BinaryWriter.



Does anybody have any idea how to achieve this from within C# code that isn't running as part of a website?







c# excel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 16:18









Uwe Keim

27.5k32130212




27.5k32130212










asked Nov 14 '18 at 16:17









PatrickAPatrickA

146117




146117








  • 1





    If the byte array is supposed to be an excel file then File.WriteAllBytes() would be the way to create it on disk, you say it doesn't work, what happens? - The first thing to do would be to open the created file in a text editor and see if contains something unexpected - i.e plaintext.

    – Alex K.
    Nov 14 '18 at 16:30






  • 1





    I believe I've found the answer: it was a fundamental misunderstanding on my part. I thought what I had was a string representation of a byte array. It turns out it was base64 encoded and that also needed to be dealt with. As soon as I added the call to Convert.FromBase64String() instead of just straight up taking the value and converting it to a byte array it worked fine.

    – PatrickA
    Nov 14 '18 at 16:47














  • 1





    If the byte array is supposed to be an excel file then File.WriteAllBytes() would be the way to create it on disk, you say it doesn't work, what happens? - The first thing to do would be to open the created file in a text editor and see if contains something unexpected - i.e plaintext.

    – Alex K.
    Nov 14 '18 at 16:30






  • 1





    I believe I've found the answer: it was a fundamental misunderstanding on my part. I thought what I had was a string representation of a byte array. It turns out it was base64 encoded and that also needed to be dealt with. As soon as I added the call to Convert.FromBase64String() instead of just straight up taking the value and converting it to a byte array it worked fine.

    – PatrickA
    Nov 14 '18 at 16:47








1




1





If the byte array is supposed to be an excel file then File.WriteAllBytes() would be the way to create it on disk, you say it doesn't work, what happens? - The first thing to do would be to open the created file in a text editor and see if contains something unexpected - i.e plaintext.

– Alex K.
Nov 14 '18 at 16:30





If the byte array is supposed to be an excel file then File.WriteAllBytes() would be the way to create it on disk, you say it doesn't work, what happens? - The first thing to do would be to open the created file in a text editor and see if contains something unexpected - i.e plaintext.

– Alex K.
Nov 14 '18 at 16:30




1




1





I believe I've found the answer: it was a fundamental misunderstanding on my part. I thought what I had was a string representation of a byte array. It turns out it was base64 encoded and that also needed to be dealt with. As soon as I added the call to Convert.FromBase64String() instead of just straight up taking the value and converting it to a byte array it worked fine.

– PatrickA
Nov 14 '18 at 16:47





I believe I've found the answer: it was a fundamental misunderstanding on my part. I thought what I had was a string representation of a byte array. It turns out it was base64 encoded and that also needed to be dealt with. As soon as I added the call to Convert.FromBase64String() instead of just straight up taking the value and converting it to a byte array it worked fine.

– PatrickA
Nov 14 '18 at 16:47












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304558%2fwriting-a-byte-array-to-an-excel-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304558%2fwriting-a-byte-array-to-an-excel-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values