Forum Discussion
jitterbitse
May 19, 2023Copper Contributor
This workbook couldn't be opened because the file format may not be matching with the file extension
Through the code attached below, I'm trying to create a spreadsheet. The files get created but I'm not able to preview it. Whereas, when I set content-type as application/text-plain to generate a text file. Not only does the file gets created but I'm also able to preview it.
String url = "https://myTenant.sharepoint.com/_api/web/GetFolderByServerRelativeUrl('/Shared%20Documents')/Files/add(url='myFile.xlsx',overwrite=true)";
List<NameValuePair> nameValueHeaders = null;
JSONObject requestBody = new JSONObject();
String fileContent = "dummy";
byte[] contentBytes = fileContent.getBytes(StandardCharsets.UTF_8);
HttpEntity entity = new ByteArrayEntity(contentBytes, ContentType.create("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));// StringEntity(requestBody.toString(), ContentType.APPLICATION_JSON);
HttpUriRequestBase requestBase = httpHandler.getHttpPost(url, nameValueHeaders, entity);
requestBase.setHeader("Authorization", "Bearer " + token);
requestBase.setHeader(HttpHeaders.ACCEPT, ContentType.APPLICATION_JSON);
HttpCallResponse callresponse = httpHandler.executeHttpRequest(requestBase);
System.out.println(callresponse.getResponse());
jitterbitse Can you add more details to your question like, where are you using this code, C# console application or SharePoint add-in or SPFx?
Check below solutions if it works for you:
- How to Create a Excel file in SharePoint?
- Can we create Excel file using c#?
- Adding new Excel file to SharePoint - ignore ask for check-in
Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.